[英]Can't Update MySQL table
I don't know what I'm doing wrong, but my little update code is giving me an error message that I can't work out how to resolve it. 我不知道自己在做什么错,但是我的小更新代码给我一个错误消息,提示我无法解决该问题。
Here's my code: 这是我的代码:
<?php
include('dbconfig.php');
$con = mysql_connect($host, $username, $password) or die(mysql_error()) ;
if (!$con){
die('Could not connect: ' . mysql_error());
}
mysql_select_db($db, $con);
function sqlEscape($string){
return "'".mysql_real_escape_string($string)."'";
}
if(isset($_POST['submit'])){
$q = "UPDATE records SET `name` = " + sqlEscape($_POST['name']) + ",
`age` = " + sqlEscape($_POST['age']) + ",
`location` = " + sqlEscape($_POST['location']) + ",
`telephone` = " + sqlEscape($_POST['telephone']) + "
WHERE id = $_POST[id]";
mysql_query($q) or die(mysql_error());
}
?>
Here's the error message it prints out: 这是它打印出来的错误消息:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '0' at line 1
Can someone see where I'm going wrong at all? 有人可以看到我哪里出问题了吗?
Thanks for your help. 谢谢你的帮助。
You are adding strings together with the +
operator, which is for adding numbers. 您正在将字符串与
+
运算符一起添加,用于添加数字。 In PHP, strings are concatenated with the .
在PHP中,字符串与串联
.
(period) operator. (句点)运算符。
$q = "UPDATE records SET `name` = " . sqlEscape(...) . ",
etc 等等
$q = "UPDATE records SET `name` = " . sqlEscape($_POST['name']) . ",
`age` = " . sqlEscape($_POST['age']) . ",
`location` = " . sqlEscape($_POST['location']) . ",
`telephone` = " . sqlEscape($_POST['telephone']) . "
WHERE id = $_POST[id]";
Use "." 采用 ”。” instead of "+" to concat strings in PHP.
而不是“ +”来连接PHP中的字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.