Solved

Insert Null Date using PHP and SprintF

Posted on 2014-01-23
8
1,451 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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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 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