如何在另一个文件中执行后使用php变量的值

xvw2m8pv  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(334)

我正在开发一个android应用程序,在这个应用程序上,我从用户那里获取应用程序id和申请人名称作为身份验证的输入值,该值使用php从mysql数据库匹配到下一个连接成功屏幕
登录.php

<?php

session_start();

require "conn.php";

$user_name = $_POST["user_name"];
$user_id = $_POST["id"];

$mysql_qry = "select * from complaints where Applicant_Name = '".$user_name."' and Complaint_ID like '".$user_id."';";

$result = mysqli_query($conn, $mysql_qry);
if (mysqli_num_rows($result) > 0){

     $_SESSION["username"] = "$user_name";
     $_SESSION["userid"] = "$user_id";

    echo "Login Success";
}
else {
    echo "Login Not Success";
}
?>

现在在下一个屏幕上,我想显示与特定应用程序id和申请人名称相关的内容,我想使用login.php中的变量$user\u name和$user\u id的值,在content.php查询的其中一部分获取我需要的特定数据。
现在我正在使用下面的代码,它现在运行良好,但是我想在content.php查询的where部分使用$user\u name和$user\u id的值
内容.php

<?php

session_start();

require "conn.php";

$username = $_SESSION["username"];
$userid = $_SESSION["userid"];

 //creating a query
 $stmt = $conn->prepare("SELECT Complaint_ID, Applicant_Name, Category, Subject, Description FROM complaints WHERE Complaint_ID = '".$userid."' AND Applicant_Name = '".$username."'");

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

 //binding results to the query 
 $stmt->bind_result($id, $name, $cat, $sub, $desc);

 $complaint = array(); 

 //traversing through all the result 
 while($stmt->fetch()){
 $temp = array();
 $temp['Complaint_ID'] = $id; 
 $temp['Applicant_Name'] = $name; 
 $temp['Category'] = $cat; 
 $temp['Subject'] = $sub; 
 $temp['Description'] = $desc; 
 array_push($complaint, $temp);
 }

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

?>

在android上没有任何问题,每件事都很好,php方面我需要帮助。

ybzsozfc

ybzsozfc1#

您可以使用php$sessions来处理这个问题。为了你的 login.php 按以下步骤操作

<?php
session_start();
require "conn.php";
$user_name = $_POST["user_name"];
$user_id = $_POST["id"];
            //your codes
    if (mysqli_num_rows($result) > 0){
         echo "Login Success";
     //just add this two sessions
         $_SESSION["username"] = "$user_name";
         $_SESSION["userid"] = "$user_id";
      } else {
      echo "Login Not Success";
             }
            ?>

然后在 content.php 如果成功,对会话执行检查?获取存储在 $SESSIONS[] 当登录成功时。

<?php
    session_start();
    if (isset($_SESSION['userid'])) {
//if succesfully set, get the values stored sessions.
        $username = $_SESSION['username'];
        $userid = $_SESSION['userid'];
//just echo to check if set output results are correct
    echo "sessions were set successfully username is $username and userid is $userid";
    }else{
    echo "was unable to set sessions";
    }
   require "conn.php"
//creating a query
    $stmt = $conn->prepare("SELECT Complaint_ID, Applicant_Name, Category, Subject, Description FROM complaints WHERE Complaint_ID = '$userid' AND Applicant_Name = '$username'");  
//Rest of your codes
        ?>

欲了解更多信息,请访问https://www.w3schools.com/php/php_sessions.asp. 所有php页面都提供了会话,因此您可以从任何地方选择它们,并且存储的信息不会保存在用户计算机中。

l7wslrjt

l7wslrjt2#

我认为首先需要在login.php中更改select查询,应该使用“=”而不是like

$mysql_qry = "select * from complaints where Applicant_Name like '$user_name' and Complaint_ID like '$user_id';";

$mysql_qry = "select * from complaints where Applicant_Name='".$user_name."' and Complaint_ID='".$user_id."' ";

需要在会话中存储$user\u name和$user\u id,并在content.php上使用该会话值,如下所示
在login.php中设置如下值

if (mysqli_num_rows($result) > 0){
   $_SESSION['name']=$user_name;
   $_SESSION['id']=$user_id; 
   echo "Login Success";
 }

别忘了在两个文件的顶部启动会话
在content.php文件中使用以下查询

$stmt = $conn->prepare("SELECT Complaint_ID, Applicant_Name, Category, Subject, Description FROM complaints WHERE Complaint_ID = '".$_SESSION['id']."' AND Applicant_Name = '".$_SESSION['name']."'");

相关问题