[英]JQuery Post to PHP Inserts data, but json response is null
I'm building a simple sign up form with AngularJS and sending the data to a PHP page using JQuery's $.post(). 我正在用AngularJS构建一个简单的注册表单,然后使用JQuery的$ .post()将数据发送到PHP页面。 When I send the data, it correctly gets inserted into the database . 当我发送数据时, 它正确地插入了数据库 。 However, the returned json that I am logging is showing my data fields as null. 但是,我正在记录的返回的json将我的数据字段显示为null。
Console: 安慰:
{"status":"success","email":null,"id":null,"sessionId":null}
Javascript: 使用Javascript:
$.post("admin/addUser.php", {
email: form.email,
password: form.password
}).done(function(data){
console.log(data);
});
PHP: PHP:
$email = mysql_real_escape_string($_POST["email"]);
$password = md5(mysql_real_escape_string($_POST["password"]));
$sessionId = md5(uniqid(microtime()) . $_SERVER['REMOTE_ADDR'] . $_SERVER['HTTP_USER_AGENT']);
//Add this user to the database
$sql = mysql_query("INSERT INTO users (email, password, sessionId) VALUES ('".$email."', '".$password."', '".$sessionId."')");
if ($sql){
//Now find the user we just added
$getUser = mysql_query("SELECT * FROM users WHERE email = '".$email."' AND sessionId = '".$sessionId."'");
if ($getUser){
$row = mysql_fetch_array($getUser);
$user = array(
'status' => 'success',
'email' => $row['email'],
'id' => $row['id'],
'sessionId' => $row['sessionId']
);
echo json_encode($user);
}else{
$user = array(
'error' => mysql_error()
);
echo json_encode($user);
}
}else{
$user = array(
'error' => mysql_error()
);
echo json_encode($user);
}
Are you sure that you have only one record in here 您确定这里只有一条记录吗
$getUser = mysql_query("SELECT * FROM users WHERE email = '".$email."' AND sessionId = '".$sessionId."'");
Try to dump $row and see the response. 尝试转储$ row并查看响应。 BTW I would suggest you to add limit 顺便说一句,我建议你增加限额
$getUser = mysql_query("SELECT * FROM users WHERE email = '".$email."' AND sessionId = '".$sessionId."' LIMIT 1");
Ok, I dug around and found the answer to this. 好的,我挖了一下,找到了答案。 It was a mistake on my end. 这是我的错误。 I was only storing the password and sessionId as varchar(30). 我只将密码和sessionId存储为varchar(30)。 When I was generating the sessionId and checking it against the DB, it was being cut off when it was stored, since I was only allowing 30 chars. 当我生成sessionId并将其与数据库进行检查时,由于我只允许30个字符,因此在存储它时被切断了。 I update to 255 and works as expected :-P. 我更新到255,并按预期工作:-P。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.