Inserting multiple session variables from PHP into mySQL database, why isn't it working

Hi I cannot get the below query to work in order to insert the session data into the database, Could anyone please tell me what is wrong with it or what I can do to fix it? Thanks in advance.

<?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;
}
 }





?>

Answers


you are missing opening bracket '(' after values in insert statement.

$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;
}

Missing opening parenthesis after VALUES and I suggest you explicitly write the variables in the string. Don't rely on php variable expansion, it's also much easier to read.

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

Need Your Help

Access Servicstack.net session in validator

c# validation session servicestack servicestack-bsd

How can I access a ServiceStack.net session in my validation code?

VS2013 cannot debug using Anonymous Authentication. 404 when debugging using Windows Authentication

c# asp.net visual-studio-2013

I turned on the feature to 'Break when an exception is Common Language Runtime Exception' using Ctrl+Alt+E in VS2013. Since then I got different errors when trying to run my project (cannot access ...