从php插入值到msql

时间:2018-05-02 11:18:08

标签: php mysqli

我正在编写一个php代码,将一个客户的个人信息插入一个名为“bookings”的表中,并将他预订的座位插入另一个名为“bookseat”的表中,该表位于同一个数据库中,同一个php文件。当代码执行时,个人信息存储在相关的表中,但是没有存储预订的座位。当我手动将值插入bookseat表时,我看到虽然没有保存php文件中的先前值,但已经保存了一些未显示的记录(因为表中的自动增量列(sid)似乎已增加)。

Here is a screenshot of the bookseat table.

这是我的PHP代码。

$servername = "localhost";
$username = "root";
$password = "";
$db = "titan3d";

$name = $_SESSION['name'];
$nic = $_SESSION['nic'];
$email = $_SESSION['email'];
$teleno = $_SESSION['teleno'];
$sdate = $_SESSION['sdate'];
$stime = $_SESSION['stime'];
$now = $_SESSION['now'];

$bookseat = array(11, 18, 12);
$arrlength = count($bookseat);

if(isset($_POST['name']))
    {
        $name = $_POST["name"];
    }
if(isset($_POST['nic']))
    {
        $nic = $_POST["nic"];
    }
if(isset($_POST['email']))
    {
        $email = $_POST["email"];
    }
if(isset($_POST['teleno']))
    {
        $teleno = $_POST["teleno"];
    }
if(isset($_POST['sdate']))
    {
        $sdate = $_POST["sdate"];
    }
if(isset($_POST['stime']))
    {
        $stime = $_POST["stime"];
    }
if(isset($_POST['now']))
    {
        $now = $_POST["now"];
    }

$conn = new mysqli($servername, $username, $password, $db);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);  
} 

$sql = "INSERT INTO bookings (name, nic, email, teleno, sdate, stime)
VALUES ('$name', '$nic', '$email', '$teleno', '$sdate', '$stime')";

for($i = 0; $i < count($bookseat); ++$i){
    $SQL_project_has_type = "INSERT INTO bookseat (seat, bid)
         VALUES({$bookseat[$i]}, $now)";
    mysqli_query($conn, $SQL_project_has_type);
}

if ($conn->query($sql) === TRUE) {
    header('Location: http://localhost/My%20Project/booksuccess.html');
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();

这里有什么问题吗?有人可以为我解决这个问题吗?

0 个答案:

没有答案