我试图检查数据库中是否已存在用户ID。但由于某种原因,它不起作用。我尝试了不同的解决方案,但都失败了。
db_connect.php工作正常,我仔细检查了一下!
create_user.php
<?php
$response = array();
if (isset($_POST['user_id'])){
$id = $_POST['user_id'];
$fullName = $_POST['user_fullName'];
$firstName = $_POST['user_firstName'];
$lastName = $_POST['user_lastName'];
$birthday = $_POST['user_birthday'];
$friends = $_POST['user_friends'];
$totalFriends = $_POST['user_totalFriends'];
$email = $_POST['user_email'];
$gender = $_POST['user_gender'];
$likes = $_POST['user_likes'];
$events = $_POST['user_events'];
$hometown = $_POST['user_hometown'];
require_once __DIR__ . '/db_connect.php';
$db = new DB_CONNECT();
$query = mysql_query("SELECT * FROM 'userData' WHERE 'id' = '$id'");
$user_data = mysql_num_rows($query);
if(empty($user_data)) {
$result = mysql_query("INSERT INTO userData(id, fullName, firstName, lastName, birthday, friends, totalFriends, email, gender, likes, events, hometown) VALUES ('$id', '$fullName', '$firstName', '$lastName', '$birthday', '$friends', '$totalFriends', '$email', '$gender', '$likes', '$events', '$hometown')");
if($result){
$response["success"] = 1;
$response["message"] = "User successfully created";
echo json_encode($response);
}
else{
$response["success"] = 0;
$response["message"] = "Error occurred";
echo json_encode($response);
}
}
else {
$response["success"] = 1;
$response["message"] = "User already in database";
echo json_encode($response);
}
}
else{
$response["success"] = 0;
$response["message"] = "Required userdata is missing";
echo json_encode($response);
}
&GT;
好。如果我在浏览器中运行此脚本,它可以正常工作。当我在我的Android应用程序中运行它时,它总是创建一个新用户。
谢谢! Ť
答案 0 :(得分:1)
您在表名和字段周围添加了单引号。
将其更改为后退。
$query = mysql_query("SELECT * FROM 'userData' WHERE 'id' = '$id'");
更新至:
$query = mysql_query("SELECT * FROM `userData` WHERE `id` = '$id'");
另外,请不要使用mysql_ *函数,因为出于安全原因这些函数已被弃用,并且将在以后的版本中删除。
单引号用于用户输入。
关键字/数据库名称/表名称/字段名称可以用后面的刻度括起来。