日期未发送到mysql

日期未发送到mysql

本文介绍了日期未发送到mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到无处不在,但似乎找不到正确的代码。我有一个html表单从用户使用日期输入的日期。



index.html:

 < form name =myformaction =process.phpmethod =POST> 
日期:< input type =datename =date>< br>
< input type =submitvalue =Submit>
< / form>

Process.php:

 $ link = mysqli_connect(my.sql.server,username,psswd,database_name)
$ date = $ _POST ['date'];
mysqli_query($ link,INSERT INTO visit_exclude_dates(date)VALUES(CAST $ date AS DATE));

从表单中输出$ date的值为yyyy-mm-dd格式,我能够成功写入数据库。我的问题是当我读数据库时,日期显示为0000-00-00。所以我假设这个问题是这样的:

  mysqli_query($ link,INSERT INTO visit_exclude_dates(date)VALUES $ date AS DATE)); 

编辑:
我想要注意的是visit_exclude_dates是我的表的名称


解决方案

尝试在查询中的date字段周围添加tick。如果您确定输入是Ymd格式,那么很可能会解决您的问题:

  $ date = date YMD,strtotime($ _ POST ['date'])); 
$ result = mysqli_query($ link,INSERT INTO visit_exclude_dates(date)VALUES('{$ date}'));

if(!$ result){
echoError:。 $ link->错误);
die();
}

$ result-> close();
$ link-> close();


I have looked everywhere, but I can't seem to find the correct code. I have an html form take in a date from the user using the "date" input.

index.html:

<form name="myform" action="process.php" method="POST">
     Date: <input type="date" name="date"><br>
     <input type="submit" value="Submit">
</form>

Process.php:

$link=mysqli_connect("my.sql.server", "username", "psswd", "database_name")
$date = $_POST['date'];
mysqli_query($link,"INSERT INTO visit_exclude_dates (date) VALUES (CAST $date AS DATE)");

the value of $date when it comes out of the form is in the yyyy-mm-dd format, and I am able to successfully write to the database. My problem is that when I read the database, the date is being shown as 0000-00-00. So I assume that the problem is with this line:

mysqli_query($link,"INSERT INTO visit_exclude_dates (date) VALUES (CAST $date AS DATE)");

EDIT:I would like to note that visit_exclude_dates is the name of my table

解决方案

Try adding ticks around the date field in your query. Provided you're SURE the input is Y-m-d format, It'll most likely fix your issue:

$date = date("Y-m-d", strtotime($_POST['date']));
$result = mysqli_query($link,"INSERT INTO visit_exclude_dates (date) VALUES ('{$date}')");

if(!$result) {
    echo "Error: " . $link->error);
    die();
}

$result->close();
$link->close();

这篇关于日期未发送到mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 05:18