gpt4 book ai didi

php - 在我的 json 响应中获取空值。

转载 作者:行者123 更新时间:2023-11-29 01:27:05 26 4
gpt4 key购买 nike

我有以下注册 php 脚本。我想取回一个 json 响应 {“结果”:“成功”,“消息”:“123”

其中 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'] 始终为空,因为该行未插入到数据库中。删除该代码。

关于php - 在我的 json 响应中获取空值。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38391772/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com