user2372469
user2372469

Reputation:

Date is not inserting in mysql

Date is not inserting in login_date field in database. It's showing the below error when i run the sql query in phpmyadmin

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''user_id','login_date','login_time','logout_date','logout_time')values('12','201' at line 1

Thanks in advance.

Code:

$date=date("d/m/y", time());
$time=date("H:i:s",time());
$q = $_POST['id'];

$sql1="insert into entries('user_id','login_date','login_time','logout_date','logout_time')values('$q','$date','$time','$date','$time')";
$res1=mysql_query($sql1);

Upvotes: 0

Views: 114

Answers (7)

munsifali
munsifali

Reputation: 1732

your values are wrap with single quotes. Use backticks.

$res1 = mysql_query("INSERT INTO entries (`user_id`, `login_date`, `login_time`, `logout_date`, `logout_time`) 
             VALUES (
               '".mysql_real_escape_string($q)."',
               '".mysql_real_escape_string($date)."', 
               '".mysql_real_escape_string($time)."')");

$res1 = mysql_query($sql1);

Upvotes: -1

Ignis
Ignis

Reputation: 126

The date format of mysql is Y-m-d so if the login_date is of type date then use :-

$date=date("Y-m-d") or $date=date("Y-m-d H:i:s") for type datetime

Upvotes: 0

fancyPants
fancyPants

Reputation: 51868

With ' MySQL thinks, it's a string, but you want to name a column. Either you simply remove those single-quotes or you use backticks.

Like this:

$date=date("d/m/y", time());
$time=date("H:i:s",time());
$q = $_POST['id'];

$sql1="insert into entries(`user_id`, `login_date`, `login_time`, `logout_date`, `logout_time`)values('{$q}','{$date}','{$time}','{$date}','{$time}')";
$res1=mysql_query($sql1);

Upvotes: 2

WordpressCoder
WordpressCoder

Reputation: 303

Replace your line with this

$sql1="insert into entries(user_id,login_date,login_time,logout_date,logout_time) values('$q','$date','$time','$date','$time')";

Don't user single quotes for column names.

Upvotes: 0

bnx
bnx

Reputation: 427

Your database doesn't like column names in single quotes, try using

`user_id`

instead of

'user_id'

Upvotes: 0

Sverri M. Olsen
Sverri M. Olsen

Reputation: 13263

It is because you are using single quotes, ', around your column names. Column names are not strings.

You should either not use anything or, preferably, use backticks, `, like so:

$sql1 = <<< SQL
    INSERT INTO `entries` (
        `user_id`,
        `login_date`,
        `login_time`,
        `logout_date`,
        `logout_time`
    ) VALUES (
        '{$q}',
        '{$date}',
        '{$time}',
        '{$date}',
        '{$time}'
    )
SQL;

Upvotes: 2

danhardman
danhardman

Reputation: 621

As I assume your user_id column is an Integer, you should take the $q variable out of quotes. You only need to put strings in quotes for some reason.

Upvotes: 0

Related Questions