Get max value from table - php

I am applying this query:
$this->db->select_max('product_id');
$this->db->from('products');
$query=$this->db->get();
return $query->result();
to get max value from table.
But when i add this result to db in another table. like this:
$this->db->insert('images',$product_id);
It show this error:
Error Number: 1054
Unknown column 'Array' in 'field list'
INSERT INTO `images` (`product_id`) VALUES (Array)
Filename: C:\wamp\www\system\database\DB_driver.php
Line Number: 330
I am using codeigniter.

You are applying an array within input here's I have updated your code
Model Code
$this->db->select_max('product_id');
$this->db->from('products');
$query=$this->db->get();
return $query->result_array();
Controller Code
$product_id = $this->Product_area_model->get_product_id();
$data['product_id'] = $product_id[0]['product_id'];
$result = $this->your_model->foo($data);
Model Code
function foo($data = ''){
$this->db->insert('images',$data);
return $this->db->insert_id();
}

Hope it will work for you...
$this->db->insert('images',$product_id[0]['product_id']);

Codeigniter allows you to specify an array of values as the data to insert, but I think your issue is that you're inserting an array of arrays.
If that's the case then use the batch function...
$data = array(
array(
'title' => 'My title' ,
'name' => 'My Name' ,
'date' => 'My date'
),
array(
'title' => 'Another title' ,
'name' => 'Another Name' ,
'date' => 'Another date'
)
);
$this->db->insert_batch('mytable', $data);
// Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date'), ('Another title', 'Another name', 'Another date')

Related

How to use insert_string query helper with SQL function?

My database is using UUIDs as a primary key. When I insert into the DB (mariaDB), I need to do:
insert into table_name (id, parent_id, name,... etc. )
values (UUID_TO_BIN(UUID()), 'a UUID', 'record name', .etc)
I would like to use CI's insert_string function, but this array:
$data = array(
'id' => 'UUID_TO_BIN(UUID())',
'name' => 'record name',
'parent_id' => 'UUID_TO_BIN(' . $parent_id . ')'
);
$this->db->insert_string('table_name',$data);
...I do not think will work, because each result is escaped, so CI will escape the whole text including the function, instead of only what is inside the UUID_TO_BIN function in the parent_id value.
I am trying to figure out if this is a possibility for the parent_id to run the function given. Otherwise, I guess the easiest way is to do the conversion to BIN from HEX in PHP, but will that break the SQL?
You could use the set() method, which accept optional third parameter ($escape), that will prevent data from being escaped if set to FALSE on the id column.
$data = array(
// 'id' => 'UUID_TO_BIN(UUID())',
'name' => 'record name',
'parent_id' => 'UUID_TO_BIN(' . $parent_id . ')'
);
//set id column value as UUID
$this->db->set('id', 'UUID_TO_BIN(UUID())', FALSE);
$this->db->insert_string('table_name', $data);
more on set() method.

Laravel database insert query

I'm trying to do simple insertion in database and im getting the following error
call_user_func_array() expects parameter 1 to be a valid callback, no
array or string given
The insertion code look like this
DB::insert('insert into users (id, name) values (?, ?)', array(1, 'Dayle'));
It's a basic query for laravel, but it won't work why?
DB::table('users')->insert(
array(
'id' => '1',
'name' => 'Dayle'
)
);
or
$values = array('id' => 1,'name' => 'Dayle');
DB::table('users')->insert($values);
But why are you inserting an ID? Should this not be an auto incremented value?
$user['id'] = 1;
$user['name'] = 'dale';
DB::table('users')->insert($user);
You can make simple like this
$val={
'id_user' => '< ID_USER >',
'title' => '< TITLE >',
'body' => '< BODY >',
}
Forum::create($val);
And its work

empty field show error for insert batch in codeigniter

