Php POST变量在会话中不起作用

Php POST变量在会话中不起作用,php,mysql,sql,session,post,Php,Mysql,Sql,Session,Post,我想将login.phpPOST variables$user\u key&$user\u id的值传递到另一个我使用会话的文件statusdata.php。在statusdata.php中,我想在statusdata.php文件的SQL查询中使用这些会话变量值 为了在login.php中实现这一点,首先我分别将POST变量$\u POST[“user\u key”]和$\u POST[“id”]的值传递给变量$user\u key$user\u id然后将变量$user\u key的值传递给会

我想将
login.php
POST variables
$user\u key
&
$user\u id
的值传递到另一个我使用会话的文件
statusdata.php
。在
statusdata.php
中,我想在
statusdata.php
文件的SQL查询中使用这些会话变量值

为了在
login.php
中实现这一点,首先我分别将POST变量
$\u POST[“user\u key”]
$\u POST[“id”]
的值传递给变量
$user\u key
$user\u id然后将变量
$user\u key
的值传递给会话变量
$\u会话[“key”]
&
$\u SESSION[“id”]
然后分别在
statusdata.php
中调用SESSION变量,并在
statusdata.php的变量
$user\u key
&
$user\u id
中传递它们的值

现在问题是值POST变量
$\u POST[“user\u key”]
&
$\u POST[“id”]
我们不会去会话
statusdata.php
,但是如果我在
login.php
中直接给出
$user\u key
&
$user\u id
的值,那么会话可以正常工作,但不会使用POST变量

我想再次澄清,POST变量是通过android应用程序从用户那里获取值,该应用程序要求
id
key
login.php
对用户进行身份验证,然后移动到另一个屏幕,使用
statusdata.php
检索与输入的
id
key
相关的数据

帮助我在会话中从
login.php
获取post变量的值,以便在
statusdata.php

login.php

<?php

// Start the session
session_start();

require "conn.php";

if (isset($_POST["id"])) {
  $user_id = mysqli_real_escape_string($conn,$_POST["id"]);
}

if (isset($_POST["user_key"])) {
  $user_key = mysqli_real_escape_string($conn,$_POST["user_key"]);
}

/* USING THESE DIRECT VALUE IN PLACE OF POST VARIABLE IS WORKING FINE IN SESSION FOR BOTH `login.php` and `statusdata.php` which indicate there is no problem with `statusdata.php` but using POST variable is working fine for `login.php` as authenticating user with `id` and `key` but value of POST variable is not going to session in `statusdata.php`.  

$user_key = "8c9333343c6c4222418edb1d7c9f84d051610526085960a1732c7c3d763fff64ec7f5220998434c896dda243ae777d0fb213f36b9b19f7e4a244d5c993b8dfed";
$user_id = "96";
*/

$mysql_qry = "select * from applications where application_key = '".$user_key."' and application_id = ".$user_id." ;";

$result = mysqli_query($conn, $mysql_qry);
if (mysqli_num_rows($result) > 0){
    $_SESSION["key"] = $user_key;
    $_SESSION["id"] = $user_id;
    echo "Login Success";
} else {
    echo "Login Not Success";   
}

?>
<?php
// Start the session
session_start();

require "conn.php";

$user_key = "-1";
if (isset($_SESSION["key"])) {
  $user_key = $_SESSION["key"];
}

