Laravel save timestamp field to database - php

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?

Related

Insert data to different table in codeigniter

So I have 2 tables.
Table 1: (person_identity)
Id_person(PK), name, address, telp
Table 2: (Student)
Id_person(FK), no_student (PK), status, parent
How to input data to name, address, no_student, parent and status at the same time with the same Id_person but at 2 different table?
You can do like this
$dataPersonIdentity = ['name' => 'Abc', 'address' => 'This is address', 'telp' => 'this is telp'];
$this->db->insert('person_identity',$dataPersonIdentity);
$personIdentityId = $this->db->insert_id();
$dataStudent = ['Id_person' => $personIdentityId, 'no_student' => 8, 'status' => 1, 'parent' => 1];
do insert in the first table (person_identity)
and then do insert in the second table (student)

Store decimal number into MySQL database

Here is the return of AJAX, which display what value I am trying to store into database:
Here is what is really stored into database (58.00 is what I stored):
Here is the structure of table:
Here is the PHP:
$req = $bdd->prepare("INSERT INTO salon_histo(reference, designation, colour, size, type, price, qty, payment, date) VALUES(:reference, :designation, :colour, :size, :type, :price, :qty, :payment, NOW())");
$req->execute(array(
'reference' => $_POST['reference'],
'designation' => $_POST['designation'],
'colour' => $_POST['colour'],
'size' => $_POST['size'],
'type' => $_POST['type'],
'price' => floatval($price[0]),
'qty' => $_POST['soldQty'],
'payment' => $_POST['payment']
));
$req->closeCursor();
echo json_encode($price[0]);
How can MySQL store a data with 2 decimals at 0 when I am trying to store 58,33? I tried in PHP to use floatval and indeed the number becomes 58.
Price appears to be an array:
'price' => floatval($price[0]),
Possibly the decimal part has key 1?
'price' => (float) ($price[0] . '.' . $price[1]),
If that doesn't work, please var_dump($price) and paste me the result.
The other thing to check is wether PHP is formatting those numbers with a comma or not.

insert in table with Laravel with period (speckeld)

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.

ERROR: UNIQUE constraint failed....but not when I use a SELECT * Statement (SQLite)

I'm having a problem. I have a table in which I want to move its data into another table. When I do :
INSERT INTO rosters SELECT * FROM rosters_test
All of the rows enter into the database. The problem is that the columns are out of order for the new data entered (the new data has id where name should be etc), therefore making the data incorrect and useless.
I have tried to be more specific about the insert statement, but that is where the ERROR: UNIQUE constraint failed occurs. I have a dual key on the rosters table which is the id and the season. I am trying to insert the 20142015 roster from the rosters_test table into the rosters table where the 20152016 and 20162017 rosters reside.
Every row in the roster_test table is 20142015 for the season and I checked all 27 rows for repeat ids and there are none. These are the queries I tried:
INSERT INTO 'rosters'
SELECT 'number' AS 'number', 'name' AS 'name' , 'height' AS 'height', 'weight' AS 'weight', 'birthplace' AS 'birthplace', 'birthdate' AS 'birthdate', 'position' AS 'position', 'id' AS 'id', 'age' AS 'age', 'season' AS 'season', 'imageURL' AS 'imageURL'
FROM 'rosters_test'
INSERT INTO 'rosters' (number, name, height, weight, birthplace, birthdate ,position ,id ,age ,season, imageURL)
SELECT 'number', 'name' , 'height', 'weight', 'birthplace', 'birthdate', 'position', 'id', 'age' , 'season', 'imageURL'
FROM 'rosters_test'
I also tried things such as rosters_test.number AS rosters.number as well. Nothing is working: Here is the schema of the two tables:
CREATE TABLE 'rosters_test' ('number' INTEGER, 'name' TEXT, 'height' INTEGER, 'weight' INTEGER, 'birthplace' TEXT, 'birthdate' TEXT, 'position' TEXT, 'id' INTEGER, 'age' INTEGER, 'season' INTEGER, 'imageURL' TEXT)
I had a dual key on the rosters_test table at one point too.
CREATE TABLE 'rosters' ('position' TEXT, 'id' INTEGER, 'weight' INTEGER, 'height' TEXT, 'imageURL' TEXT, 'birthplace' TEXT, 'age' INTEGER, 'name' TEXT, 'birthdate' TEXT, 'number' INTEGER, 'season' INTEGER, PRIMARY KEY('id','season'))
Use INSERT INTO...SELECT and explicity specify the columns (and their order) which you want for the rosters table:
INSERT INTO rosters (position, id, weight, height, imageURL, birthplace, age, name,
birthdate, number, season)
SELECT position, id, weight, height, imageURL, birthplace, age, name, birthdate,
number, season
FROM rosters_test

mdb2 error with integer = NULL in prepare statement

I have an annoying problem with mdb2 while trying to insert empty values in an integer field.
$query = $conn->prepare("INSERT INTO enquiries
(type, idvenue, cname, fname, lname, phone, email, date, guests, budget, comments)
VALUES (:party, :venue, :cname, :fname, :lname, :phone, :email, :ddate, :guests, :budget, :options)",
array('integer', 'integer', 'text', 'text', 'text', 'text', 'text', 'datetime', 'integer', 'integer', 'text'),
MDB2_PREPARE_MANIP);
$result = $query->execute($_POST);
my $_POST array is:
[party] => 2
[venue] =>
[anyve] => checked
[cname] =>
[fname] => Javi
[lname] => Prieto
[phone] => 078087028492
[email] => ravenfewejp#gmfweail.com
[ddate] => 10/10/2011
[guests] => 10
[budget] => 15
[options] => My party!
in the moment that :venue is not empty, it works like a charm, even if :cname (text) is still empty, but when :venue is empty like this I get an undefined error. So I guess is happening only with integer fields, what am I missing?
try this before executing statement:
$_POST['venue'] = (int) $_POST['venue'];
I realised there's no problem with passing null values to integer fields in this kind of queries. The problem was I was sending one more element in $_POST when venue was empty, so the number of elements in $_POST was different than the number of placeholders.
Thanks for the help!

Categories