如何从mysql获取客户ID,然后将其存储在会话变量中以使用另一个文件中的会话变量来检索信息

时间:2017-11-21 04:09:23

标签: php mysql session session-variables

我是PHP的新手。 我已经创建了一个登录页面和一个注册页面,我希望在成功登录或注册我的会话变量后存储客户ID,以便我可以在我的购物车文件中稍后使用会话变量来检索该客户的购物车详细信息。客户ID是在表上自动增量,因此我不会从客户输入客户ID。 这是代码

的login.php:

    <?php
session_start();
$db=mysqli_connect("localhost","root","Naruto97","musicgallery");

if (isset($_POST['login_btn'])){
    $email = mysqli_real_escape_string($db,$_POST['email']);
    $password = mysqli_real_escape_string($db,$_POST['password']);
    $password=md5($password);
    $sql="SELECT * FROM LoginCredentials WHERE email='$email' AND password='$password'";
    $result = mysqli_query($db,$sql);
    if(mysqli_num_rows($result)==1){
        $_SESSION['message']="You are now logged in";
        $_SESSION['email']=$email;
        //Want to store the customer in session variable here after successful login
        header("location:home.php");
    }
    else{
        $_SESSION['message']= "Email/Password combination Incorrect";
    }
}

   ?>

register.php:

     <?php
session_start();
$db=mysqli_connect("localhost","root","Naruto97","musicgallery");

if (isset($_POST['register_btn'])){

    $username = mysqli_real_escape_string($db,$_POST['username']);
    $email = mysqli_real_escape_string($db,$_POST['email']);
    $password = mysqli_real_escape_string($db,$_POST['password']);
    $password2 = mysqli_real_escape_string($db,$_POST['password2']);

    if($password==$password2){
        $password=md5($password);
        $sql = "INSERT INTO LoginCredentials(Username,email,password) VALUES('$username','$email','$password')";
        mysqli_query($db, $sql);
        $_SESSION['message']="You are now logged in";
        $_SESSION['email']=$email;
        //Want to store the customer_id here in the session variable after successful registration
        header("location:home.php");
    }else{
        $_SESSION['message']="The passwords do not match";

    }
}
   ?>

cart.php:

     <?php
       session_start();
       $db=mysqli_connect("localhost","root","Naruto97","musicgallery");
       if(isset($_POST["add_to_cart"])){
         if(isset($_SESSION['email'])){
           $album_name = mysqli_real_escape_string($db,$_POST['hidden_name']);
           $price = mysqli_real_escape_string($db,$_POST['hidden_price']);
           $quantity = mysqli_real_escape_string($db,$_POST['quantity']);
           //$customer_id= $_SESSION['customer_id'];
           $sql = "INSERT INTO cart(album_name,price,quantity,customer_id) VALUES('$album_name','$price','$quantity','$customer_id')";
          mysqli_query($db, $sql);
          echo "<script>alert('Added to cart')</script>";
          echo "<script>window.location('kpop.php')</script>";
          } 
          else
          {
             echo "<script>alert('Please Login')</script>";
             echo "<script>window.location('kpop.php')</script>";
          }
      }
     ?>

1 个答案:

答案 0 :(得分:0)

要使用mysqli中的最后一个insertid: -

std::vector<int>::iterator it;
it = find (myvector.begin(), myvector.end(), newvalue);
if (it == myvector.end()) {
    // newvalue is not found
}

如需进一步参考,您可以查看此link