46

In my MySQL database, I have a table with structure

username - varchar
insert_time - timestamp

This table was created in MySQL using the phpMyAdmin tool and for the insert_time column, I have mentioned default value as 0000-00-00 00:00:00.

Now the problem is, I have to update this default value with the current timestamp later on, using a PHP script.

I tried doing the following PHP code:

$update_query = 'UPDATE db.tablename SET insert_time=now() '.
                'WHERE username='.$somename;

When the PHP script is run, it fails, and is unable to insert anything into the database.

What am I doing wrong?

1
  • i've set a custom error to be displayed if the insert query fails (using a "or die()" in the query) Commented May 20, 2011 at 18:08

6 Answers 6

57

What error message are you getting?

I'd guess your actual error is because your php variable isn't wrapped in quotes. Try this

$update_query = "UPDATE db.tablename SET insert_time=now() WHERE username='" .$somename . "'"; 
Sign up to request clarification or add additional context in comments.

6 Comments

yes! that was the issue. guess i should sleep now. its late night here, IST. else these issues will creep all over the code!!
NOW() is redundant, use UNIX_TIMESTAMP()
@NitinBansal, can you provide a link the MySQL where it says is deprecated? I can't find any evidence to your claim.
@AshBurlaczenko He didn't say it was deprecated. He said it was redundant.
@LeeFuller, redundant means something will work without the other thing. NitinBansal is suggesting replacing now() not removing it so I assumed they'd got there words mixed up.
|
20

This format is used to get current timestamp and stored in mysql

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

$update_query = "UPDATE db.tablename SET insert_time=".$date." WHERE username='" .$somename . "'"; 

Comments

15

Your usage of now() is correct. However, you need to use one type of quotes around the entire query and another around the values.

You can modify your query to use double quotes at the beginning and end, and single quotes around $somename:

$update_query = "UPDATE db.tablename SET insert_time=now() WHERE username='$somename'";

3 Comments

This is the solution, but you should ALWAYS escape variables before putting them into your database queries. Check out PDO php.net/pdo as a database abstraction layer which can help with this, or at least run mysql_real_escape_string($somename) before inserting it.
@mjec: You are correct, but I left it out to avoid clutter. The OP may be escaping the input before the line in question, or this may not be user input at all.
better: UNIX_TIMESTAMP()
10

Forgot to put the variable in the sql statement without quotations.

 $update_query = 
      "UPDATE db.tablename SET insert_time=NOW() WHERE username='" .$somename."'";

Comments

1

Don't like any of those solutions.

this is how i do it:

$update_query = "UPDATE db.tablename SET insert_time=now() WHERE username='" 
            . sqlEsc($somename) . "' ;";

then i use my own sqlEsc function:

function sqlEsc($val) 
{
    global $mysqli;
    return mysqli_real_escape_string($mysqli, $val);
}

Comments

1

Instead of NOW() you can use UNIX_TIMESTAMP() also:

$update_query = "UPDATE db.tablename 
                 SET insert_time=UNIX_TIMESTAMP()
                 WHERE username='$somename'";

Difference between UNIX_TIMESTAMP and NOW() in MySQL

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.