您的查询是单行,但错误在第 2 行
您的代码不安全,您没有清理值,错误来自这一事实。mysql_*
此类错误现在已弃用函数。
您的代码也有一个逻辑问题:如果用户向服务器提交信息,您希望处理更改,但是您的条件if (!isset($_POST['submit']))
说如果未设置,因此会在不应该更新信息时尝试更新信息。
使用 PDO(假设连接成功)
include 'includes/connection.php';
if (isset($_POST['submit'])) {
$statement = $db->prepare("UPDATE student SET `name`=:name , `email`=:email , `dob`=:dob , `phone`=:phone , `college`=:college , `address`=:address , `state`=:state , `country`=:country WHERE id = :id");
$statement->execute(array(
':name' => $_POST['name'],
':email' => $_POST['email'],
':dob' => $_POST['dob'],
':phone' => $_POST['phone'],
':college' => $_POST['college'],
':address' => $_POST['address'],
':state' => $_POST['state'],
':country' => $_POST['country'],
':id' => $_POST['id']
));
}
使用mysql_*
函数
include 'includes/connection.php';
if (isset($_POST['submit'])) {
$name = mysql_real_escape_string($_POST['name']);
$email = mysql_real_escape_string($_POST['email']);
$dob = mysql_real_escape_string($_POST['dob']);
$phone = mysql_real_escape_string($_POST['phone']);
$college = mysql_real_escape_string($_POST['college']);
$address = mysql_real_escape_string($_POST['address']);
$state = mysql_real_escape_string($_POST['state']);
$country = mysql_real_escape_string($_POST['country']);
$id = mysql_real_escape_string($_POST['id']);
/**
* For debugging purposes
*/
$query = "UPDATE student SET `name`='$name' , `email`='$email' , `dob`='$dob' , `phone`='$phone' , `college`='$college' , `address`='$address' , `state`='$state' , `country`='$country' WHERE id = '$id'";
mysql_query($query) or die(mysql_error());
/**
* For debugging purposes
*/
echo "<pre>Last query: $query</pre>";
}
请注意,$id
应该:
mysql_real_escape_string
在查询中使用引号进行转义
- 或转换为 int
intval
(假设是数字索引)