2

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

<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:

<?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?

avrahamcool
  • 13,888
  • 5
  • 47
  • 58
user2075811
  • 17
  • 1
  • 2
  • 4

3 Answers3

2

You've got multiple things wrong with your 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 name attribute to the input field for it to be included in _POST and _GET requests, you should also close the form (but this wont stop it from being posted)

also, your body tags were the wrong way around.

in your query_insert.php file you need to finish your INSERT statement:

$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

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

php function;

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

input -> 2013-10-6

output -> '2013-10-6'

Mysql insert => '2013-10-6'

and we are done..

bysey
  • 3
  • 2
-1

Here you can find more information:

PHP mysql insert date format

But you can try

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