$user_id = "-1";
if (isset($_SESSION["id"])) {
  $user_id = $_SESSION["id"];
}

 //creating a query
 $stmt = $conn->prepare("SELECT applications.application_id, applications.applicant_name, applications.applicant_aadhaar, applications.applicant_phone, applications.subject, applications.date, applications.description, applications.chairperson_remark, applications.status, officer_accounts.name_of_officer, applications.officer_remark, applications.last_update_on 
FROM applications INNER JOIN officer_accounts ON applications.account_id = officer_accounts.account_id 
WHERE applications.application_id = ? AND applications.application_key = ? ;");

 $stmt->bind_param('ss',$user_id,$user_key);

 //executing the query 
 $stmt->execute();

 //binding results to the query 
 $stmt->bind_result($id, $name, $aadhaar, $phone, $subject, $date, $description, $chairperson, $status, $officername, $officerremark, $lastupdate);

 $applications = array(); 

 //traversing through all the result 
 while($stmt->fetch()){
 $temp = array();
 $temp['applications.application_id'] = $id; 
 $temp['applications.applicant_name'] = $name; 
 $temp['applications.applicant_aadhaar'] = $aadhaar; 
 $temp['applications.applicant_phone'] = $phone; 
 $temp['applications.subject'] = $subject;
 $temp['applications.date'] = $date;
 $temp['applications.description'] = $description;
 $temp['applications.chairperson_remark'] = $chairperson;
 $temp['applications.status'] = $status;
 $temp['officer_accounts.name_of_officer'] = $officername;
 $temp['applications.officer_remark'] = $officerremark;
 $temp['applications.last_update_on'] = $lastupdate;
 array_push($applications, $temp);
 }

 //displaying the result in json format 
 echo json_encode($applications);

 // remove all session variables
session_unset(); 

// destroy the session 
session_destroy(); 

?>
注意-我将以JSON格式获取
statusdata.php
SQL查询的输出,最后我将在android中提取它


请帮助我我尝试了其他类似问题建议的所有方法,但没有任何帮助您的SQL语句有问题:

$mysql\u qry=“从应用程序中选择*,其中application\u key=”“$user\u key.”和
应用程序_id='“$user_id.”“;”;
将此更改为:

$mysql_qry = "select * from applications where application_key = '".$user_key."' and 
application_id = ".$user_id." ;";
应用程序_id的数据类型很可能是整数。
尝试在MySQL提示符下运行它。如果继续,它将向您显示错误

不是一个完整的答案,而是对脚本的一些改进:

login.php:

<?php

// Start the session
session_start();

require "conn.php";

if (isset($_POST["id"])) {
  $user_id = $conn->real_escape_string($_POST["id"]);
}

if (isset($_POST["user_key"])) {
  $user_key = $conn->real_escape_string($_POST["user_key"]);
}

// check output is as expected
echo "ID:" . $user_id;
echo "Key:" . $user_key;

/* USING THESE DIRECT VALUE IN PLACE OF POST VARIABLE IS WORKING FINE IN SESSION FOR BOTH `login.php` and `statusdata.php` which indicate there is no problem with `statusdata.php` but using POST variable is working fine for `login.php` as authenticating user with `id` and `key` but value of POST variable is not going to session in `statusdata.php`.  

$user_key = "8c9333343c6c4222418edb1d7c9f84d051610526085960a1732c7c3d763fff64ec7f5220998434c896dda243ae777d0fb213f36b9b19f7e4a244d5c993b8dfed";
$user_id = "96";
*/

$mysql_qry = <<<EOF
SELECT * from applications
WHERE application_key = "{$user_key}" and application_id = "{$user_id}"
EOF;

$result = $conn->query($mysql_qry);
if ($result->num_rows() > 0) {
    $_SESSION["key"] = $user_key;
    $_SESSION["id"] = $user_id;
    echo "Login Success";
} else {
    echo "Login Not Success";   
}

?>
<?php
// Start the session
session_start();

require "conn.php";

// use ternary operators, you can even try ?: operator
$user_key = isset($_SESSION["key"]) ? $_SESSION["key"] : '-1';
$user_id = isset($_SESSION["id"]) ? $_SESSION["id"] : '-1';

// use heredocs for creating a query, they let you format your query neatly
// also try using shorter names 
$query = <<<EOF
SELECT app.application_id, app.applicant_name, 
    app.applicant_aadhaar, app.applicant_phone, 
    app.subject, app.date, app.description, 
    app.chairperson_remark, app.status, 
    oa.name_of_officer, app.officer_remark, 
    app.last_update_on 
FROM applications app
INNER JOIN officer_accounts oa
ON app.account_id = oa.account_id 
WHERE app.application_id = ? AND app.application_key = ? ;
EOF;

$stmt = $conn->prepare($query);
$stmt->bind_param('ss',$user_id,$user_key);    
//executing the query 
$stmt->execute();

$applications = array(); 
$result = $stmt->get_result();

// get the rows, use fetch_object or fetch_array
while ($row = $result->fetch_object()) {
    $applications[] = $row;
}

//displaying the result in json format 
echo json_encode($applications);

// remove all session variables
session_unset(); 

// destroy the session 
session_destroy(); 

?>

Statusdata.php:

<?php

// Start the session
session_start();

require "conn.php";

if (isset($_POST["id"])) {
  $user_id = $conn->real_escape_string($_POST["id"]);
}

if (isset($_POST["user_key"])) {
  $user_key = $conn->real_escape_string($_POST["user_key"]);
}

// check output is as expected
echo "ID:" . $user_id;
echo "Key:" . $user_key;

/* USING THESE DIRECT VALUE IN PLACE OF POST VARIABLE IS WORKING FINE IN SESSION FOR BOTH `login.php` and `statusdata.php` which indicate there is no problem with `statusdata.php` but using POST variable is working fine for `login.php` as authenticating user with `id` and `key` but value of POST variable is not going to session in `statusdata.php`.  

$user_key = "8c9333343c6c4222418edb1d7c9f84d051610526085960a1732c7c3d763fff64ec7f5220998434c896dda243ae777d0fb213f36b9b19f7e4a244d5c993b8dfed";
$user_id = "96";
*/

$mysql_qry = <<<EOF
SELECT * from applications
WHERE application_key = "{$user_key}" and application_id = "{$user_id}"
EOF;

$result = $conn->query($mysql_qry);
if ($result->num_rows() > 0) {
    $_SESSION["key"] = $user_key;
    $_SESSION["id"] = $user_id;
    echo "Login Success";
} else {
    echo "Login Not Success";   
}

?>
<?php
// Start the session
session_start();

require "conn.php";

// use ternary operators, you can even try ?: operator
$user_key = isset($_SESSION["key"]) ? $_SESSION["key"] : '-1';
$user_id = isset($_SESSION["id"]) ? $_SESSION["id"] : '-1';

// use heredocs for creating a query, they let you format your query neatly
// also try using shorter names 
$query = <<<EOF
SELECT app.application_id, app.applicant_name, 
    app.applicant_aadhaar, app.applicant_phone, 
    app.subject, app.date, app.description, 
    app.chairperson_remark, app.status, 
    oa.name_of_officer, app.officer_remark, 
    app.last_update_on 
FROM applications app
INNER JOIN officer_accounts oa
ON app.account_id = oa.account_id 
WHERE app.application_id = ? AND app.application_key = ? ;
EOF;

$stmt = $conn->prepare($query);
$stmt->bind_param('ss',$user_id,$user_key);    
//executing the query 
$stmt->execute();

$applications = array(); 
$result = $stmt->get_result();

// get the rows, use fetch_object or fetch_array
while ($row = $result->fetch_object()) {
    $applications[] = $row;
}

//displaying the result in json format 
echo json_encode($applications);

// remove all session variables
session_unset(); 

// destroy the session 
session_destroy(); 

?>

我认为,您的问题与您在分配
$\u会话
值之前输入的
if
语句有关。您可以检查数据库中是否有与查询相对应的数据,否则您将永远无法在statusdata.php文件中获得这两个会话值。
您可以通过分配
$\u SESSION
值来检查我的答案,而不必使用
if
语句,并尝试再次访问它们。

错误报告和查询错误检查是一个开始。这里没有HTML,也没有其他这些是android应用程序的PHP脚本HTML不是必需的。我已经在任何情况下提到过它,见我的第一条评论。您没有检查错误并假设它按原样工作。我已经尝试了错误检查,但我不明白为什么post变量没有进入statusdata。phpHai post在
登录时工作正常。php
我想做的是在
statusdata中使用post变量的值。php
您检查过我的SQL语句吗提到了吗?请尝试在第一个脚本中打印$user\u key和$user\u id的值,以检查mysqli\u escape\u字符串是否破坏了它们。还要回显第二个脚本中的值,看看它们是否是从$u会话okayAgain中检索到的。发生了我在给login.php中的id和key赋予直接值的帖子中提到的同样的事情,然后回显$user_key和$user_id在login.php和statusdata.php中都起作用,但是使用post来代替赋予它的直接值login.php但这些值在statusdata.php中不起作用。在使用
if
和不使用
if
时,这两种方法都存在问题