php jquery datepicker mysql 日期格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19223370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 19:03:21  来源:igfitidea点击:

Jquery datepicker mysql date format

phpdatepicker

提问by user2075811

I know there are some answers in this topic but i'm fresh and can't manage to do it I have insered Jquery UI DatePicker into PHP code but when I'm sending data to MySql I get this Error: Incorrect date value: '' for column 'date' at row 1

我知道这个主题中有一些答案,但我很新鲜,无法做到我已经将 Jquery UI DatePicker 插入到 PHP 代码中,但是当我将数据发送到 MySql 时,我收到此错误: Incorrect date value: '' for column 'date' at row 1

<html>
<head>

<script>
    $(document).ready(function() {
    $("#datepicker").datepicker({
        dateFormat: 'yy-mm-dd'
        });
    })
</script>
</head>

</body>
<form action="query_insert.php" method="post">
<input type="text" id="datepicker">
<body>

query_insert.php:

query_insert.php:

<?php
$con = mysqli_connect("127.0.0.1", "root", "user", "pass");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO expenses (date) VALUES ('$_POST[datepicker]'
?> 

what I have to change to make it work?

我必须改变什么才能使它工作?

回答by C?????N????

You've got multiple things wrong with your html..

你的 html 有很多问题。

</body>
<form action="query_insert.php" method="post">
<input type="text" id="datepicker">
<body>

should be

应该

<body>
<form action="query_insert.php" method="post">
<input type="text" id="datepicker" name="datepicker">
<input type="submit" value="INSERT DATE">
</form>
</body>

Basically, you have to add a nameattribute to the inputfield for it to be included in _POSTand _GETrequests, you should also close the form(but this wont stop it from being posted)

基本上,你有一个添加name属性的input字段,它被包含在_POST_GET请求,你也应该关闭form(但不会被停止发布它)

also, your bodytags were the wrong way around.

此外,你的body标签是错误的方式。

in your query_insert.phpfile you need to finish your INSERT statement:

在您的query_insert.php文件中,您需要完成 INSERT 语句:

$sql="INSERT INTO expenses (date) VALUES ('$_POST[datepicker]'

should be

应该

$sql="INSERT INTO expenses (date) VALUES ('" . $_POST['datepicker'] . "')";

Warning: I would make sure you validate the $_POST['datepicker']before inserting it though

警告:我会确保$_POST['datepicker']在插入之前验证

回答by bysey

dateFormat: 'yy-mm-dd', (OKE)

php function;

php函数;

$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

$theValue = ($theValue != "") ?“'”。$theValue 。“'“ : “空值”;

input -> 2013-10-6

输入 -> 2013-10-6

output -> '2013-10-6'

输出 -> '2013-10-6'

Mysql insert => '2013-10-6'

Mysql 插入 => '2013-10-6'

and we are done..

我们完成了..

回答by user2842104

Here you can find more information:

您可以在这里找到更多信息:

PHP mysql insert date format

PHP mysql 插入日期格式

But you can try

但是你可以试试

    $sql="INSERT INTO expenses (date) VALUES ( date('yyy-mm-dd','$_POST[date]'))