我正在努力使这些代码行起作用,但没有成功......如果有人可以指出编写此类查询的最佳做法会不会很好?
if(isset($_POST) && $_POST["id"] > 0)
{
include('../../config.php');
$id = $_POST["id"];
$title = mysql_real_escape_string($_POST["title"]);
$desc = mysql_real_escape_string($_POST["desc"]);
$cat = $_POST["catid"];
$_DB->Execute("UPDATE gallery_imgs SET title = `$title`, description = `$desc` WHERE id = $id");
header("location: admin.php?mode=images&id=$cat");
}
else
{
//Other stuff!
}
这是我得到的错误:
Error number: 1054
Error : Unknown column 'The SIEK!' in 'field list'
答案 0 :(得分:3)
那些不是你在那里使用的单引号。刻度线`用于列名。
答案 1 :(得分:2)
MySQL使用包裹字符串的单引号。像这样:'。
$_DB->Execute("UPDATE gallery_imgs SET title = '$title', description = '$desc' WHERE id = $id");
您还应该考虑验证$ id。 (使用is_numeric()或强制转换为int)。
提示:要获得更健壮,更安全的MySQL解决方案,您应该查看PHP PDO。
答案 2 :(得分:1)
首先,您已对字符串值调用mysql_real_escape_string()
,但您还必须验证$_POST['id']
的内容。
// invalid string values will convert to integer 0
// If that is not allowable, you should not proceed with the query.
$id = intval($_POST['id']);
输入字符串必须用单引号括起来,而不是反引号(用于列和表标识符);
$_DB->Execute("UPDATE gallery_imgs SET title = '$title', description = '$desc' WHERE id = $id");
除此之外,您应该在重定向标头中使用之前验证$_POST['catid']
的内容。例如,如果它应该是数字:
// At least cast it to an int if it is an invalid string....
$catid = intval($_POST['catid']);