将多个会话变量从PHP插入到mySQL数据库中,为什么它不起作用

时间:2015-12-03 18:28:24

标签: php mysql mysqli sql-insert

您好我无法获得以下查询以便将会话数据插入数据库中,有人可以告诉我它有什么问题或我可以做些什么来解决它?提前谢谢。

<?php
session_start();
 if (isset($_POST['sub'])) {
$host='localhost';
$user='root';
$pass='';
$db='theatre_booking';

$con=mysqli_connect($host,$user,$pass,$db);

$row = $_POST['row'];
$_SESSION["row"]=$row;  

$zone = $_POST['zone'];
$_SESSION["zone"]=$zone;

$quantity = $_POST['numberOfTickets'];
$_SESSION["numberOfTickets"]=$quantity;



$sql="INSERT INTO booking(PerfDate, PerfTime, Name, Email, RowNumber) 
VALUES 
    '{$_SESSION['date']}',
    '{$_SESSION['time']}',
    '{$_SESSION['name']}',
    '{$_SESSION['email']}',
    '{$_SESSION['row']}')";

    if ($con->query($sql) === TRUE) {
    echo "Booking successful";
} else {
    echo "Error: " . $sql . "<br>" . $con->error;
}
 }





?>

2 个答案:

答案 0 :(得分:0)

你缺少开括号'('插入语句中的值之后。

$sql="INSERT INTO booking(PerfDate, PerfTime, Name, Email, RowNumber) 
VALUES (
    '{$_SESSION['date']}',
    '{$_SESSION['time']}',
    '{$_SESSION['name']}',
    '{$_SESSION['email']}',
    '{$_SESSION['row']}')";

    if ($con->query($sql) === TRUE) {
    echo "Booking successful";
} else {
    echo "Error: " . $sql . "<br>" . $con->error;
}

答案 1 :(得分:0)

VALUES之后缺少左括号,我建议你明确地在字符串中写入变量。 不要依赖php变量扩展,它也更容易阅读。

$sql='INSERT INTO booking(PerfDate, PerfTime, Name, Email, RowNumber)
 VALUES
 ("' . $_SESSION["date"] . '", 
  "' . $_SESSION['time'] . '",
  "' . $_SESSION['name'] . '",
  "' . $_SESSION['email'] . '",
  "' . $_SESSION['row']  . '"
  )';