问题
I've done heaps of research on this topic but for some reason nothings working!
I want to use the datepicker widget to update a row in MySQL (which has a date column). For some reason, whenever I try to update the row (which works for other input things) it says in my MySQL table 0000-00-00 ??
This is my jQuery code:
<script>
$( function() {
$( "#datepicker" ).datepicker({
dateFormat:"yy-mm-dd",
altField: "#alternate",
altFormat: "DD, d MM, yy"
});
$( "#selectable" ).selectable();
} );
</script>
And my HTML:
<p> Date: <input name="slot_date" id="datepicker" > <input type="text" id="alternate" size="30"></p>
My PHP:
if( isset($_POST['btn-book']) ) {
$slot_date = date('yy-mm-dd',strtotime($_POST['slot_date']));
$slot_line=$_POST['slot_line'];
$reason=$_POST['reason'];
$id=$_POST['id'];
$sql="UPDATE appointments SET slot_date='$slot_date', slot_line='$slot_line', reason='$reason' WHERE id='$id'";
$result=mysql_query($sql);
if($result){
header ("Location: homepage_loggedin_book_success.php");
}else {
echo "ERROR";
}
}
Anyway, i've been trying for ages but the datepicker wont enter into my MySql table??
回答1:
Use strtotime() and date() to enforce your desired format.
$slot_date = date('Y-m-d',strtotime($slot_date)); # will output 2016-08-16
$sql="UPDATE appointments SET slot_date='$slot_date', slot_line='$slot_line', reason='$reason' WHERE id='$id'";
Your code may be prone to SQL injections. Please refer to http://php.net/manual/en/security.database.sql-injection.php.
回答2:
In the original form which created the record, is everything being set correctly? Are the slot_line and reason fields updating correctly? If they aren't the problem could be in your PHP. Could you post more of the PHP code for context?
来源:https://stackoverflow.com/questions/38964225/jquery-datepicker-in-html-form-to-update-mysql-rows