drtoclr046994545 2018-07-05 11:48
浏览 48

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

I am working on an android app on which I take Application ID and Applicant Name from the user as an input value for authentication which is matched from an MYSQL database using PHP to get to the next screen on connection success

login.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";
}
?>

Now on next screen, I want to show the content related to that particular Application ID and Applicant Name for which I want to use the value of variable $user_name and $user_id from login.php in WHERE part of query of content.php to fetch the particular data I require.

Right now I am using the code below which is working fine right now but I want to use the value of $user_name and $user_id to use in WHERE part of content.php query

content.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);

?>

On android there is no issue every thing is working fine its the PHP side I need Help.

  • 写回答

2条回答 默认 最新

  • duanbin198788 2018-07-05 12:08
    关注

    I think first you need to change your select query in login.php, you should use '=' instead of like

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

    to

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

    and need to store that $user_name and $user_id in session and use that session values on content.php like below

    In login.php set values like below

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

    Dont forget to start session at the top of both files

    In content.php file use below query

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

报告相同问题?

悬赏问题

  • ¥15 vscode问题请教
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM