-3

I want to set a record in mysql table. It goes like this:

mysql_query("UPDATE posts SET creatdate = $timestring WHERE postid = 1 ");

I get $timestring from anther record in this table:

while($row = mysql_fetch_array($table)){

    $timestring = $row['creatdate'];

and

echo $timestring;

get

2015-07-30 18:32:18

why can't I set this record

Henry
  • 25
  • 1
  • 2
  • 7
  • 1
    put single quotes around $timestring – amdixon Jul 30 '15 at 10:52
  • In mysql, if your variable is a string, you have to put quote. So do as fallow : `mysql_query("UPDATE posts SET creatdate = '$timestring' WHERE postid = 1 ");` – Hearner Jul 30 '15 at 10:55
  • 2
    Please _Stop using the **deprecated** `mysql` extension_. Read [***the red warning box*** at the top](http://php.net/mysql_connect). Learn about injection, prepared statements and learn to use either `PDO` or `mysqli` (the `i` of which stands for _improved_). and yes, amdixon is right: quote the string, something you could've found out by checking the return value of `mysql_error`... debugging is as much a part of programming as writing code is – Elias Van Ootegem Jul 30 '15 at 10:55
  • It works, thank you dude! @amdixon – Henry Jul 30 '15 at 10:59

2 Answers2

1

You need to place quotes around your date i.e. $timestring as

"UPDATE posts SET creatdate = '$timestring' WHERE postid = 1 "
Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54
0

You have to add quotes around your date $timestring.

Try

mysql_query("UPDATE posts SET creatdate = '$timestring' WHERE postid = 1 ");

You can also use mysql built in function NOW()

mysql_query("UPDATE posts SET creatdate = NOW() WHERE postid = 1 ");
Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50