PHP如何显示用户数据

时间:2018-04-17 14:00:48

标签: php mysql

我正在尝试显示已登录的唯一用户,然后将其存储到我的数据库中的表中,例如,如果我以James身份登录,我想显示James'来自数据库的数据并将他的id存储到另一个table.from我的理解$ _SESSION [' login_user'];应该返回唯一用户的用户名。

当我跑步时,我得到这个错误,这是输出:

注意:未定义的索引:第3行的C:\ xampp \ htdocs \ bid.php中的login_user

注意:未定义的变量:第8行的C:\ xampp \ htdocs \ bid.php中的SESSION 感谢您与我们联系

的login.php

<?php
   include("config.php");
   session_start();

   if($_SERVER["REQUEST_METHOD"] == "POST") {
      // username and password sent from form 

      $myusername = mysqli_real_escape_string($conn,$_POST['username']);
      $mypassword = mysqli_real_escape_string($conn,$_POST['password']); 

      $sql = "SELECT customer_id FROM customer WHERE email_adress = '$myusername' and password = '$mypassword'";
      $_SESSION['username'] = $myusername
      $result = mysqli_query($conn,$sql);
      $row = mysqli_fetch_array($result,MYSQLI_ASSOC);
      $active = $row['customer_id'];

      $count = mysqli_num_rows($result);

      // If result matched $myusername and $mypassword, table row must be 1 row

 if($count >= 1) {
     $_SESSION['login_user'] = $myusername;

     header("location: index2.php");
  }else {
     $error = "Your Login Name or Password is invalid";
  }
   }
?>

bid.php:

<?php
    session_start();
    echo $_SESSION['username'];
require 'config.php';
//include "job.php" 
$jobid    = $_POST['job_id'];
$bid    = $_POST['bid'];
bidderid = $SESSION['username']
echo "$jobid";






$query   = "INSERT into bid (bid_amount,job_id) VALUES('" . $bid . "','" . $jobid . "')";
$success = $conn->query($query);

if (!$success) {
    die("Couldn't enter data: ".$conn->error);

}

echo "Thank You For Contacting Us <br>";

$conn->close();

 ?>

session.php文件

<?php
   include('config.php');
   session_start();

   $user_check = $_SESSION['login_user'];

   $ses_sql = mysqli_query($conn,"select email_adress from customer where email_adress = '$user_check' ");

   $row = mysqli_fetch_array($ses_sql,MYSQLI_ASSOC);

   $login_session = $row['email_adress'];

   if(!isset($_SESSION['login_user'])){
      header("location:login.php");
   }
?>

1 个答案:

答案 0 :(得分:0)

的login.php

export async function get(pathname) {
    const params = {
      headers: {
        'Content-Type': 'application/json',
      }
    };
    let response = await fetch(`${__ROOT_API__}${pathname}`, params);
    if (!validateStatusCode(response)) {
      var error = new Error(response.statusText)
      error.response = response
      throw error
    }
    return response.json();
}

现在,如果成功,$ _SESSION [&#39; login_user&#39;]将在bid.php和其他所有网页中提供。

  1. 在session.php中,请恢复为这些。

      11 |     let response = await fetch(`${__ROOT_API__}${pathname}`, params);
          12 |     if (!validateStatusCode(response)) {
        > 13 |       var error = new Error(response.statusText)
          14 |       error.response = response
          15 |       throw error
          16 |     }
    
          at _callee$ (src/services/project/project.js:13:19)
          at tryCatch (node_modules/regenerator-runtime/runtime.js:62:40)
          at Generator.invoke [as _invoke] (node_modules/regenerator-runtime/runtime.js:296:22)
          at Generator.prototype.(anonymous function) [as next] (node_modules/regenerator-runtime/runtime.js:114:21)
          at step (node_modules/babel-runtime/helpers/asyncToGenerator.js:17:30)
          at node_modules/babel-runtime/helpers/asyncToGenerator.js:28:13
    
  2. bid.php中的
  3. 请参阅此内容。

    <?php
       session_start();
       // session should start here
    
       include("config.php");
    
    
       if($_SERVER["REQUEST_METHOD"] == "POST") {
          // username and password sent from form 
    
          $myusername = mysqli_real_escape_string($conn,$_POST['username']);
          $mypassword = mysqli_real_escape_string($conn,$_POST['password']); 
    
          $sql = "SELECT customer_id FROM customer WHERE email_adress = '$myusername' and password = '$mypassword'";
    
          $_SESSION['username'] = $myusername; // you missed a semi colon
    
          $result = mysqli_query($conn,$sql);
          $row = mysqli_fetch_array($result,MYSQLI_ASSOC);
          $active = $row['customer_id'];
    
          $count = mysqli_num_rows($result);
    
          // If result matched $myusername and $mypassword, table row must be 1 row
    
     if($count >= 1) {
    
         $_SESSION['login_user'] = $myusername; // $myusername contains username of this user
    
         $_SESSION['login_data'] = $row; // $row contains user record from the db. including customer_id etc..
    
    
         header("location: index2.php");
      }else {
         $error = "Your Login Name or Password is invalid";
      }
       }
    ?>
    
  4. 希望这有帮助..快乐编码