Welcome Guest, Not a member yet? Register   Sign In
Help deleting a record from my database.
#1

[eluser]snoopy[/eluser]
Hello there im new to codeigniter and MVC and im tryng to create a code to delete a record in my database for several days now here is my actual code:

model :
Code:
function delete($id)
    {
      $this->db->where('sale_id', $id);
      $this->db->delete('phppos_sales');
      
      return $this->db->affected_rows();    }

controller :
Code:
function apaga_venda($sale_id)
    {
     $sale_id = $this->input->post('sale_id');
     $this->load->model('sale');
    
     if ($this->sale->delete($sale_id))
     {
     redirect('vendas');
    
     }  else { die('Erro');  }

view :
Code:
<ul>

&lt;?php foreach($vendas as $v){

echo anchor('vendas/apaga_venda/$v->sale_id', 'Apaga').'Data da Venda: '.$v['sale_time']. " / ".'ID do Cliente: '.$v['customer_id']. " / ".' ID da Venda: '.$v['sale_id']."<br />";

}
?&gt;

</ul>

Thanks in advance for the help!
ps: im desperate =)
#2

[eluser]JHackamack[/eluser]
I believe the problem lies in the anchor tag you're using. You're mixing object arrays with standard arrays for $v and you've encapsulated the anchor in single quotes which won't be executed by PHP so if you look at the link in a web browser it'd look like this:
Code:
http://example.com/vendas/apaga_venda/$v->sale_id

Try this:
Code:
echo anchor('vendas/apaga_venda/'.$v->sale_id, 'Apaga').'Data da Venda: '.$v['sale_time']. " / ".'ID do Cliente: '.$v['customer_id']. " / ".' ID da Venda: '.$v['sale_id']."<br />";

or

echo anchor('vendas/apaga_venda/'.$v['sale_id'], 'Apaga').'Data da Venda: '.$v['sale_time']. " / ".'ID do Cliente: '.$v['customer_id']. " / ".' ID da Venda: '.$v['sale_id']."<br />";
#3

[eluser]snoopy[/eluser]
Well the url problem has been solved now the anchor is sending the the correct id now.
But im stil getting an error while tryng do delete the row i think its on the controller parameter or in the model, thanks alot JHackamack.
#4

[eluser]JHackamack[/eluser]
The logic for the model seems a little off to me. If i were trying to check if a row is deleted I'd do something like:
Code:
$this->db->where('sale_id', $id);
if($this->db->delete('phppos_sales')) {
     return TRUE;
}
return FALSE;
affected rows might not always work because if the row is already removed in the db, the query will still fire off successfully but return 0 rows affected, which evaluates to false.
#5

[eluser]snoopy[/eluser]
im tryng with this code now but im still getting an error in the controller.

model :

Code:
function delete()
    {
     $this->db->where('sale_id', $this->uri->segment(3));
     if($this->db->delete('phppos_sales')) {
     return TRUE;
}
return FALSE;  
    }

controller :

Code:
function apaga_venda()
    {
    
     $this->load->model('sale');
    
     if ($this->sale->delete())
     {
     redirect('vendas');
    
     }  else { die('Error');  }
        
    }

viewer :

Code:
<ul>

&lt;?php foreach($vendas as $v){

echo anchor('vendas/apaga_venda/'.$v['sale_id'], 'Apaga').'Data da Venda: '.$v['sale_time']. " / ".'ID do Cliente: '.$v['customer_id']. " / ".' ID da Venda: '.$v['sale_id']."<br />";

}
?&gt;

</ul>

Im getting this errer message:

Cannot delete or update a parent row: a foreign key constraint fails (`pos`.`phppos_sales_items`, CONSTRAINT `phppos_sales_items_ibfk_2` FOREIGN KEY (`sale_id`) REFERENCES `phppos_sales` (`sale_id`))
#6

[eluser]snoopy[/eluser]
Well i figured it out already, the error was a database issue ...
Thanks alot for the help finally solved my headache =) peace




Theme © iAndrew 2016 - Forum software by © MyBB