Zend db - UPDATE does not work - php

I'm using Zend db to update a record in a MySQL table. I have a SELECT that gets a uuid; and then the following to update the same record:
$data = array(
'fieldname' => 'foobar',
);
$where = array();
$where["uuid = ?"] = $uuid;
$db->update('customers', $data, $where);
Unfortunately the record does not update, and I don't get an error message. The uuid is correct.

I think I was missing the execute. I ended up doing something like this:
$update = $db->update('customers');
$update->where(['uuid' => $uuid]);
$update->set(['fieldname' => 'foobar']);
$statement = $db->prepareStatementForSqlObject($update);
$results = $statement->execute();
This worked fine.

Related

how to make a dynamic query using cakephp 3?

I have a filter to search the database from it. But if any of the filter fields are empty it is to ignore.
$name = $this->request->data['name'];
$status = $this->request->data['status'];
$code_transaction = $this->request->data['code_transaction'];
$orders = $this->Orders->find('all')->where(['name'=> $name, 'status'=> $status, 'id'=> $code_transaction]);
when you have a blank field behind not query anything database.
I need help to create a query to do the query in the database when you first filled field and ignore those that are blank. Could anyone help me? I accept any idea to create it.
You can do something like this:
$query = ['name'=> $name, 'status'=> $status, 'id'=> $code_transaction];
$newdataquery =[];
foreach($query as $key => $data) {
if (strlen($data) > 0 )
$newdataquery[$key] = $data;
}
$orders = $this->Orders->find('all')->where($newdataquery);

How do I get the last id from a table after multiple insert

I am adding data to three tables, I needed to get the last ID of the first table to use in the second table, which was successful with $this->db->insert_id() function, Trying that with the second table still gives me the ID of the first table. The arrangement of my code is:
function addcrm() {
//Post data collection array from the webform form
$customerdata = array(
"salutation"=>$this->input->post('salutation'),
"mobilenumber"=>$this->input->post('mobilenumber'),
"emailaddress"=>$this->input->post('emailaddress')
);
$this->db->insert('api_customer', $customerdata);
$customer=$this->db->insert_id();
$leaddata = array(
"fieldrep"=>$this->input->post('fieldrep'),
"fk_customerID"=>$customer,
"te"=>$this->input->post('takage'),
"othercost"=>$this->input->post('othercost')
);
$this->db->insert('api_lead', $leaddata);
$leadID = $this->db->insert_id();
for ($i =0; $i<count($_POST['w_product']); $i++){
$productdata = array(
"name" => $_POST['w_product'][$i],
"type" => $_POST['w_type'][$i],
"cost" => $_POST['w_cost'][$i],
"fk_leadID"=> $leadID
);
$this->db->insert('api_prod',$productdata);
}
$url = base_url('cXXXXXXXXXXXXXX);
redirect($url);
}
You are missing something obviously in the second call. ;)
$customer = $this->db->insert_id();
$leadIdD = $this->db->insert_id;
See? :)
Try working with the following methods:
$this->db->start_cache(); // Before query
$this->db->stop_cache(); // After query
$this->db->flush_cache(); // Clear query
This way you make clear and flushed queries.
if you are using an auto increment table you may try using MYSQL_LAST_INSERT_ID();
http://dev.mysql.com/doc/refman/5.0/en/information-functions.html

$this->db->insert_id() seems to be wiping away my previous insert_id variables value

Here is the code:
$the_question = $_POST['question'];
$the_answer = $_POST['answer'];
$dummy_num[] = $_POST['dummy_answer_1'];
$dummy_num[] = $_POST['dummy_answer_2'];
$dummy_num[] = $_POST['dummy_answer_3'];
//Get Hidden Test ID and Q_order
$test_id = $_POST['test_id'];
$q_order = $_POST['q_order'];
//Submit Question
$data_submit_q = array (
'type' => 1,
'question' => $the_question,
'done' => 1
);
$this->db->where('test_id', $test_id);
$this->db->where('q_order', $q_order);
$this->db->update('questions', $data_submit_q);
$question_id = $this->db->insert_id();
$time_created = date('Y-m-d H:i:s');
//Submit Answer
$data_submit_a = array (
'test_id' => $test_id,
'question_id' => $question_id,
'option' => $the_answer,
'company_id' => $data['company']->id,
'job_id' => $data['session_job_id'],
'time_created' => $time_created
);
$this->db->insert('options', $data_submit_a);
$answer_id = $this->db->insert_id();
//Let question know that answer is right.
$data_submit_qr = array (
'answer_id' => $answer_id
);
$this->db->where('id', $question_id);
$this->db->where('test_id', $test_id);
$this->db->update('questions', $data_submit_qr);
Setting the answer id removes the value of the question id, then on updating the database the answer id has no value also. Even though it does right before.
The method $this->db->insert_id() retrieves the ID when performing database inserts (as the name hints).
You're using it after an update, that's why your $question_id gives problems (I think it would be set to FALSE, but I don't know for sure what does that method return when called on the wrong context). WHen you do your last update you use this as a WHERE condition, and if it is not set...
It's not that your second call to insert_id() wipes out the first, I suspect is more like the first one is already NOT SET (or FALSE)
It seems that there is a bug with insert_id, you can try using:
$query = $this->db->query('SELECT LAST_INSERT_ID()');
$row = $query->row_array();
$lastInsertId = $row['LAST_INSERT_ID()'];
Hope it helps

am i writing this update query correctly?

this is the sql query i used before i switch to codeigniter
UPDATE transaction SET due_amount = to_pay-$payment_amount WHERE id = '$id'
with codeigniter
$this->db->set('due_amount', 'to_pay-'.$payment_amount, FALSE);
$this->db->where('id', $id);
$result = $this->db->update('transaction');
in the codeigniter code am i writing $this->db->set correctly?
Regards
You should do it like this:
$this->db->where('id', $id);
$result = $this->db->update('transaction',array('due_amount'=>'to_pay-'.$payment_amount));
Hope this helps.
There are multiple variants to use the updateDocs function. Yours doesn't look specifically bad (I would however don't bypass escaping if not for a specific reason), maybe you prefer to use an array (or object) to provide the data being updated:
$data = array(
'due_amount' => 'to_pay-'.$payment_amount,
);
$this->db->update('transaction', $data, sprintf('id = %d', $id));
I think here could be some issue with $this->db->set
Standard way of updating content in codeignitor is this
$data = array(
'title' => $title,
'name' => $name,
'date' => $date
);
$this->db->where('id', $id);
$this->db->update('mytable', $data);
Produces:
UPDATE mytable
SET title = '{$title}', name = '{$name}', date = '{$date}'
WHERE id = $id

Zend Framework Database Insert Issue

I'm starting out using the Zend Framework and have created a suitable model which saves data back to a database table. The issue I am having is that the sql statement is trying to insert '?' as the value for each column in the database. I have created the following save function which passes an array of data to the DBtable adapter functions:
public function save() {
$data = $this->getData();
if ($data['pageId']==0) {
$this->getDbTable()->insert($data);
} else {
$this->getDbTable()->update($data, array('pageId = ?' => $data['pageId']));
}
}
This seems to go through the appropriate motions but the item is not added to the database and the sql statement within MySql logs looks something like:
insert into DB_Table ('pageId','title','body') values ('?', '?', '?');
Not quite sure where this is falling down, any pointers would be gratefully received.
Thanks
Data should be in next format:
$data = array(
'pageId' => 1,
'title' => 'title',
'body' => 'body'
);
Are you sure that $this->getDbTable() returns your db adapter?
Try to use:
$db = new Zend_Db_Table('table_name');
$db->insert($data);

Categories