datepicker没有发布到mysql
I have tried most if not all of the suggestions on this forum, as well as a few others, but I'm still getting 0000-00-00 in MySQL DB even though it echos back correct format.
Here is the PHP code:
<?php
include_once("includes/form_functions.php");
// START FORM PROCESSING
if (isset($_POST['submit'])) { // Form has been submitted.
$errors = array();
// perform validations on the form data
$required_fields = array('location', 'date');
$errors = array_merge($errors, check_required_fields($required_fields, $_POST));
$location = trim(mysql_prep($_POST['location']));
$date = mysql_prep($_POST['date']);
$adult = mysql_prep($_POST['adult']);
$child = mysql_prep($_POST['children']);
$guest = mysql_prep($_POST['guest']);
$hg = mysql_prep($_POST['holyghost']);
$baptism = mysql_prep($_POST['baptism']);
if ( empty($errors) ) {
$query = "INSERT INTO attendance (
location, date, adult, children, guest, holyghost, baptism
) VALUES (
'{$location}', {$date}, {$adult}, {$child}, {$guest}, {$hg}, {$baptism}
)";
$result = mysql_query($query, $connection);
if ($result) {
$message = "submission completed.";
} elseif
($message = "This form cannot be submitted at this time.");
$message .= "<br />" . mysql_error();
}
}
?>
Here is the datepicker code:
<head>
<link rel="stylesheet" href="stylesheets/jquery-ui.css" />
<script src="javascripts/jquery-ui-1.10.2/jquery-1.9.1.js"></script>
<script src="javascripts/jquery-ui-1.10.2/ui/jquery-ui.js"></script>
<script type="text/javascript">
$(function() {
$( "#datepicker" ).datepicker({
dateFormat: "yy-mm-dd"
});
});
</script>
</head>
Any Help would be appreciated.
I think you need to use single code(') in your insert statement for date to fix this issue like below..
INSERT INTO attendance (
location, date, adult, children, guest, holyghost, baptism
) VALUES (
'{$location}', '{$date}', '{$adult}', '{$child}', '{$guest}', '{$hg}', '{$baptism}'
);
Always use single code(') & mysql_escape_string() function while passing value in MySQL
- Use MySQL's STR_TO_DATE() function to convert the string:
INSERT INTO user_date VALUES ('', '$name', STR_TO_DATE('$date', '%m/%d/%Y'))
-
Convert the string received from jQuery into a PHP timestamp—e.g. using strtotime():
$timestamp = strtotime($_POST['_date']);
and then either:
format the timestamp using date():
$date = date('Y-m-d', $timestamp);
pass the timestamp directly to MySQL using FROM_UNIXTIME():
INSERT INTO user_date VALUES ('', '$name', FROM_UNIXTIME($timestamp))
i think is based on mysql db column type. you need to change $date value like that
`$explode_Date=explode("-",$date);`
$date=date("d-m-Y, mktime(0,0,0,$explode_Date[1],$explode_Date[0],$explode_Date[2]));
then try