Welcome Guest, Not a member yet? Register   Sign In
CI returns last value in a table when it doesnt have to...
#1

[eluser]Unknown[/eluser]
Hello Everyone, i´m new to CI, and i wish somebody could help me. I created a table with and id and sometext...also created a model who retrieves information, a controller and a view...the thing is that in the view i put an input for deleting the information retrieved from the database...
So my error is that it doesnt matter which delete input i click...it deletes the last row in the table...please somebody help...thank you everyone... sorry for my bad english... :S

Controller
Code:
<?php
class Jokes extends CI_Controller
{
  
function __construct(){
  parent::__construct();
  $this->load->model("MJokes");
}

public function index(){
  $data['chistes'] = $this->MJokes->obtenerTodos();
  $this->load->view("jokes",$data);
}

public function addJoke(){
  $this->load->view("addjoke");
}

function createJoke(){
  if ($this->input->post('joketext')){
   $this->MJokes->agrChiste();
   redirect('jokes/index','refresh');
  }
}

function deleteJoke($id){
  if ($this->input->post('id')){
   $this->MJokes->brrChiste($id);
  }
  redirect('jokes/index','refresh');
}

}
?>

Model
Code:
?php
class MJokes extends CI_Model
{
function __construct(){
  parent::__construct();
}

function obtenerTodos(){
  $keri = $this->db->query("SELECT id, joketext FROM joke");
  return $keri->result_array();
}

function agrChiste(){
  $data = array('joketext' => $_POST['joketext'],
    'jokedate' =>  $_POST['jokedate'],
    // 'authorname' => $_POST['authorname'],
    // 'authoremail' => $_POST['authoremail']
    );
    
  $this->db->insert('joke',$data);
}

function brrChiste($id){
  $this->db->delete('joke', array('id' => $id));
}
}

?>

The View
Code:
<!DOCTYPE html>
&lt;html lang="en"&gt;
&lt;head&gt;
  &lt;meta charset="utf-8" /&gt;
  &lt;title&gt;jokes&lt;/title&gt;
  &lt;meta name="viewport" content="width=device-width; initial-scale=1.0" /&gt;
&lt;/head&gt;

&lt;body&gt;
  <div>
   &lt;header&gt;
    <h1>The funniest jokes of all time!!!</h1>
   &lt;/header&gt;
  
   <section>
    &lt;?php
     foreach($chistes as $joke): ?&gt;
    
    &lt;?php echo form_open('jokes/deleteJoke');?&gt;
     <blockquote><p>
      &lt;?php
      echo $joke['joketext'];
      $data = array(
          'name'=>'id',
          'value'=>$joke['id']
         );
      echo form_input($data);
      
      echo form_submit('delete','Borrar Chiste');
      ?&gt;
     </p></blockquote>
    &lt;?php endforeach; ?&gt;
   </section>
   <aside>
    <a >Cuéntanos un chiste.</a>
   </aside>
  
   <footer>
    <p>
     &copy; Copyright  by Sistemas
    </p>
   </footer>
  </div>
&lt;/body&gt;
&lt;/html&gt;
#2

[eluser]john_j[/eluser]
Looks like your primary key is the id. If you are properly picking the id when user clicks delete and then using the same id in the sql to delete, you shouldn't be having this issue.
Take a look at your sql used for deletion.




Theme © iAndrew 2016 - Forum software by © MyBB