Adding date, time and foreign key values from user input into mysql db?

喜你入骨 提交于 2019-12-13 08:56:03

问题


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:


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?



来源:https://stackoverflow.com/questions/46362395/adding-date-time-and-foreign-key-values-from-user-input-into-mysql-db

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!