Adding date, time and foreign key values from user

2019-09-20 15:44发布

I have the below code that should add user input into the db, I can't understand why its not adding to db, the email field in the table is a foreign key that references to another table, and I'm using session to store email in the $email and save it to db when user saves data, also I'm accepting date and time from user input which is exactly as per the db format but it still doesn't save, I have tried entering static data as well, not working either. Am I missing something ?

    $server = "localhost";
    $user   = "root";
    $pwd    = "";
    $sql_db = "cabcustomers";
    $email = $_SESSION['sesName'];

$conn = @mysqli_connect($server,$user,$pwd,$sql_db);

    if (isset ($_POST["name"]) && isset ($_POST["contact"]) && isset ($_POST["unitno"]) && isset ($_POST["streetno"]) && isset ($_POST["streetname"]) && isset ($_POST["suburb"]) && isset ($_POST["destsuburb"]) && isset ($_POST["pickdt"]) && isset ($_POST["picktime"]))
    {
        $name = $_POST["name"];
        $contact = $_POST["contact"];
        $unitno = $_POST["unitno"];
        $streetno = $_POST["streetno"];
        $streetname =  $_POST["streetname"];
        $suburb =  $_POST["suburb"];
        $destsuburb = $_POST["destsuburb"];
        $pickdt = $_POST["pickdt"];
        $picktime = $_POST["picktime"];

        if(empty($name) || empty($contact) || empty($unitno) || empty($streetno) || empty($streetname) || empty($suburb) || empty($destsuburb) || empty($pickdt) || empty($picktime))
        {
            echo "<p>ONE OR MORE OF FIELDS HAVE MISSING INFORMATION, KINDLY CHECK AND TRY AGAIN!</p>";
        }
        elseif (!is_numeric($contact))
        {
            echo "<p>CONTACT NUMBER MUST BE NUMERIC!</p>";
        }
        else
        {
            $idlen = 7;
            $bookingid = uniqid (rand(), true);
            $bookingid = "BK" . substr($bookingid, 0, $idlen);
            $status = "unassigned";
            $pickdt = $pickdt . " " . $picktime;
            $query = "insert into bookings (bookingid, pname, contact, unitno, streetno, streetname, suburb, destsuburb, pickupdt, bookingdt, status, email) values ('$bookingid', '$name', '$contact', '$unitno', '$streetno', '$streetname', '$suburb', '$destsuburb','$pickdt', 'NOW()','$status', '$email');";
            echo $email;
            $result = mysqli_query($conn, $query);
            echo $result;
            echo "<p>INFORMATION SAVED</p>";
        }
        mysqli_close($conn);
    }

1条回答
Bombasti
2楼-- · 2019-09-20 16:16

Based on the comments after your initial question, I don't think the connection is the problem, the problem is most likely happening during the INSERT query. Have you tried running the query from phpMyAdmin to troubleshoot the syntax of the query outside of PHP?

查看更多
登录 后发表回答