[英]Getting null value in my json response.
我有以下注册php脚本。 我想以{“ result”:“ success”,“ message”:“ 123”}的形式返回json响应
其中123是注册用户的ID。 我需要此ID,以便以后用户可以将数据发布到其他表。
但是我明白了。
{"result":"fail","message":null}
这是我的剧本。
<?php
session_start();
require "init.php";
header('Content-type: application/json');
$id = $_POST['id'];
$email = $_POST['email'];
$user_name = $_POST['user_name'];
$user_pass = $_POST['user_pass'];
$passwordEncrypted = sha1($user_pass);
$confirmPass = $_POST['confirm_pass'];
$confPasswordEncrypted = sha1($confirmPass);
$msg = "Congratulations. You are now registered to the most amazing app
ever!";
if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
$don = array('result' =>"fail","message"=>"Please enter a valid email");
}
if($email && $user_name && $user_pass && $confirmPass && filter_var($email, FILTER_VALIDATE_EMAIL)){
$sql_query = "select * from user_info WHERE email ='".mysqli_real_escape_string($con, $email)."' or user_name
='".mysqli_real_escape_string($con, $user_name)."'";
$result = mysqli_query($con, $sql_query);
$results = mysqli_num_rows($result);
if ($results){
$don = array('result' =>"fail","message"=>"Email or username exists.");
}else{
//This is where I am trying to get the id
while($row = mysqli_fetch_array($result)) {
$posts['id'] = $row['id'];
}
$sql_query = "insert into user_info values('$id','$email','$user_name','$passwordEncrypted','$confPasswordEncrypted');";
if(mysqli_query($con,$sql_query)){
$_SESSION['id'] = mysqli_insert_id($con);
//And this is the json response I was talking about
$don = array('result' =>"success","message"=>$posts['id']);
mail($email,"Well done. You are registered to my sample app!",$msg);
}
}
}else if(!$email){
$don = array('result' =>"fail","message"=>"Please enter a valid email");
}else if(!$user_name){
$don = array('result' =>"fail","message"=>"Please enter your username");
}else if(!$user_pass){
$don = array('result' =>"fail","message"=>"Please enter a password");
}else if(!confirmPass){
$don = array('result' =>"fail","message"=>"Please confirm your
password");
}
echo json_encode($don);
?>
更改
$don = array('result' =>"success","message"=>$posts['id']);
至
$don = array('result' =>"success","message"=>$_SESSION['id']);
$ posts ['id']始终为null,因为该行未插入数据库。 删除该代码。
更改
$don = array('result' =>"success","message"=>$posts['id']);
至:
$don = array('result' =>"success","message"=>mysqli_insert_id($con));
问题是您要引用的$posts['id']
始终为null
尝试在此处进行设置:
$results = mysqli_num_rows($result);
if ($results){
$don = array('result' =>"fail","message"=>"Email or username exists.");
}else{
while($row = mysqli_fetch_array($result)) {
$posts['id'] = $row['id'];
}
...
注意,只有while
$result
不包含任何行时,我们才会到达while
。 因此, mysqli_fetch_array($result)
为false,并且该循环永远不会执行。 实际上,此循环在此脚本中无用,应将其删除。
这超出了您的问题范围,但是您应该考虑以下问题:
$confirmPass
什么$confirmPass
? 您无需检查密码和确认密码是否匹配。 SELECT
查询至少转义了值,但INSERT
查询没有转义。 这使您容易受到SQL注入攻击的影响。 sha1($user_pass)
不是哈希pwd进行存储的好方法。 请改用PHP的password_hash
和password_verify
函数。 见指南 您不需要以下几行,因此请删除它们,因为在选择查询之前正在使用它们。
//This is where I am trying to get the id
while($row = mysqli_fetch_array($result)) {
$posts['id'] = $row['id'];
}
您可以更换
$don = array('result' =>"success","message" => $posts['id']);
同
$don = array('result' =>"success","message"=> mysqli_insert_id($con));
也不需要会话。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.