我有一个链接到预订表的用户表,因此我可以告诉用户做了哪些预订。我试图将用户表加入预订表,但它没有用。我还试图尝试传递会话userID,但它再次没有工作。我的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";
}
}
?>
&#13;
答案 0 :(得分:0)
我在系统的登录部分添加了会话变量,然后从那里传递了它。
以下登录部分:
<?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";
}
}
?>
&#13;
我还删除了PHP代码中的连接,最后得到了以下内容。它通过我的userID传递,但是此代码中仍然存在任何正在查看此错误的人的错误。我必须在我的页面顶部声明变量userID,然后再在我的prepare语句中声明。
<?php
session_start();
$userID = $_SESSION['userID'];
?>
&#13;
<?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";
}
}
?>
&#13;