3

I have some difficulties with inserting date (type: datetime) into my database. Whatever I put there, it throws me syntax errors about the date format etc. Could someone help me please?

DB::insert('INSERT INTO hidden_authors (e_user = ? AND h_user = ? AND h_change_time = ?)', array($userBanId, $id, date(Y-m-d H:i:s) ));

I have already tried to use sql getdate() but it also gives me an error.

2
  • add quotes for date() Commented Oct 13, 2015 at 12:10
  • It looks like e_user.h_change_time should be a timestamp field in the database anyway - with ON UPDATE CURRENT_TIMESTAMP ... which would make this entire question moot. Commented Oct 13, 2015 at 12:21

3 Answers 3

6

Error Because of missing " inside date() function.

change this to

date(Y-m-d H:i:s)

this

date("Y-m-d H:i:s")

So final Well Form code should be

DB::insert('INSERT INTO hidden_authors (e_user = ? AND h_user = ? AND h_change_time = ?)',array($userBanId, $id, date("Y-m-d H:i:s")));

PHP date() Function

Tip For Your Code:

Without add exact function in query, assign that to variable and call it to your query

Example :

$date = date("Y-m-d H:i:s");

So inside Query

DB::insert('INSERT INTO hidden_authors (e_user = ? AND h_user = ? AND h_change_time = ?)',array($userBanId, $id, $date));

So when you read that insert function you can get clear idea about

  1. what you did?
  2. what you need to do?
  3. where is the error??

Its simply point all of these questions

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for a great answer. Unfortunately i still have a syntax error around h_change_time. Do you know why?
Try this (new) DB::insert('insert into hidden_authors (e_user, h_user,h_change_time ) values (?, ?, ? )', array($userBanId, $id, $date));
updated new insert use this DB::insert('insert into hidden_authors (e_user, h_user,h_change_time ) values (?, ?, ? )', array($userBanId, $id, $date));
3

you have missed quotes for your date(). This should help you.

DB::insert('INSERT INTO hidden_authors (e_user = ? , h_user = ? , h_change_time = ?)',
     array($userBanId, $id, date("Y-m-d H:i:s") ));
                                 ^           ^

7 Comments

Its always a good practice to use single quotes rather than double quotes where we have any PHP code in a string. PHP parses everything inside double quotes therefore takes more time to dun the same script comparing to the one having single quotes
i added double coz, he has single quotes in his query.
that wont bother. PHP will parse date function string separately
Thank you for a great answer. Unfortunately i still have a syntax error around h_change_time. Do you know why?
replace and my comma ,
|
1

mysql datetime NOW()

DB::insert('INSERT INTO hidden_authors (e_user = ? AND h_user = ? AND 
h_change_time = ?)', array($userBanId, $id, NOW() ));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.