我正在尝试使用准备好的语句将$ _SESSION ['userId']变量插入数据库中,而无需用户将其输入到预订表单中

时间:2019-02-16 09:45:06

标签: php mysqli

我正在创建一个预订系统,在该系统中,已经登录并存储在$ _SESSION ['userId']中的用户可以填写表格以预订公交车。我想通过使用准备好的语句将表单数据插入数据库。当我不尝试将userId插入数据库时​​,数据将顺利插入数据库,但是,一旦我尝试插入userId,它要么a)返回预订成功消息,但不插入数据库b)sqlerror

我对php很陌生,因此将不胜感激。

<?php

if (isset($_POST['booking-submit'])) {

    require 'dbh.inc.php';

    $user = $_SESSION['userId'];
    $pickupaddress = $_POST['pickupaddr'];
    $destaddress = $_POST['destaddress'];
    $returnaddress = $_POST['returnaddr'];
    $bussize = $_POST['dropdownbussize'];
    $datetimepickup = $_POST['datetimepickup'];
    $timereturn = $_POST['timereturn'];
    $remarks = $_POST['remarks'];


    $sql = "INSERT INTO booking (userBooking, originBooking, 
    destinationBooking, returndestBooking, busSizeBooking, 
    datetimeBooking, returnTimeBooking, remarksBooking) VALUES (?, 
    ?, ?, ?, ?, ?, ?, ?)";
    $stmt = mysqli_stmt_init($conn);
    if (!mysqli_stmt_prepare($stmt, $sql)) {
        header("Location: ../booking.php?error=sqlerror");
        exit();
    } else {
        mysqli_stmt_bind_param($stmt, "ssssssss", $user, $pickupaddress, 
        $destaddress, $returnaddress, $bussize, $datetimepickup, 
        $timereturn, $remarks);
        mysqli_stmt_execute($stmt);
        header("Location: ../booking.php?booking=success");
        exit();
    }
}

mysqli_stmt_close($stmt);
mysqli_close($conn);

} else {
    header("Location: ../booking.php");
    exit();
}

我希望URL中显示?booking = success消息,然后能够检查数据库并找到输入到行中的所有数据。

但是,我收到了?booking = success消息,数据库中没有新行。我在做什么错了?

0 个答案:

没有答案