将用户输入的日期,时间和外键值添加到MySQL数据库? [英] Adding date, time and foreign key values from user input into mysql db?

查看:213
本文介绍了将用户输入的日期,时间和外键值添加到MySQL数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的代码,应该添加用户输入数据库,我不明白为什么不添加到数据库,表中的电子邮件字段是一个外键引用到另一个表,我正在使用会议存储电子邮件在$电子邮件,并保存到数据库时,用户保存数据,我也接受日期和时间从用户输入,这是完全按照数据库格式,但它仍然不保存,我已经尝试进入静态数据,也不工作。我错过了什么?

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);
    }


推荐答案

最初的问题,我不认为连接是问题,这个问题很可能发生在INSERT查询期间。您是否尝试过从phpMyAdmin运行查询来排除PHP之外的查询语法?

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?

这篇关于将用户输入的日期,时间和外键值添加到MySQL数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