MySQL语法错误:第1行附近的“1”

时间:2014-07-09 08:12:54

标签: mysql

我收到错误' 1'在第1行,我有点难过这个;我是一个完整的菜鸟,通常会在没有求助的情况下尝试纠正错误

它将所有数据正确输入到数据库中,但提供了此错误。

还有一些其他线程有同样的问题,一个解决方案是: "这就是错误出现的原因:您尝试使用上次更新查询的结果调用mysql_query。当TRUE转换为String时,只是' 1'。"

$title = mysqli_real_escape_string($con, $_POST['title']);
$firstname = mysqli_real_escape_string($con, $_POST['firstname']);
$lastname = mysqli_real_escape_string($con, $_POST['lastname']);
$jobtitle = mysqli_real_escape_string($con, $_POST['jobtitle']);
$address = mysqli_real_escape_string($con, $_POST['address']);
$address2 = mysqli_real_escape_string($con, $_POST['address2']);
$address3 = mysqli_real_escape_string($con, $_POST['address3']);
$postcode = mysqli_real_escape_string($con, $_POST['postcode']);
$telephone = mysqli_real_escape_string($con, $_POST['telephone']);
$email = mysqli_real_escape_string($con, $_POST['email']);

(我知道我应该做一个准备好的陈述)

$sql = mysqli_query($con, "UPDATE users SET title='$title', firstname='$firstname', lastname='$lastname', jobtitle='$jobtitle', address='$address', address2='$address2', address3='$address3', postcode='$postcode', telephone='$telephone', email='$email' WHERE username='$user'");

if (!mysqli_query($con,$sql)) {
$result_array = mysql_fetch_assoc($qStuff);
die('Error: ' . mysqli_error($con));
}

1 个答案:

答案 0 :(得分:1)

以下行存储值" 1"在插入成功时在$ sql中,在失败时为0。

$sql = mysqli_query($con, "UPDATE users SET title='$title', firstname='$firstname', lastname='$lastname', jobtitle='$jobtitle', address='$address', address2='$address2', address3='$address3', postcode='$postcode', telephone='$telephone', email='$email' WHERE username='$user'");

你正试图执行这个

if (!mysqli_query($con,$sql)) 

这里,$ sql的值是" 1"这不是有效的查询。这导致语法错误。 可能如果你想要达到这个目的:

$sql = "UPDATE users SET title='$title', firstname='$firstname', lastname='$lastname', jobtitle='$jobtitle', address='$address', address2='$address2', address3='$address3', postcode='$postcode', telephone='$telephone', email='$email' WHERE username='$user'";
if(mysqli_query($con,$sql)){
    //do something if the operation is successful
}