insert in table with Laravel with period (speckeld) - php

Good morning everybody.
I have a problem that I could not solve
I'm trying to save a record in the database with Laravel, the problem is that one of the fields has a point in its name.
This is the structure:
struct table
I tried to do this:
$emqu_accountavg = DB::table('emqu_accountavg_resptime')->insert([
'company_id' => $company_id,
'#Month' => $item['#Month'],
'System' => $item['System'],
'APPLID' => $item['APPLID'],
'GUI/NoGUI' => $item['GUI/NoGUI'],
'Resptime avg. (ms)' => $item['Resptime avg. (ms)'],
]);
But this is the error I get:
QueryException in Connection.php line 647:SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Resptime avg. (ms)' in 'field list' (SQL: insert into `emqu_accountavg_resptime` (`company_id`, `#Month`, `System`, `APPLID`, `GUI/NoGUI`, `Resptime avg`.` (ms)`) values (1, 12017, BWP, APPL358552, GUI, 1581,4))
I also tried to do it this way:
emqu_accountavg_resptime::create([
'company_id' => $company_id,
'#Month' => $item['#Month'],
'System' => $item['System'],
'APPLID' => $item['APPLID'],
'GUI/NoGUI' => $item['GUI/NoGUI'],
'Resptime avg. (ms)' => $item['Resptime avg. (ms)'],
]);
No error occurs and the records are saved, but the one that has point keeps it null.
I check this value:
$item['Resptime avg. (ms)']
And okay, the problem is the name of that field with speckeld (point) in the database

You can see on your first error that the column laravel tries to insert to is `Resptime avg`.` (ms)` It should be `Resptime avg. (ms)`.
You can find how to properly escape the value here:
Updating a MySQL column that contains dot (.) in its name

I got the answer, without specifying the names of the fields of the table, but respecting the order of the same as it is stored, thus:
DB::insert('insert into emqu_accountavg_resptime values (?, ?, ?, ?, ?, ?)', [$company_id, $item['#Month'],$item['System'],$item['APPLID'],$item['GUI/NoGUI'],$item['Resptime avg. (ms)']]);
Thanks everyone for your help.

Related

Laravel save timestamp field to database

I have timestamp field
$table->timestamp('age')->nullable()->default(null);
I need save is field is value to database.
$dateOfBirth = Carbon::now()->subYears(intval($row['age']));
return Customer::updateOrCreate(['id' => $row['id']],['id' => $row['id'],
'name' => $row['name'],
'age' =>$dateOfBirth->getTimestamp(),
'email' => $row['email']]);
But got error
Incorrect datetime value: '871238720' for column 'age' at row 1 (SQL: insert into customers (id, name, age, email, updated_at, created_at) values (1, Annika Kane, 871238720, malattia#hotmail.com, 2022-08-10 18:45:20, 2022-08-10 18:45:20))
How fix it?

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.

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
);

wpdb insert with no ID

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.

Categories