会话变量在echo中起作用,但在SQL查询中不起作用

时间:2018-07-23 11:40:10

标签: php mysql sql session session-variables

我想将login.php变量$user_key$user_id的值传递给我使用了会话的另一个文件statusdata.php。在statusdata.php中,我想在statusdata.php文件的sql查询中使用这些会话变量值。

要在login.php中实现此目的,我先将变量$user_key$user_id的值分别传递给会话变量$_SESSION["key"]$_SESSION["id"],然后再传递给{{1} }我将会话变量称为变量,并在statusdata.php

的变量$user_key$user_id中传递它们的值

现在的问题是,在SQL查询中使用变量statusdata.php$user_key时,它没有返回正确的输出,但是在echo中使用了相同的变量,却给出了适当的值,这意味着会话可以正常工作。回显变量,但在SQL中不起作用。我也尝试过直接传递会话变量,但在echo中却发生了同样的事情,但在SQL中却没有。

$user_id

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中使用直接值,而不是login.php中的会话变量,我会得到它)

$user_id

注意:我正在以JSON格式获取[{"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. SQL查询的输出,最后在Android中将其提取。

请帮助我,我已经尝试了其他类似问题所建议的一切,但没有帮助

1 个答案:

答案 0 :(得分:0)

看起来更像是错别字。您在login.php中有此内容:

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

通过添加不必要的点来污染您的原始值。只需简单分配即可:

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