Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 尝试将输入类型date插入到类型为date的mysql列时抛出SQL错误_Php_Mysql_Sql_Date - Fatal编程技术网

Php 尝试将输入类型date插入到类型为date的mysql列时抛出SQL错误

Php 尝试将输入类型date插入到类型为date的mysql列时抛出SQL错误,php,mysql,sql,date,Php,Mysql,Sql,Date,我的表单中有两个字段,分别名为开始日期和截止日期,都有输入类型date。我在数据库中插入这两个字段,列类型为date 我的html看起来像: <form class="" action='a.php' method="POST"> <label for="From_date" class="left-text">From Date:</label> <input type="date" name="from_date" maxlength="" i

我的表单中有两个字段,分别名为
开始日期
截止日期
,都有输入类型date。我在数据库中插入这两个字段,列类型为date

我的html看起来像:

<form class="" action='a.php' method="POST">
<label for="From_date" class="left-text">From Date:</label>

<input type="date" name="from_date" maxlength=""   id="fromdate" placeholder="" class="input-xlarge">
<label for="To_date" class="left-text">To Date:</label>

<input type="date" name="to_date" maxlength=""   id="todate" placeholder="" class="input-xlarge">
但这让我犯了一个错误:

无法执行INSERT INTO table(从\u日期到\u日期)值(2015-05-272015-05-15)。您的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,以了解在第1行中使用“From_date,to_date”值(2015-05-272015-05-15)的正确语法

我试图更改表单中的日期格式:

$to_date = date('Y-m-d', strtotime(str_replace('-','/', $to_date)));
$from_date = date('Y-m-d', strtotime(str_replace('-','/', $from_date)));
但这也没有解决问题


问题出在哪里?我无法将从表单收到的日期插入数据库表?

我会执行以下操作:

$from_date = new DateTime($_POST['from_date']);
$to_date = new DateTime($_POST['to_date']);

// Insert the following into the database
$from_date = mysqli_real_escape_string($link, $from_date->format('Y-m-d'));
$to_date = mysqli_real_escape_string($link, $to_date->format('Y-m-d'));

// If you want those slashes
$from_date = mysqli_real_escape_string($link, $from_date->format('Y/m/d'));
$to_date = mysqli_real_escape_string($link, $to_date->format('Y/m/d'));
对于您的查询:

$sql = "INSERT INTO airport_data
(From_date, To_date)
VALUES ('".$from_date."','".$to_date."')
";

一种简单而优雅的方式,可以按您希望的方式输出日期。

(从日期到日期)
@Fred ii-错误地键入了它实际上不在那里YUM。。。我想知道;你用什么MySQL API连接?试着打印出
$sql
变量,看看你得到了什么。@AmmarUlHassan这不是全部。阅读以上我的观点。也是1。你如何提交你的表格?2.您在字段中输入了什么?为什么要执行echo$date->format('Y-m-d');另外,我如何使用mysqli\u real\u escape\u字符串和您的答案来查看输出。这当然不是必需的。这对开发很好,但在生产中使用准备好的语句。感谢编辑Peter,我接受了它,但我可能会删除我的答案。OP的问题不清楚他们的代码是如何/在哪里设置的,包括序列。编辑:我删除了我的答案@彼得
$from_date = new DateTime($_POST['from_date']);
$to_date = new DateTime($_POST['to_date']);

// Insert the following into the database
$from_date = mysqli_real_escape_string($link, $from_date->format('Y-m-d'));
$to_date = mysqli_real_escape_string($link, $to_date->format('Y-m-d'));

// If you want those slashes
$from_date = mysqli_real_escape_string($link, $from_date->format('Y/m/d'));
$to_date = mysqli_real_escape_string($link, $to_date->format('Y/m/d'));
$sql = "INSERT INTO airport_data
(From_date, To_date)
VALUES ('".$from_date."','".$to_date."')
";