简体   繁体   中英

Wrong variable posts via mysql_query

I have a table gquestions with columns id , answer , date . I'm trying to update string in table with this HTML form and PHP queries:

HTML

<form action="/reply" method="post">
    <textarea name="answer"></textarea><br>
    <input type="submit" value="Reply">
</form>

PHP

$p_answer = $_POST["answer"];
$time = time();
mysql_query("UPDATE gquestions SET answer='$p_answer' AND date='$time' WHERE id='1'");
echo "UPDATE gquestions SET answer='$p_answer' AND date='$time' WHERE id='1'";

Okay, in form I'm typing in textarea Test and clicking Reply and get this result in page:

UPDATE gquestions SET answer='Test' AND date='1490982467' id='1'

but in table I get 0 instead of Test in answer column. Why I'm getting 0 in my column?

column type is different, maybe int change it to varchar or text for answer column.

your query is wrong

"UPDATE gquestions SET answer='$p_answer' AND date='$time' WHERE id='1'"

should be there shouldn't and

"UPDATE gquestions SET answer='$p_answer',date='$time' WHERE id='1'"

Change

UPDATE gquestions SET answer='$p_answer' AND date='$time' WHERE id='1'

To

UPDATE gquestions SET answer='$p_answer', date='$time' WHERE id='1'

update your code

UPDATE gquestions SET answer='Test' AND date='1490982467' id='1'

to

UPDATE gquestions SET answer='Test' , date='1490982467' id='1'

this is common format

  UPDATE table_name
  SET column1 = value1, column2 = value2, ...
  WHERE condition;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM