code:
<?php
if($this->input->post('submit'))
{
$data = array(
'enq_id'=> $this->input->post('ids'),
'status' => $this->input->post('status'),
'follow_up_date' => date('d-m-Y')
);
$query = $this->db->insert('enq_process',$data);
if($query == true)
{
echo "<p style='color:green;font-size:12px;text-align:center;'>Your data successfully save.</p>";
}
else
{
echo "<p style='color:red;font-size:12px;text-align:center;'>Error!</p>";
}
}
?>
I have a form having input field name (ids,status) with submit button. Now, I am inserting form data into database now I want that when from data inserting first time then how to update same form data second time ?
$ this-> db-> insert_id()将返回最后插入的行ID,因此您可以在下一个查询中使用此ID。
Hi you can use the following way : First check whether the data by its condition is present in your database table. If exists then update else insert your data
if(post request){
Step 1 : Check whether enqid along with some condtion is already present or not in database table
Step 2:
if (exists) {
UPDATE
} else {
INSERT
}
}
try this
$q = $this->db->get('enq_process');
if ( $q->num_rows() > 0 )
{
$this->db->where('ids',$data['enq_id']);
$this->db->update('enq_process',$data);
} else {
$this->db->insert('enq_process',$data);
}
make new function in same controller
public function insert_or_update($table='', $data=array()){
$seprator=" ";
$update="";
foreach ($data as $feild => $value) {
$update.=$seprator."`$feild`='$value'";
$seprator=', ';
}
$sql = $this->db->insert_string('table', $data) . ' ON DUPLICATE KEY UPDATE '.$update;
if($this->db->query($sql)){
return $this->db->insert_id();
}else{
return false;
}
}
and use this function in your controller
if($this->input->post('submit'))
{
$data = array(
'enq_id'=> $this->input->post('ids'),
'status' => $this->input->post('status'),
'follow_up_date' => date('d-m-Y')
);
$query = $this->insert_or_update('enq_process',$data);
if($query == true)
{
echo "<p style='color:green;font-size:12px;text-align:center;'>Your data successfully save.</p>";
}
else
{
echo "<p style='color:red;font-size:12px;text-align:center;'>Error!</p>";
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.