wpdb insert with no ID - php

I'm new in WordPress
I'm trying to insert on database without knowing the id, here what I'm trying to do:
$create = $wpdb->insert('wp_ito_plan', array('name' => $_POST['name'], 'tickets' => $_POST['tickets'], 'price' => $_POST['price'], 'visits' => $_POST['visits']));
and I got this error:
WordPress database error: [Duplicate entry '0' for key 'PRIMARY']
INSERT INTO `wp_ito_plan` (`name`,`tickets`,`price`,`visits`) VALUES ('asd','123','123','123')
I tried adding to the array: 'id' => $wpdb->insert_id, but stills the same.
How can I fix this? Do I need to check what is the last ID on database and then increment? There's no easiest way?

It appears as though you aren't auto incrementing the ID column so you're insert is going to overwrite an existing row. Set the ID column to auto increment and it should work fine.

Related

Inserting new entries with Laravels upsert function with no key

I am trying to pass an array that has both existing and new rows I would like to insert into my QueueLane model by using laravel upsert function.
The code:
QueueLane::upsert([
['name' => 'Existing Lane Update', 'id' => 5],
['name' => 'A New Lane', 'id' => null]
], ['id'], ['name']);
You can see the first row has an id, so I want to update the name column with whatever value is in the array, the second row, however, doesn't have an id so I want this record to be created.
When running the code I get the following error:
null value in column "id" violates not-null constraint
I'm expecting the Laravel to populate the ID with a new auto incrementing integer as is the usual behavior for new records in the Model.

cant get the group of an int field from a query in cakephp3

I have a table with a field called 'year'. It has many repetitions in the column so I want to find the distinct group. I have 4 different 'years' in about 20 rows and I dont get these values from the query. Instead what is returned are 4 numbers which are not the years (5,14,4,70). The same code worked fine when I used this with suburb field in another table where there were multiple values of this field. I dont get why this isnt working.
//in view
echo $this->Form->input('year', ['label' => 'Year','options' => $allyears]);
//controller
$allyears = $this->TimesheetDates->find('list')
->select(['TimesheetDates.id', 'TimesheetDates.year'])
->group(['TimesheetDates.year'])->autoFields(true)
->order(['TimesheetDates.year'=> 'ASC'])
->hydrate(false);
$this->set('allyears',$allyears);
//another controller and this code worked fine
$suburb = $this->Students->find('list')->where(['Students.address_suburb !=' => '','Students.student_inactive' => 0])
->select(['Students.id','Students.address_suburb'])
->group(['Students.address_suburb'])->autoFields(true)
->order(['Students.address_suburb' => 'ASC'])
->hydrate(false);
take a look at the documentation about how find('list') works
$allyears = $this->TimesheetDates->find('list', [
'keyField' => 'id',
'valueField' => 'year']
)
->group(['year'])
->order(['year'=> 'ASC']);
Note that it has no meaning selecting the id of the TimesheetDates Table as you are grouping by year and the id is choosen randomly between all the records that share the same year

Update Duplicate Data when insert data in laravel

Hi I've search and try to solve the issue but failed to grab solution sorry for taking your time.
I am inserting a set of data in laravel with the query builder to insert multiple data at a time.
DB::table('table')->insert(
array(
array(
'col1' => 'data1',
'col2' => 'data1'
),
array(
'col1' => 'data2',
'col2' => 'data2'
),
)
);
Is there any way to check if exist the col1,col2 value in table if exist then update otherwise insert. I wanted to get return true or false from the query result if all data successfully update or inserted. I wanted to solve it with laravel query builder.
Thanks
As far as I know, the Laravel query builder does not support this.
You could do a raw MySQL query:
INSERT INTO table (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE c=c+1;
https://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html
But that is very messy and MySQL specific.
Instead i would suggest you use the Eloquent ORM:
// Retrieve the flight by the attributes, or create it if it doesn't exist...
$flight = App\Flight::firstOrCreate(['name' => 'Flight 10']);
http://laravel.com/docs/5.1/eloquent#inserting-and-updating-models

inserting data into postgresql database with codeigniter

i have a problem when i try to insert a row in my user table in postgresql, e.g.
I have the normal insert with active record, $this->db->insert($table,$data) and i get an error something like this:
ERROR: it does not exists the column «user_accounts» in the relation «user_accounts» LINE 1: INSERT INTO "user_accounts" ("user_accounts"."uacc_group_fk"
and i think the problem could be the table name inside the fields that i will insert ("user_accounts"."uacc_group_fk"),.
Do you know how can I avoid this? i just want to put the name of fields without table name.
Thanks in advance! =)
Have you checked that the column "user_accounts"."uacc_group_fk" exists ( with quotes )?
this is the data array used in insert_user fuction of https://github.com/haseydesign/flexi-auth/blob/master/library_files/application/models/flexi_auth_model.php:
$sql_insert = array(
$this->auth->tbl_col_user_account['group_id'] => $group_id,
$this->auth->tbl_col_user_account['email'] => $email,
$this->auth->tbl_col_user_account['username'] => ($username) ? $username : '',
$this->auth->tbl_col_user_account['password'] => $hash_password,
$this->auth->tbl_col_user_account['ip_address'] => $ip_address,
$this->auth->tbl_col_user_account['last_login_date'] => $this->database_date_time(),
$this->auth->tbl_col_user_account['date_added'] => $this->database_date_time(),
$this->auth->tbl_col_user_account['activation_token'] => $activation_token,
$this->auth->tbl_col_user_account['active'] => 0,
$this->auth->tbl_col_user_account['suspend'] => $suspend_account
);

how to use insert_batch with unique ids

I am using codeignitor's insert batch function to insert multiple rows to a table.
$this->db->insert_batch('table', $sizes);
my $sizes array looks like this
$sizes = array(
array(
'size' => 'M' ,
'product' => 'Hat'
),
array(
'size' => 'L' ,
'product' => 'Hat'
)
);
I was intending for separate rows to be added to my db table with these values along with unique ids, however when each nested array is added, It adds 0 to the id field, rather than a unique ID
I am receiving the error "Duplicate entry '0' for key 'id'"
What is the best approach to solve this? Thanks for reading!
Make your UNIQUE Column with AUTO_INCREMENT

Categories