gpt4 book ai didi

php - session 变量在 echo 中工作但在 SQL 查询中不起作用

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

我想将 login.php 变量 $user_key$user_id 的值传递到另一个文件 statusdata.php 我已经使用了 session.在 statusdata.php 中,我想在 statusdata.php 文件的 SQL 查询中使用这些 session 变量值。

为了在 login.php 中实现此目的,我将变量 $user_key$user_id 的值传递给 session 变量 $_SESSION ["key"]$_SESSION["id"] 分别在 statusdata.php 中调用 session 变量并将其值传递到变量 statusdata.php

的 $user_key$user_id

现在的问题是,在 SQL 查询中使用变量 $user_key$user_id 时,它没有返回正确的输出,但在 echo 中使用相同的变量,它给出了正确的输出当我回显变量但在 SQL 中不起作用时,值均值 session 工作正常。我也尝试过直接传递 session 变量,但是在 echo 中发生了同样的事情,但在 SQL 中却没有。

login.php

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

require "conn.php";

$user_key = '8C9333343C6C4222418EDB1D7C9F84D051610526085960A1732C7C3D763FFF64EC7F5220998434C896DDA243AE777D0FB213F36B9B19F7E4A244D5C993B8DFED';
$user_id = '1997';

$mysql_qry = "select * from applications where application_key = '".$user_key."' and application_id like '".$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";
}
?>

statusdata.php

<?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 = '".$user_id."' AND applications.application_key = '".$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);

// Echo session variables that were set on previous page
echo "<br>key is " . $user_key . ".<br>";
echo "id is " . $user_id . ".";

?>

输出login.php

Login Success

输出statusdata.php

[]
key is .8C9333343C6C4222418EDB1D7C9F84D051610526085960A1732C7C3D763FFF64EC7F5220998434C896DDA243AE777D0FB213F36B9B19F7E4A244D5C993B8DFED..
id is .1997..

我想要从 statusdata.php 输出(如果我在变量 $user_key$user_id 中使用直接值,我就会得到它> 不是来自 login.php 的 session 变量)

[{"applications.application_id":1997,"applications.applicant_name":"Tanishq","applications.applicant_aadhaar":"987654321","applications.applicant_phone":"123456789","applications.subject":"asdnjsnadnksncdnsjnvsavasdnjsnadnksncdnsjnvsav","applications.date":"2018-07-02 09:11:47","applications.description":"asdnjsnadnksncdnsjnvsavasdnjsnadnksncdnsjnvsavasdnjsnadnksncdnsjnvsavasdnjsnadnksncdnsjnvsavasdnjsnadnksncdnsjnvsav","applications.chairperson_remark":"asdnjsnadnksncdnsjnvsavasdnjsnadnksncdnsjnvsav","applications.status":1,"officer_accounts.name_of_officer":"Chayan Bansal","applications.officer_remark":"asdnjsnadnksncdnsjnvsavasdnjsnadnksncdnsjnvsav","applications.last_update_on":"2018-07-22 09:14:25"}]
key is 8C9333343C6C4222418EDB1D7C9F84D051610526085960A1732C7C3D763FFF64EC7F5220998434C896DDA243AE777D0FB213F36B9B19F7E4A244D5C993B8DFED.
id is 1997.

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

请帮助我,我已经尝试了其他类似问题所建议的所有内容,但没有任何帮助

最佳答案

看起来更像是拼写错误的情况。您在 login.php 中有这个:

$_SESSION["key"] = ".$user_key.";
$_SESSION["id"] = ".$user_id.";

这会玷污你的原始值(value)观……在它们周围添加不​​必要的点。通过简单地分配来清理它:

$_SESSION["key"] = $user_key;
$_SESSION["id"] = $user_id;

它应该开始更好地工作。

<小时/>

旁注,您正在使用prepare,但没有使用bind_param。将 SQL prepare 更改为以下内容(注意 ? 占位符),并添加 bind_param:

$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);

关于php - session 变量在 echo 中工作但在 SQL 查询中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51477911/

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