drwu24647 2018-04-17 14:00
浏览 80
已采纳

PHP如何显示用户数据

I am trying to display the unique user who is logged in and then store it into a table in my database for example if I was logged in as James i want to display James' data from the database and store his id into another table.from my understanding the $_SESSION['login_user']; should return the unique users username.

when ever I run I get this error this is the output:

Notice: Undefined index: login_user in C:\xampp\htdocs\bid.php on line 3

Notice: Undefined variable: SESSION in C:\xampp\htdocs\bid.php on line 8 Thank You For Contacting Us

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条回答 默认 最新

  • dongmin3754 2018-04-17 14:26
    关注

    login.php

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

    Now if it's a success $_SESSION['login_user'] would be available in bid.php and every other pages.

    1. In session.php, please revert to these.

        <?php
         session_start(); 
         // this should be called first before including external files 
         include('config.php');
      
         $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");
         }
      ?>
      
    2. in bid.php please refer to this.

           $bidderid = $_SESSION['login_user']->username or  $_SESSION['login_user']['username']
      

    Hope this helps.. Happy coding

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?