Update query insert zero in table in ci - php

Update query insert zero in table everytime.
I have prtinted the query.From phpmyadmin the lastquery working fine.updated with same value
But when db active query then it has updating 0.
tbl_setitbl
set_id(primary key)
reference(text)`
Here is my code.
public function edit_set($id,$setvalue)
{
$data = array('reference' => $setvalue);
$this->db->where('set_id', $id);
$this->db->update('tbl_setitbl', $data);
if($this->db->affected_rows())
return true;
else
return false;
}

I have tried this code also.
$this->db->where('set_id', $id);
$this->db->update('tbl_setitbl', array('reference' => $setvalue));
echo $this->db->last_query();
UPDATE tbl_setitbl SET reference = 'hhhhhhhh' WHERE set_id = 1

Sorry every one...
Get solved
actually the problem is in controller.
query has run two times as redirection has not doing properly

see the result by using $this->db->last_query() then verify the sql code if it is similiar to the sql code that you have tried in phpmyadmin

Related

codeigniter and mysql update error?

when i try to update a table i get an error that time.
there is my controller code.
$this->hr_user->insert_user($info);
$n=explode('--', $info['user_code']);
$temp=$n[1];
$lnum=ltrim($temp,'0');
//print_r($lnum);
//echo "<br>lastnumber".$lnum;
$id=$info['user_type'];
echo $id.''.$lnum;
$this->hr_user->update_number($id,$lnum);
redirect('hrm/index');
and here is my model code.
public function update_number($id,$lnum)
{
$this->db->where('em_id',$id);
$this->db->update('tbl_emaster.em_lastnumber',$lnum);
}
$this->db->set('field', 'field+1');
$this->db->where('id', 2);
$this->db->update('mytable');
// gives UPDATE `mytable` SET `field` = ' field+1' WHERE `id` = 2
https://www.codeigniter.com/userguide3/database/query_builder.html#updating-data
The mysql error you got explains every thing. Your query order doesn't generate a qualified query, try:
public function update_number($id,$lnum)
{
$this->db->set('eenter code herem_lastnumber', '$lnum');
$this->db->where('em_id',$id);
$this->db->update('emaster');
}
for more info www.codeigniter.com
Mysql update query syntax is :
UPDATE table_name SET field1 = new-value1, field2 = new-value2
[WHERE Clause]
You can see, there are fields after SET.
And because of your CI update syntax, column name is not being passed. And to pass that, you should pass array as 2nd argument of $this->db->update('tbl_emaster.em_lastnumber',$lnum);
So you need to change it like this:
$this->db->update('tbl_emaster',array('em_lastnumber' => $lnum);
change query
public function update_number($id,$lnum)
{
$data=array('em_lastnumber'=>$lnum);
$this->db->where('em_id',$id);
$this->db->update('tbl_emaster',$data);
}

Codeigniter Transfer data table to table

I'm trying to insert data from a table in MySql to other table , but I need to insert a new key before , so I tried this:
$this->db->trans_start();
$this->db->insert($this->table, $data);
$prodId = $this->db->insert_id();
$this->db->from('tmp_prod_icms');
$this->db->where('CADKEY', $this->session->userdata('key'));
$tmpICMS = $this->db->get()->result();
foreach($tmpICMS as $r) { // loop over results
$r["CADPROCOD"] = $prodId;
$this->db->insert('prod_icms', $r);
}
$this->db->trans_complete();
But when I try to insert the key "CADPROCOD" the app just stops, don't give even a error, just stops
Have you checked the logs? In the MySQL ones you should most likely find the error and why it fails.
I found the error, I use $tmpICMS = $this->db->get()->result();
And this return a object, so I can't manipulate this like a Array, so i just need to change for $tmpICMS = $this->db->get()->result_array();
So this work for me

Codeigniter Active Record Insert Function

So i have been pulling my hair out over this for the past two days. I have identified the problem down to this so far:
I am inserting some simple data into the database using Active Record:
if($this->db->insert('table', $data)){
return true;
}
else{
return false;
}
The problem is that it was always returning true whether the data got inserted or not. How i figured this out was that after several failed attempts when the data finally got inserted, the AUTO_INCREMENT ID was at 17, meaning that the insert query was running but failing to insert, hence always returning true. I want to know a reliable method of knowing whether data got inserted or not. Tried:
$this->db->affected_rows() > 0;
as well. But same issue prevails. It returns true.
If you have auto incremental id in your table then check $this->db->insert_id()
if greater the zero or us number then we can say data inserted or
again fire a sql query with id we get and if record exist then data is inserted
but i think that is not necessary just check insert_id
You need to insert your data first and get the result after. You will need to do something like this:
$this->db->insert('my_table', $my_data);
$is_inserted = $this->db->affected_rows() > 0;
if($is_inserted) {
echo 'Yay! Its works!';
} else {
echo 'Something went wrong. No insert ):'
}
You must perform your insert query before get the result. So you will need to run your insert and then get the affected rows. Codeigniter provides the class $this->db that help us to do this very easily. You can even get the inserted id running $this->db->insert_id() instead $this->db->affected_rows() to get the brand new inserted id.
You may find these links useful:
Query Helper Functions -
https://ellislab.com/codeigniter/user-guide/database/helpers.html
Active Record Class
https://ellislab.com/codeigniter/user-guide/database/active_record.html
Good Luck!
i always do this for check data is inserted or not
$this->db->insert('table', $data);
$id = $this->db->insert_id();
if($id)
{
echo "data inserted";
}
else
{
echo "data not inserted";
}
you can also do this also worked for me
$query = $this->db->insert('table', $data);
if($query)
{
echo "data inserted";
}
else
{
echo "data not inserted";
}
This is what I do. It works for me.
$this->db->set($data)->insert($table_name);
if ($this->db->affected_rows()) {
return $this->db->insert_id(); // or return true; in your case
}
return false;

MySQL: Can't get LAST_INSERT_ID()

I'm creating an application using FuelPHP framework and MySQL and I'm trying to AJAX-update/insert a new log for an item already in DB.
This is my MySQL code:
UPDATE `work_orders` SET `status_id`='{$status}' WHERE `id` = '{$wo_id}';
INSERT INTO `work_order_logs`(`id`, `work_order_id`, `log_text`, `status_id`) VALUES ('{$id}', '{$wo_id}', '{$text}', '{$status}')
ON DUPLICATE KEY UPDATE `log_text`='{$text}',`status_id`='{$status}';
SELECT LAST_INSERT_ID() as id;
When this code is executed from phpmyadmin it runs successfully and returns the id, however when executed from FuelPHP it only returns 1, which I assume means a successful operation.
The FuelPHP code:
public static function updateLogById($id, $wo_id, $text, $status)
{
try {
$log_query = \DB::query("
UPDATE `work_orders` SET `status_id`='{$status}' WHERE `id` = '{$wo_id}';
INSERT INTO `work_order_logs`(`id`, `work_order_id`, `log_text`, `status_id`) VALUES ('{$id}', '{$wo_id}', '{$text}', '{$status}')
ON DUPLICATE KEY UPDATE `log_text`='{$text}',`status_id`='{$status}';
SELECT LAST_INSERT_ID() as id;
")->execute();
} catch(\Database_Exception $e) {
return array(false, \DBE::handle_error());
}
return array(true, $log_query);
}
Can anybody see, what's wrong?
Thanks for any answer.
separate the insert from the update that will fix it
In order to get the right result you'll have to supply the query type in this case because it's not designed to do super smart result type detection based on the query. I think your query should work when you supply the following second parameter:
$result = \DB::query($query, \DB::SELECT);
This should give you a Result object from which you can get the id.

Codeigniter handle database error without num_rows()

I'm having trouble finding a simple method for handling database errors in CI. For instance, I can't insert duplicate entries in my database table. If I try to, I get a 1062 database error.
The most common solution suggested is to check if the entry already exists and use
$query->num_rows() > 0
in a if-statement to prevent an error. That method seems redundant to me because I'm performing an extra query. Ideally I want to check if an error occurs in my main query or if a row is affected.
I found the following functions that may help
$this->db->affected_rows()
$this->db->_error_message()
however I'm not sure how to use them.
I tried in my Model:
$this->db->insert('subscription', $data);
return $this->db->affected_rows();
To my understanding that should return the number of effected rows. Then in my controller I added:
$affected = $this->Subscribe_model->subscribe($data);
if ($affected < 1)
{
//display error message in view
}
else
{
$this->Subscribe_model->subscribe($data); //perform query
}
Unfortunately the script stops in the model at $this->db->insert('subscription', $data); if an error occurs and displays the entire database error.
I do not know if this works for $this->db->insert();, but $this->db->query(); will return false if it errors so you could do something like this:
$sql = $this->db->insert_string('subscription', $data);
$sql = $this->db->query($sql)
if(!$sql){
//Do your error handling here
} else {
//query ran successfully
}
Try using #$this->db->insert('subscription', $data);, #, in PHP means "suppress warning".
As an alternate -- if you know that data is safe, or you're willing to use $this->db->insert_string, you could add, on duplicate key to the end of the query.
This should work (untested):
$this->db->simple_query( $this->db->insert_string( 'subscription', $data ) .
' ON DUPLICATE KEY ' .
$this->db->update_string(
'subscription',
$data,
/* your where clause here */ );

Categories