我想使用 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、nullnullnull。它被写入检查您的版本的语法,我正在本地主机上运行 5.5.12 php 版本和 5.6.17 mysql 版本。我已经检查了语法,不同的网站显示不同的语法,如何获得正确的语法,下面是我的 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":""}}
最佳答案
各处遗漏了一些空格和 qutoes(')。将您的 SQL 语句更改为此
$sql ="UPDATE users" . " SET Question1 = '".$Question1."', Question3 = '".$Question3."', Question4= '".$Question4."' WHERE email = '".$email."'";
此外,这些变量中的值似乎不正确,看看为什么您没有获得正确的值(或者是您传递的值?)
Question1 = null2null
Question3 = nullnull
Question4 = nullnullnullnull
email = bb
关于java - 使用php更新mysql中的数据时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35357428/