PHP MySQL Insert Date Format Mismatch
When inserting dates into a MySQL database using PHP, it's crucial to ensure the date format matches the expectations of the database.
MySQL recognizes dates in specific formats, including 'YYYY-MM-DD' or 'YY-MM-DD'. However, when inserting a date such as '08/25/2012' using the following code:
mysql_query("INSERT INTO user_date VALUE( '', '$name', '$date')")
the database inserts '0000-00-00 00 00 00'. This happens because MySQL interprets the input as a string, not a date literal.
Solution Options
There are several options for resolving this date format issue:
Warning
Before proceeding, ensure that your code is protected against SQL injection and consider using prepared statements instead of the deprecated mysql_* functions. Additionally, consider using the DATE type for storing dates without time information.
The above is the detailed content of How Can I Fix PHP MySQL Date Insertion Format Mismatches?. For more information, please follow other related articles on the PHP Chinese website!