[英]how to get customer id from mysql and then to store it in a session variable to use the session variable in another file to retrieve information
I am new to PHP. 我是PHP新手。 I've made a login page and a register page, and I want to store the customer id after successful login or registration on my session variable so that I can use a session variable later in my cart file to retrieve cart details of that customer.The customer id is autoincrementing on the table so I don't take the input from the customer for the customer id. 我已经完成了登录页面和注册页面的创建,我想在成功登录或注册会话变量后存储客户ID,以便以后可以在购物车文件中使用会话变量来检索该客户的购物车详细信息。客户ID在表上自动递增,因此我不从客户那里输入客户ID。 here are the codes 这是代码
login.php: 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: 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: 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>";
}
}
?>
To get the last insertid in mysqli use:- 要获取mysqli中的最后一个insertid,请使用:-
$sql = "INSERT INTO cart(album_name,price,quantity,customer_id) VALUES('$album_name','$price','$quantity','$customer_id')";
mysqli_query($db, $sql);
$_SESSION['customerId']=mysqli_insert_id($db);//gets the last inserted customerid from db
For any further reference you can check in this link . 有关更多参考,您可以检查此链接 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.