Solved

Insert Null Date using PHP and SprintF

Posted on 2014-01-23
8
1,380 Views
Last Modified: 2014-02-02
I am updating a form and have an issue whereby i can't insert a NULL value into a datetime field. The datetime field allows NULL and is NULL by default.

I have tried inserting a NULL directly into the table and that works fine.

I believe that the issue is being caused because I am sending 'NULL' as opposed to NULL.. but i'm not entirely sure how i can do it using the following approach :

<?php

	$enquirysubmitted=NULL;
	$insert_sql = sprintf("INSERT INTO enquiry (enquirysubmitted) VALUES ('%s');", mysql_real_escape_string($enquirysubmitted));
	mysql_query($insert_sql)
	
?>

Open in new window


The reason why i need to be able to insert null is because the date may or may not be needed. For example, someone could enter a date and decide later that it no longer applies.
0
Comment
Question by:maddisoncr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 35

Accepted Solution

by:
gr8gonzo earned 250 total points
ID: 39803224
With sprintf, you're defining a template first, and your template has quotes around your value, so the value will ALWAYS have quotes around it:

...VALUES ('%s');

If you want to be able to insert a NULL, you need to change your template so it doesn't have quotes:

...VALUES (%s);

and then you need to update your sprintf so that you add quotes when the value isn't null:

$insert_sql = sprintf("INSERT INTO enquiry (enquirysubmitted) VALUES (%s);", ($enquirysubmitted === null ? "NULL" : "'".mysql_real_escape_string($enquirysubmitted)."'"));
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39803387
I've never used or seen a DATETIME column that was null.  Why not make it like this?

NOT NULL DEFAULT 0000-00-00 00:00:00

In unrelated news, this.
0
 

Author Comment

by:maddisoncr
ID: 39804153
thank you to both of you

i will have a look at the solution now


as for leaving the date as 0000-00....

when you use that approach, i presume you just test for a 0 date when displaying the form and hide it ?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39804287
Yes, I always know that a zero date means there is no information.  It makes this easy to sort.
0
 

Author Comment

by:maddisoncr
ID: 39805906
absolutely, i will use that approach

i will also beging replacing mysql :-/
0
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 39806214
If you choose MySQLi as the MySQL replacement and you use the object-oriented notation you will find the replacement goes smoothly.  You can connect to the MySQL extension and the MySQLi extension at the same time and convert the queries one at a time from the obsolete extension to the MySQLi extension.  If you choose the procedural MySQLi it may be more work since the function calls now put the arguments in a different order than MySQL.  If you choose PDO you have more work to do.  Potentially a lot more work because all of the query strings are different.  It's quite an exercise!
0
 

Author Comment

by:maddisoncr
ID: 39828293
Thanks for your help guys

The sprintf information was exactly what i asked for.. thank you

i have decided though to replace all mysql with PDO.. thankfully, it's done and now working !
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39828301
Thanks for the points and thanks for using EE, ~Ray
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP: Best way to scan folders and process files 10 61
Add Logo to Bookmark and Tab Headings in Browser 3 32
How to get this library to work load? 8 40
xml with php question 5 37
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question