I am trying to update specific row in tables using button tag .But it is updating last inserted row only and not the selected button tag.
Model
public function title_d($data,$title_id)
{
$this->db->where('title_id',$title_id);
$this->db->update('experience',$data);
}
view
<form method="POST" action="http://localhost/portfolio/user/title">
<?php
foreach($experience->result() as $values1) {
?>
<section >
<section style="background: #fff; width: 30%; height: 250px;">
<input type="text" value="<?php echo $values1->title_id;?>" name="title_id" hidden />
<h5>Title</h5>
<input type="text" value="<?php echo $values1->title;?>" name="title" style="width: 200px; height: 30px; border: 1px solid;">
</section>
<section style="background: #fff; width: 23%;height: 250px;">
<h5>Write What you know</h5>
<textarea style=" height: 150px; width: 180px;" name="write">
<?php echo $values1->write;?>
</textarea>
</section>
<section style="background: #fff; width: 25%; height: 250px; ">
<h5>update your details</h5>
<button type="submit" value="<?php echo $values1->title_id; ?>" style="background:#1C8ACA; height: 40px; " >Update<?php echo $values1->title_id; ?></button>
<button type="submit" value="<?php echo $values1->title_id; ?>"style="background:#ff0000; height: 40px; " >Delete<?php echo $values1->title_id;?></button>
</section>
</section>
<?php
}
?>
</form>
controller:
Class User extends CI_Controller {
public function index()
{
$this->load->model('User_model');
$user_id = 1;
$data['admin'] = $this->User_model->admin_data($user_id);
$data['images'] = $this->User_model->get_images($user_id);
$data['names'] = $this->User_model->get_name();
$data['intro'] = $this->User_model->get_intro();
$data['experience'] = $this->User_model->get_title();
$data['contact'] = $this->User_model->get_contact();
$this->load->helper('url');
$this->load->view('Home',$data);
}
public function title()
{
$this->load->model('User_model');
$title_id = $this->input->post('title_id');
$title = $this->input->post('title');
$write = $this->input->post('write');
$data = array("title"=>$title,"write"=>$write);
$this->User_model->title_d($data,$title_id);
$this->User_model->delete_title($title_id,$data);
redirect('/');
}
}
CodePudding user response:
I think you have to create your loop with the form inside, like:
<?php foreach($experience->result() as $values1) { ?>
<form method="POST" action="http://localhost/portfolio/user/title">
<section >
<section style="background: #fff; width: 30%; height: 250px;">
<input type="text" value="<?php echo $values1->title_id;?>" name="title_id" hidden />
<h5>Title</h5>
<input type="text" value="<?php echo $values1->title;?>" name="title" style="width: 200px; height: 30px; border: 1px solid;">
</section>
<section style="background: #fff; width: 23%;height: 250px;">
<h5>Write What you know</h5>
<textarea style=" height: 150px; width: 180px;" name="write">
<?php echo $values1->write;?>
</textarea>
</section>
<section style="background: #fff; width: 25%; height: 250px; ">
<h5>update your details</h5>
<button type="submit" value="<?php echo $values1->title_id; ?>" style="background:#1C8ACA; height: 40px; " >Update<?php echo $values1->title_id; ?></button>
<button type="submit" value="<?php echo $values1->title_id; ?>"style="background:#ff0000; height: 40px; " >Delete<?php echo $values1->title_id;?></button>
</section>
</section>
</form>
<?php } ?>