I have multiple form field. I want to insert more than 25 row in db with insert_batch in codeigniter. like
public function purchase(){
$data = array(
array(
'date'=> $this->input->post('date'),
'vendor_name'=>$this->input->post('vendor_name'),
'model'=>$this->input->post('model'),
'price' =>$this->input->post('price'),
'purchase_quantity'=>$this->input->post('quantity'),
'amount' =>$this->input->post('price')*$this->input->post('quantity'),
'invoice_no'=>$this->input->post('invoice')
),
array(
'date'=> $this->input->post('date'),
'vendor_name'=>$this->input->post('vendor_name2'),
'model'=>$this->input->post('model2'),
'price' =>$this->input->post('price2'),
'purchase_quantity'=>$this->input->post('quantity2'),
'amount' =>$this->input->post('price2')*$this->input->post('quantity2'),
'invoice_no'=>$this->input->post('invoice')
),
array(
'date'=> $this->input->post('date'),
'vendor_name'=>$this->input->post('vendor_name3'),
'model'=>$this->input->post('model3'),
'price' =>$this->input->post('price3'),
'purchase_quantity'=>$this->input->post('quantity3'),
'amount' =>$this->input->post('price3')*$this->input->post('quantity3'),
'invoice_no'=>$this->input->post('invoice')
)
);
$insert = $this->db->insert_batch('purchase',$data);
return $insert;
}
but if I complete two field and submit it show error like
Incorrect integer value: '' for column 'purchase_quantity' at row 3
INSERT INTO `purchase` (`amount`, `date`, `invoice_no`, `model`, `price`,
`purchase_quantity`, `vendor_name`) VALUES
(4995,'18-09-2014','vvvvv','m6','999','5','mizan'),
(5000,'18-09-2014','vvvvv','ab12','1000','5','abcde'), (0,'18-09-2014','vvvvv',0,0,'','')
Filename: C:\xampp\htdocs\final\sys\database\DB_driver.php
Line Number: 330
Please help.
use intval before integer values..
intval($this->input->post('quantity2'))
https://ellislab.com/forums/viewthread/238144/
It will be better if you use variables to insert batch data.
$purchase_quantity=empty($this->input->post('quantity3'))==true?0:$this->input->post('quantity3');
And use this $purchase_quantity in array to insert.
The code above will check if quantitiy3 have empty value (ie 0,null or false) it will replace with 0 (An integer) and save in variable that you can use in array to insert
you can set the values as null or default value if form elements are empty like this..
'date'=>($this->input->post('date'))?$this->input->post('date'):null;
or
'date'=>($this->input->post('date'))?$this->input->post('date'):date('Y-m-d');
Or do like this if you want to ignore insert totally:
$data = array();
for($i=1;$i<=25;$i++)
{
if($i>1)
{
if($this->input->post('vendor_name'.$i) && $this->input->post('model'.$i) && $this->input->post('price'.$i) && $this->input->post('quantity'.$i))
{
$record = array(
'date'=> $this->input->post('date'),
'vendor_name'=>$this->input->post('vendor_name'.$i),
'model'=>$this->input->post('model'.$i),
'price' =>$this->input->post('price'.$i),
'purchase_quantity'=>$this->input->post('quantity'.$i),
'amount' =>$this->input->post('price'.$i)*$this->input->post('quantity'.$i),
'invoice_no'=>$this->input->post('invoice')
);
$data[]=$record;
}
}
else{
if($this->input->post('vendor_name') && $this->input->post('model') && $this->input->post('price') && $this->input->post('quantity'))
{
$record = array(
'date'=> $this->input->post('date'),
'vendor_name'=>$this->input->post('vendor_name'),
'model'=>$this->input->post('model'),
'price' =>$this->input->post('price'),
'purchase_quantity'=>$this->input->post('quantity'),
'amount' =>$this->input->post('price')*$this->input->post('quantity'),
'invoice_no'=>$this->input->post('invoice')
);
$data[]=$record;
}
}
}

mysql insert batch vs. insert loop (codeigniter)

kindly enlightened me, which is faster/better approach or just the same between CI batch insert and loop insert.
$data = array(
array(
'title' => 'My title' ,
'name' => 'My Name' ,
'date' => 'My date'
),
array(
'title' => 'Another title' ,
'name' => 'Another Name' ,
'date' => 'Another date'
)
);
batch insert:
$this->db->insert_batch('mytable', $data);
/* produces: INSERT INTO mytable (title, name, date)
VALUES ('My title', 'My name', 'My date'),
('Another title', 'Another name', 'Another date'); */
loop insert (php):
for( $i = 0; $ < count($data); $i++ )
{
INSERT INTO mytable (title, name, date)
VALUES ($data[$i]['title'], $data[$i]['name'], $data[$i]['date'])
}
thanks!
Batch inserts are usually faster since they process the data in once, were as the INSERT has some overhead (eg, the SQL optimizer cannot deduct certain steps). That said, you need to process a tremendous number of rows to create a notable difference.
If your curious if it would matter anyway, then don't forget to also measure the time it costs the framework to map the classes to the database table(s). There's a good chance the ORM consumes more resources than a looped SQL INSERT.

How do I update multiple inputs with the same name in codeigniter?

This is what I have so far
Controller
$i = 0;
foreach ($this->input->post('skill') as $cat) {
$data[$i++]['skill'] = $cat;
}
$this->db->update_batch('skills', $data);
}
Model
function update_record($data)
{
$this->db->update('skills', $data);
}
View
<?php foreach ($skills as $skill):?>
<input type="text" name="skill[]" value="<?php echo $skill->skill;?>">
<?php endforeach?>
I am getting a database error
You must specify an index to match on for batch updates.
Please help me fix this I tried googling and nothing is coming up.
Ok, here is the issue.
According to the CI Docs for update_batch you need to add a third parameter that is the where key . Here is an example from the docs.
$data = array(
array(
'title' => 'My title' ,
'name' => 'My Name 2' ,
'date' => 'My date 2'
),
array(
'title' => 'Another title' ,
'name' => 'Another Name 2' ,
'date' => 'Another date 2'
)
);
$this->db->update_batch('mytable', $data, 'title');
So, above in the queries the title column is what the row is compared to. So for the rows that have title as 'My Title' The first array element is used as update and for those with 'Another Title' the second element. I hope you understand. If we don't have a compare field then the whole database will be updated :-P
Here is the final query produced from the above operation.
UPDATE `mytable` SET `name` = CASE
WHEN `title` = 'My title' THEN 'My Name 2'
WHEN `title` = 'Another title' THEN 'Another Name 2'
ELSE `name` END,
`date` = CASE
WHEN `title` = 'My title' THEN 'My date 2'
WHEN `title` = 'Another title' THEN 'Another date 2'
ELSE `date` END
WHERE `title` IN ('My title','Another title')
Hope this helps.

Categories