简体   繁体   English

如何使用 php 将日期从 html 表单插入数据库?

[英]How to insert date from html form to database using php?

I have an HTML form which takes in details of a project and details like start date and deadline, When I tried to push the data from the form to the database, I am getting an error which says that my date field is empty, I am using PHP to push the data.我有一个 HTML 表单,其中包含项目的详细信息以及开始日期和截止日期等详细信息,当我尝试将数据从表单推送到数据库时,我收到一个错误,提示我的日期字段为空,我是使用 PHP 推送数据。 I will give the code for HTML and PHP below.我将在下面给出 HTML 和 PHP 的代码。 I recently started learning to code using PHP and SQL so any tips and advice are welcome.我最近开始学习使用 PHP 和 SQL 进行编码,因此欢迎任何提示和建议。 I have checked other forums but none of them explains how to insert date.我检查了其他论坛,但没有一个解释如何插入日期。

<form action="includes/sale.inc.php" method="POST">
    <input type="text" name="orderno" placeholder="Order No" required="required" />
    <input type="date" name="date" placeholder="Order Placed on" required="required" />
    <input type="text" name="item" placeholder="Description of goods" required="required" />
    <input type="text" name="qty" placeholder="Quantity" required="required" />
    <input type="date" name="ddate" placeholder="Due date" required="required" />
    <input type="text" name="rate" placeholder="Price" required="required" />
    <button type="submit" name="sale_submit" class="btn btn-primary btn-block btn-large">Submit</button>
</form>
<?php

if (isset ($_POST['sale_submit'])) {
    require 'saledbh.inc.php' ;
    $orderno = $_POST['orderno'];
    $dateplaced = date("Y-m-d H:i:s", $_POST['date']);
    $item = $_POST['item'];
    $qty = $_POST['qty'];
    $datedue = date("Y-m-d H:i:s", $_POST['ddate']);
    $price = $_POST ['rate'];

    if (empty($orderno)){
        header("Location: ../soentry.php?error=emptyorder");
        exit();
    }
    elseif (empty($item)){
        header("Location: ../soentry.php?error=emptyitem");
        exit();
    }
    elseif (empty($qty)){
        header("Location: ../soentry.php?error=emptyqty");
        exit();
    }
    elseif (empty($duedate)){
        header("Location: ../soentry.php?error=emptyddate");
        exit();
    }
    elseif (empty($price)){
        header("Location: ../soentry.php?error=emptyprice");
        exit();
    }
    elseif (empty($dateplaced)){
        header("Location: ../soentry.php?error=emptydate");
        exit();
    }
    else {
        $sql = "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = 'salesrecords' AND table_name = ?;";
        $sql1 = "INSERT INTO $orderno (orderno=? ,dplaced=?,item=?,qty=?,ddue=?,price=?);";
        $stmt = mysqli_stmt_init($conn);
        if (!mysqli_stmt_prepare($stmt, $sql)) {
            header("Location: ../soentry.php?error=sqlerror");
            exit();
        }
        else {
            mysqli_stmt_bind_param($stmt, "s",$orderno);
            mysqli_stmt_execute($stmt);
            $result = mysqli_stmt_get_result($stmt);
            if($result1 = mysqli_fetch_assoc($result)){
                if ($result == 0){
                    $sqlcreate= "CREATE TABLE $orderno (
                        ID int(11) AUTO_INCREMENT,
                        orderno varchar(255) NOT NULL,
                        dplaced date NOT NULL,
                        item varchar(255) NOT NULL,
                        qty int(11) NOT NULL,
                        ddue date NOT NULL,
                        price int(255) NOT NULL,
                        PRIMARY KEY (ID)
                        );";
                    }
                else {
                    header("location: ../soentry.php?error=notablecreate");
                }
                }
            else{
                header("location: ../soentry.php?=nofetchassoc");
            }
            }
        }
    }
else{
    header("location: ../soentry.php?error=noice");
    exit();
}
?>

First of all use the isset to check if a post variable is set before assigning it to a variable.首先使用 isset 检查是否设置了 post 变量,然后再将其分配给变量。 This can be done like so:这可以这样做:

PHP 7 + PHP 7 +

$var = $var ?? "default value";

Before PHP 7 PHP 7 之前

$var = isset($var) ? $var : "default value";

This will make sure you have no empty fields, however it will set a default value so in case your post value is indeed empty, the value will be different from the post.这将确保您没有空字段,但是它将设置一个默认值,因此如果您的帖子值确实为空,则该值将与帖子不同。

I used your code in my test environment and copied the exact same code like you have to see if my post date gets through.我在我的测试环境中使用了您的代码并复制了完全相同的代码,就像您必须查看我的发布日期是否通过一样。 For myself it did work and the check if a post is set would prevent you from getting an error.对我自己来说,它确实有效,并且检查是否设置了帖子可以防止您出错。

If you want to troubleshoot more for yourself to see if your date is included in your post or something else might be the issue I suggest putting the next line of code before executing the command:如果您想为自己解决更多问题以查看您的日期是否包含在您的帖子中或其他问题可能是我建议在执行命令之前放置下一行代码的问题:

exit('<pre>' . print_r($_POST,1));

This will give you a clear overview on your posted variables.这将使您对发布的变量有一个清晰的概述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM