【发布时间】:2015-11-17 06:36:31
【问题描述】:
我编写了简单的 PHP 代码来从我的 iphone 应用程序中注册用户详细信息。它工作正常并返回 JSON 输出。我在下面添加了该代码
header('Content-type: application/json');
include 'connection.php';
$response = array();
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
if($username == NULL || $password == NULL || $email == NULL ){
$response["success"] = 0;
$response["message"] = "Something Empty";
}else{
$sql = "INSERT INTO User (username, password, email)
VALUES ('".$username."', '".$password."', '".$email."')";
if ($conn->query($sql) === TRUE) {
$response["success"] = 1;
$response["message"] = "Done";
} else {
$response["success"] = 0;
$response["message"] = "Error";
}
}
echo json_encode($response);
$conn->close();
但是当我尝试在添加到表之前检查用户名已经存在时。我从我的 Xcode 日志中收到错误消息。
JSON text did not start with array or object and option to allow fragments not set.
if($username == NULL || $password == NULL || $email == NULL ){
$response["success"] = 0;
$response["message"] = "Something Empty";
}else{
$query = mysql_query("SELECT * FROM User WHERE username='".$username."'");
if (mysql_num_rows($query) != 0)
{
$response["success"] = 0;
$response["message"] = "Username Already Exists";
}else{
$response["success"] = 1;
$response["message"] = "That Name Fine";
}
}
【问题讨论】:
标签: php ios json xcode afnetworking