SQL INSERT doens插入,没有任何错误

时间:2018-03-26 15:41:58

标签: php html sql

我正在为学校项目制作酒店预订系统。 客人首先需要创建一个帐户:

<?php 

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

include_once 'dbh.inc.php';

$signupdate = mysqli_real_escape_string($conn, $_POST['signupdate']);
$first = mysqli_real_escape_string($conn, $_POST['firstname']);
$last = mysqli_real_escape_string($conn, $_POST['lastname']);
$email = mysqli_real_escape_string($conn, $_POST['email']);
$phone = mysqli_real_escape_string($conn, $_POST['phone']);
$address = mysqli_real_escape_string($conn, $_POST['address']);
$pwd = mysqli_real_escape_string($conn, $_POST['pwd']);

$hoteluserkey = uniqid('', true);

//Error handlers
//Check for empty fields
if (empty($signupdate) || empty($first) || empty($last) || empty($email) || empty($phone) || empty($address) || empty($pwd)) {
    header("Location: ../index.php?signup=empty");
    exit();
} else {
    //Check if input characters are valid
    if (!preg_match("/^[a-zA-Z]*$/", $first) || !preg_match("/^[a-zA-Z]*$/", $last)) {
        header("Location: ../index.php?signup=invalid");
        exit();
    } else {
        //Check if email is valid
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            header("Location: ../index.php?signup=invalidemail");
            exit();
        } else {
                //Hashing the password
                $hashedPwd = password_hash($pwd, PASSWORD_DEFAULT);
                //Insert the user into the database
                $sql = "INSERT INTO hotelusers 
                    (hotelusers_signupdate, hotelusers_hoteluserkey, hotelusers_first, hotelusers_last, hotelusers_email, hotelusers_phone, hotelusers_address, hotelusers_pwd) 
                    VALUES ('$signupdate', '$hoteluserkey', '$first', '$last', '$email', '$phone', '$address', '$hashedPwd');"; 
                mysqli_query($conn, $sql);
                header("Location: ../index.php?signup=success");
                exit();
            }
        }
    }
} else {
header("Location: ../index.php");
exit();
}

此代码有效。 现在问题来了。当有人预订房间时,他们会看到以下输入字段:

<div class="book">
    <p class="main_p_ex">Book a room</p>
    <form class="book" action="includes/book.inc.php" method="post">

        <input type="hidden" name="bookdate" value="<?php echo date("Y-m-d h:i:sa"); ?>">

        <input type="text" name="userkey" placeholder="your key">
        <input type="password" name="pwd" placeholder="password">
        <p>room</p>
        <select name="room">
            <option value="1">one</option>
            <option value="2">two</option>
            <option value="3">three</option>
            <option value="4">four</option>
            <option value="5">five</option>
            <option value="6">six</option>
            <option value="7">seven</option>
            <option value="8">eight</option>
            <option value="9">nine</option>
            <option value="10">ten</option>
        </select>

        <p>from</p>
        <input type="date" name="from" min="<?php echo date("Y-m-d");?>">
        <p>to</p>
        <input type="date" name="to" min="<?php echo date("Y-m-d");?>">

        <textarea name="otherguests" placeholder="full names of all other 
              guests"></textarea>
        <textarea name="comments" placeholder="any comments?"></textarea>
        <button type="submit" name="submit">Book!</button>
    </form>
    </div>

这也很好。 我有这个代码用于将这些输入插入数据库:

<?php 

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

include_once 'dbh.inc.php';

$bookdate = mysqli_real_escape_string($conn, $_POST['bookdate']);
$userkey = mysqli_real_escape_string($conn, $_POST['userkey']);
$pwd = mysqli_real_escape_string($conn, $_POST['pwd']);
$room = mysqli_real_escape_string($conn, $_POST['room']);
$from = mysqli_real_escape_string($conn, $_POST['from']);
$to = mysqli_real_escape_string($conn, $_POST['to']);
$otherguests = mysqli_real_escape_string($conn, $_POST['otherguests']);
$comments = mysqli_real_escape_string($conn, $_POST['comments']);

$bookingkey = uniqid('', true);

//Error handlers
//Check if inputs are empty
if (empty($userkey) || empty($pwd) || empty($room) || empty($from) || empty($to) || empty($pwd)) {
    header("Location: ../index.php?login=empty");
    exit();
} else {
    $sql = "SELECT * FROM hotelusers WHERE hotelusers_hoteluserkey='$userkey'";
    $result = mysqli_query($conn, $sql);
    $resultCheck = mysqli_num_rows($result);
    if ($resultCheck < 1) {
        header("Location: ../index.php?key=error");
        exit();
    } else {
        if ($row = mysqli_fetch_assoc($result)) {
            //De-hashing the password
            $hashedPwdCheck = password_verify($pwd, $row['hotelusers_pwd']);
            if ($hashedPwdCheck == false) {
                header("Location: ../index.php?key=error");
                exit();
            } elseif ($hashedPwdCheck == true){

                $sql = "SELECT * FROM hotelrooms WHERE hotelrooms_id='$room'";
                $result = mysqli_query($conn, $sql);
                $resultCheck = mysqli_num_rows($result);
                if ($resultCheck > 0) {
                    while ($row = mysqli_fetch_assoc($result)) {
                    $roomnew = $row['hotelrooms_name'];         }
                }

                $fromnew = strtotime($from);
                $tonew = strtotime($to);
                $datediff = $tonew - $fromnew;
                $days = round($datediff / 86400);

                $sql = "SELECT * FROM hotelrooms WHERE hotelrooms_id='$room'";
                $result = mysqli_query($conn, $sql);
                $resultCheck = mysqli_num_rows($result);
                if ($resultCheck > 0) {
                    while ($row = mysqli_fetch_assoc($result)) {
                    $pricepd = $row['hotelrooms_price'];        }
                }

                $price = $days * $pricepd;

                echo $roomnew . " -- " . $price  . " -- " . $days . " -- " . $bookdate . " -- " . $userkey . " -- " . $room . " -- " . $otherguests . " -- " . $comments;

                $sql = "INSERT INTO hotelbookings 
                    (hotelbooking_bookingkey, hotelbooking_bookdate, hotelbooking_userkey, hotelbooking_room, hotelbooking_from, hotelbooking_to, hotelbooking_days, hotelbooking_price, hotelbooking_paid, hotelbooking_otherguests, hotelbooking_comments) 
                    VALUES ('$bookingkey', '$bookdate', '$userkey', '$roomnew', '$from', '$to', '$days', '$price', '$otherguests', '$comments');";  
                mysqli_query($conn, $sql);
                //header("Location: ../index.php?booking=success");
                exit();
            }
        }
    }
}   
}   else {
    header("Location: ../index.php?booking=error");
    exit();
}

注意:我禁用了最后一个头函数进行调试。取消评论它不会改变任何东西。还尝试清除浏览器历史记录,cookie和所有这些。什么都行不通。

我在这里缺少什么?

我没有收到任何错误,echo $roomnew . " -- " . $price . " -- " . $days . " -- " . $bookdate . " -- " . $userkey . " -- " . $room . " -- " . $otherguests . " -- " . $comments;工作正常。它只是没有插入任何东西。

1 个答案:

答案 0 :(得分:0)

通过再次制作一切来解决这个问题...我知道有点奇怪