Skip to content
Advertisement

Insert NULL to mySQL-DB instead of 0000-00-00 using PHP Class/function

I have a little problem with my PHP-class/function which adds data to my mySQL DB. There is a form in html/php which calls a function on submit to insert data into the DB. Inside the form there is a date-field which is optional to fill out.

When I now pass the form data to my function to insert to the database and the user entered no date, mySQL always inserts “0000-00-00” to the date-field (pt_duedate) instead of NULL. But in case user does not provide a duedate, I want mysql to insert NULL. If user provides a date, I want mysql to insert that date.

My current workaround is to fire a second query to set pt_duedate=NULL where pt_duedate=’0000-00-00′ but I don’t think thats a good way to do this.

I read several topics here and also on Google but could’t solve my problem.

Hope you guys have any ideas/hints for me.

JavaScript
JavaScript

Thanks a lot in advance! 🙂

Advertisement

Answer

Check if the field is empty and bind to null instead.

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement