Solved

Insert Null Date using PHP and SprintF

Posted on 2014-01-23
8
1,317 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
  • 4
  • 3
8 Comments
 
LVL 34

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 109

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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 109

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 109

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 109

Expert Comment

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

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Wordpress Plugin - Search From Action to Another Plugin file 13 47
paypal ipn url 5 56
maybe no no httpd.conf 6 46
Eloquent ORM manual paginator defaults to simple 2 21
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

776 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