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
Related
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.
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);
I'm making a game and I ran into some trouble with a query that won't seem to work what ever I try to do. I have four variables (that are validated)
$id, $plantId, $stage, $started
And my query looks like this...
$sql = "
UPDATE growth
SET plantId = '$plantId', stage = '$stage', started = '$started'
WHERE id = '$id'
";
It returns false, but I tried the same query in phpMyAdmin and it worked!
What am I doing wrong?
Try not to use plain MySql. I know that learning new technologies might feel overwhelming but we live in year 2016. There are plenty of tools that will make your life easier.
For example DiBi database layer:
https://github.com/dg/dibi
You can use either OFFICIAL VERSION as shown in the documentation:
SELECT:
dibi::query('SELECT * FROM users WHERE id = ?', $id);
INSERT
$arr = [
'name' => 'John',
'is_admin' => TRUE,
];
dibi::query('INSERT INTO users', $arr);
UPDATE
dibi::query('UPDATE users SET', $arr, 'WHERE `id`=?', $x);
Or you can use FLUENT VERSION:
SELECT
$res = dibi::select('product_id')->as('id')
->select('title')
->from('products')
->innerJoin('orders')->using('(product_id)')
->orderBy('title')
->fetchAll();
INSERT
$record = array(
'title' => 'Výrobek',
'price' => 318,
'active' => TRUE,
);
dibi::insert('products', $record)
->execute();
UPDATE
$record = array(
'title' => 'Výrobek',
'price' => 318,
'active' => TRUE,
);
dibi::update('products', $record)
->where('product_id = %d', $id);
->execute();
It will make your life much easier. Learning fluent version and concatenating your queries is even quite fun. When you need to test your query just use test() method.
You do not even have to worry about sql injections.
I try to insert data to a table in CI 3 that has fields as below:
pageid is auto increment and I write code in model as this:
$data = array(
'title'=>$title,
'description'=>$des,
'url'=>'page/view/'.pageid
);
$this->db->insert('page',$data);
The problem is that the pageid is unknown as the field name. What should I do because I want to concatenate pageid to url?
You need two query for it
1)You need to insert title and description first and get last page id from database
$data = array(
'title' => $title,
'description' => $des
);
$this->db->insert('page', $data);
$page_id = $this->db->insert_id();// get last pageid
2)Update url second
$data = array(
'url' => 'page/view/'.$page_id
);
$this->db->where('pageid'$page_id);// update with page id
$this->db->update('page', $data);
OK so you can add all data first then after insert just update the url
field.
$data = array(
'title' => $title,
'description' => $des,
'url'=>'page/view/'
);
$this->db->insert('page', $data);
$page_id = $this->db->insert_id();
2)Now update URL
$update= array(
'url' => 'page/view/'.$page_id
);
$this->db->where('page_id'$page_id);
$this->db->update('page', $update);
Hope this will help you
Common & Easy Method
Goto
Phpmyadmin
select your database, then go to page table.
Click structure on top.
Click Change, in front of id field.
Then there is check box with AI(Auto Increment)
Check that and click save
If with code(use this)
public function FunctionName()
{
$query = $this->db->query("SELECT id FROM pages ORDER BY id DESC LIMIT 1");
$result = $query->result_array();
$id = $result['id']; // or some times $result[0]['id']
$data = array(
'title' => $title,
'description' => $des,
'url' => 'page/view/'.pageid,
'id' => $id
);
$this->db->insert('page',$data);
}
Just do like this:
$data = array(
'title'=>$title,
'description'=>$des
);
$this->db->insert('page',$data);
$page_id = $this->db->insert_id();
//To pass page_id to url column
$this->db->where('page_id', $page_id);
$data_1 = array('url'=>'page/view/'.$pageid);
$this->db->update('page', $data_1);
You can first get the highest Auto-Incremented id from select query and then, Use insert query for inserting the new row.
eg.
$id = $this->db->query("Select max(page_id) AS page_id from pages");
if($id->num_rows() > 0){
$result = $id->result_array();
$page_id = $result[0]['page_id'] +1;
}
// if table is empty
else{
$page_id = 1;
}
$data = array(
'title' => $title,
'description' => $des,
'url'=>'page/view/'.$page_id );
$this->db->insert('page',$data);
Please check it image for add auto increment id in phpmyadmin.
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