gpt4 book ai didi

php - 将用户 ID 传递到预订表中

转载 作者:行者123 更新时间:2023-11-29 22:29:41 25 4
gpt4 key购买 nike

我有一个链接到预订表的用户表,这样我就可以知道哪些用户进行了哪些预订。我尝试将用户表与预订表左连接,但没有成功。我还尝试过尝试传递 session 用户ID,但同样没有成功。我的 PHP 代码如下所示,我现在正试图通过它。

	<?php
include "config.php";

//Booking point
if(isset($_POST['booking']))
{
//get values for variables
$pitchID = $_POST['pitchID'];
$start_date = $_POST['start_date'];
$start_hour = $_POST['start_hour'];
$end_hour = $_POST['end_hour'];
$booking_age = $_POST['booking_age'];
$pitch_size = $_POST['pitch_size'];
$light_tokens = $_POST['light_tokens'];

$q = $db->prepare("SELECT *
FROM booking
LEFT JOIN user
ON booking.userID=user.userID");
$query = $q-> execute();

if($query)
{
$q = $db->prepare("INSERT INTO booking SET pitchID = ?, start_date = ?, start_hour = ?, end_hour = ?, booking_age = ?, pitch_size = ?, light_tokens = ?");
$query = $q->execute(array($pitchID,$start_date,$start_hour,$end_hour,$booking_age,$pitch_size,$light_tokens));
$count = $q->rowCount();

if($count == 1){
echo "Your booking has been made";
header("Location:home2_template.html");
return;
}else {
echo "Fail";
}
} else {
echo"Booking already exists";
}
}
?>

最佳答案

我在系统的登录部分添加了 session 变量,然后从那里传递它。

登录部分如下:

<?php
include "config.php";
if(isset($_POST['submit'])){

$username = $_POST['username'];
$password = $_POST['password'];

$q = $db->prepare("SELECT * FROM user WHERE username = ? AND password = ?");
$query = $q->execute(array($username, $password));
while($dbRow = $q->fetch(PDO::FETCH_ASSOC)) {
$userID = $dbRow['userID'];
}
$count = $q->rowCount();
if($count == 1){
session_start();
$_SESSION['userID'] = $userID;
header("Location:home2_template.html");
return;
}else{
echo "You have entered an incorrect login/password";
}
}
?>

我还删除了 PHP 代码中的连接,最后得到了下面的结果。它通过了我的用户 ID,但对于查看此代码的任何人来说,此代码中仍然存在错误。我必须在页面顶部声明变量 userID,然后在准备语句中再次声明。

<?php 
session_start();
$userID = $_SESSION['userID'];
?>

<?php
include "config.php";
echo $userID;
//Booking point
if(isset($_POST['booking']))
{
//get values for variables
$pitchID = $_POST['pitchID'];
$start_date = $_POST['start_date'];
$start_hour = $_POST['start_hour'];
$end_hour = $_POST['end_hour'];
$booking_age = $_POST['booking_age'];
$pitch_size = $_POST['pitch_size'];
$light_tokens = $_POST['light_tokens'];

$q = $db->prepare("INSERT INTO booking SET pitchID = ?, start_date = ?, start_hour = ?, end_hour = ?, booking_age = ?, pitch_size = ?, light_tokens = ?, userID='$userID'");
$query = $q->execute(array($pitchID,$start_date,$start_hour,$end_hour,$booking_age,$pitch_size,$light_tokens));

$count = $q->rowCount();
if($count == 0)
{
echo "Your booking has been made";
header("Location:home2_template.html");
return;
}else {
echo "That booking already exists";
}
}

?>

关于php - 将用户 ID 传递到预订表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29917098/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com