I am having problems inserting times in to a mysql database. I can enter a time through cpanel without problems so i know the database is ok. My problem is when using a php form,code is below: insert.php mysql_select_db("mydatabase_name", $con); $sql="INSERT INTO db (event ,location ,town ,date ,time ,text ,type ,) VALUES ('$_POST[event]','$_POST[location]','$_POST[town]','$_POST[date]','$_POST[time]','$_POST[text]','$_POST[type]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); Code (markup): insert.html: <html> <body> <form action="insert.php" method="post"> event: <input type="text" name="event" /> location: <input type="text" name="location" /> town: <input type="text" name="town" /> date: <input type="text" name="date" /> time: <input type="text" name="time" /> text: <input type="text" name="text" /> type: <input type="text" name="type" /> <input type="submit" /> </form> </body> </html> Code (markup): This is the error I get: Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'text ,type ,) VALUES ('smith','church','manchester','02-09-2007','18' at line 1
Mysql date type is Y-m-d, you have to convert form data_field into correct format. $new_date_array = split("-", $_POST['date']); // current date is d-m-Y $new_date = "{$new_date_array[2]}-{$new_date_array[1]}-{$new_date_array[0]}"; PHP: You can now safely insert $new_date in your table.
Could someone show me the code inserted in to my code. I cant get it to work ? I also imputed the date as yyyy/mm/dd and it still chocked...
the problem is "comma" after fieldname type: " ,text ,type ,)" delete that comma (,) or write a field name after that.