PHP更新sql记录

时间:2012-10-31 17:13:08

标签: php html sql

在尝试更新我的sql db中的记录时,我得到了sql错误

这是我当前的代码

html表格:

<html>
<head>
<title>title here</title>
</head>
<body>
<form action="end.php" method="POST">
<input type="text" id="comment" name="comment" placeholder="Kommentar"><p>
<input type="submit" value="Stop arbejde">
</form>
</body>
</html>

end.php

<?php
$host="localhost";
$username="root";
$password="password";
$db_name="db";
$tbl_name="log";

mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$comment=$_POST['comment'];
$datetime=date("y/m/d H:i:s");
$editit=date("W/D");

$sql="UPDATE log SET end=$datetime, comment=$comment WHERE editid='$editit'";
$result=mysql_query($sql);

if($result){
echo "Successful<BR>";
}
else {
echo "Fejl";
}
mysql_close();
?>

因为我收到错误“Fejl”,我做错了什么?

2 个答案:

答案 0 :(得分:7)

字符串必须用单引号包装

$sql="UPDATE log SET end='$datetime', comment='$comment' WHERE editid='$editit'";

但您的查询很容易SQL Injection。请花时间阅读下面的文章

答案 1 :(得分:0)

$ sql中的变量名应该在单引号中。

$sql="UPDATE log SET end='$datetime', comment='$comment' WHERE editid='$editit'";

此外,您应该从表单标记中的html代码中删除段落标记。