使用php更新mysql中的数据时出错

时间:2016-02-12 07:56:45

标签: java php android mysql

我想使用php更新数据库中的数据,这是在logcat中显示的错误

Error: UPDATE usersSET Question1=null2null,Question3=nullnull,Question4=nullnullnullnullWHERE email=bb<br>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 '=null2null,Question3=nullnull,Question4=nullnullnullnullWHERE email=bb' at line 1{"error":false,"uid":"56bd5f88afb7b3.99372648","user":{"name":"Bb","email":"bb","created_at":"2016-02-12 09:58:56","updated_at":null,"Question1":"","Question3":"","Question4":""}

Question1,Question3和Question4列的值没有得到更新,它应分别为null2null,nullnull,nullnullnullnull。它写的是检查你的版本的语法,我在localhost上运行5.5.12 php版本和5.6.17 mysql版本。我检查了语法和不同的网站显示不同的显示不同的syntasx如何获得正确的一个,下面是我的PHP代码

PHP

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "android_api";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
require_once 'include/DB_Functions.php';
$db = new DB_Functions();

// json response array
$response = array("error" => FALSE);

if (isset($_POST['Question1']) && isset($_POST['Question3']) && isset($_POST['Question4'])) {

    // receiving the post params
    $email = $_POST['email'];
    $password = $_POST['password'];

     $Question1 = $_POST['Question1'];
    $Question3 = $_POST['Question3'];
    $Question4 = $_POST['Question4'];
 /*$sql = "INSERT INTO users (Question1, Question2, Question4)
    VALUES ('$Question1', '$Question3', '$Question4')"; */
    $user = $db->getUserByEmailAndPassword($email, $password);
   // $result = mysql_query("UPDATE users"."SET Question1='$Question1',Question3='$Question3',Question4='$Question4'"."WHERE email=$email";
    $sql="UPDATE users"."SET Question1=$Question1,Question3=$Question3,Question4=$Question4"."WHERE email=$email";
    if ($conn->query($sql) === TRUE) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
    // get the user by email and password
    $user = $db->getUserByEmailAndPassword($email, $password);

    if ($user != false) {
        // use is found
        $response["error"] = FALSE;
        $response["uid"] = $user["unique_id"];
        $response["user"]["name"] = $user["name"];
        $response["user"]["email"] = $user["email"];
        $response["user"]["created_at"] = $user["created_at"];
        $response["user"]["updated_at"] = $user["updated_at"];
         $response["user"]["Question1"] = $user["Question1"];
          $response["user"]["Question3"] = $user["Question3"];
           $response["user"]["Question4"] = $user["Question4"];
        echo json_encode($response);
    } else {
        // user is not found with the credentials
        $response["error"] = TRUE;
        $response["error_msg"] = "ABCD";
        echo json_encode($response);
    }
} else {
    // required post params is missing
    $response["error"] = TRUE;
    $response["error_msg"] = "abcd";
    echo json_encode($response);
}
?>

修改

阅读答案并更改语法后,错误将更改为

 Error: UPDATE users SET Question1=null2null,Question3=nullnull,Question4=nullnullnullnull WHERE email=bb<br>Unknown column 'bb' in 'where clause'{"error":false,"uid":"56bd5f88afb7b3.99372648","user":{"name":"Bb","email":"bb","created_at":"2016-02-12 09:58:56","updated_at":null,"Question1":"","Question3":"","Question4":""}}

3 个答案:

答案 0 :(得分:1)

在这里和那里错过了几个白色空间和qutoes(')。将您的SQL语句更改为此

 $sql ="UPDATE users" . " SET Question1 = '".$Question1."', Question3 = '".$Question3."', Question4= '".$Question4."' WHERE email = '".$email."'";

此外,这些变量中的值似乎不正确,看看为什么你没有得到正确的值(或者你正在传递的是什么?)

Question1 = null2null
Question3 = nullnull
Question4 = nullnullnullnull
email = bb

答案 1 :(得分:1)

这里你错过了三件事:
 1.用户之后的空间SET  2.价值分配报价(Question1 = null2null)
 3. WHERE关键字前的空格(Question4 = nullnullnullnullWHERE)
 您的查询的正确版本如下:

 UPDATE users SET Question1='null2null', Question3='nullnull' ,
    Question4='nullnullnullnull' WHERE email='b'

所以在脚本中替换下面的行:

$sql    ="UPDATE users"."SET Question1=$Question1,Question3=$Question3,Question4=$Question4"."WHERE email=$email";


$sql    =" UPDATE users SET Question1 = '".$Question1."', Question3 = '".$Question3."', Question4= '".$Question4."' WHERE email = '".$email."'";

答案 2 :(得分:0)

您好需要正确保存问题的详细信息,因为数据包含null作为字符串。

您需要准备如下查询:

Question1 = null2null

在保存到数据库之前,您必须引用所有参数。像这样

Question1 = 'null2null'

以下是供您参考的示例更新代码,养成在使用适当的引用字符串创建时清理查询的习惯,以提高可读性。

$email = $_POST['email'];
$password = $_POST['password'];

$Question1 = $_POST['Question1'];
$Question3 = $_POST['Question3'];
$Question4 = $_POST['Question4'];

$user = $db->getUserByEmailAndPassword($email, $password);

$sql="UPDATE users SET Question1 = '$Question1', Question3 = '$Question3', Question4 = '$Question4' WHERE email = '$email'";
if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
  echo "Error: " . $sql . "<br>" . $conn->error;
}