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
Solved

Insert Null Date using PHP and SprintF

Posted on 2014-01-23
8
1,345 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Fulfillment API php code sample 1 58
mysql query for sum() 3 28
Redirect 301 from one address  to another 5 25
PHP Echo with auto submit 8 28
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
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…

856 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