Jump to content

Problems inserting NULL into database


Icebergness

Recommended Posts

Hi,

 

This one has been doing my head in for hours!.

 

I have a script that, to put it simply, inserts either NULL or a string in to a MySQL database. However, it's acting rather weird with different values.

 

Here is my SQL script...

<?php

$sql = "INSERT INTO stocks (s_isin) VALUES ('$isin')";

mysql_query($sql);

?>

 

And then the variables being fed in to it are...

<?php 
if ($user_isin_dummy == "1")
// ISIN is Dummy
	{
		$isin = "NULL";	
	}

else
// ISIN is live
	{
		$isin = $user_isin;
	}
?>

 

In it's current state, the "else" clause works fine, but the "if" statement inserts the string "NULL" in to the database.

 

If I remove the single quotes from "$isin" in the SQL code, the "else" works only if the string begins with a number. If it starts with a letter, it simply doesn't insert at all. However, the "if" now correctly inserts a null value.

 

How can I make it so that a proper null value is inserted on the "if" statement, and also for any string to be inserted on the else clause?

 

Cheers,

Dave

Link to comment
Share on other sites

Your $sql assumes that there should be quotes around $isin (the general case). Remove those.

$isin may need quotes around its value (the specific case). Add them if it should have them. As in

$isin = "'" . $user_isin . "'";

But $user_isin is a string, right? Not a number? If it is a number then it shouldn't have quotes at all.

Link to comment
Share on other sites

Your $sql assumes that there should be quotes around $isin (the general case). Remove those.

$isin may need quotes around its value (the specific case). Add them if it should have them. As in

$isin = "'" . $user_isin . "'";

But $user_isin is a string, right? Not a number? If it is a number then it shouldn't have quotes at all.

 

Hi Requinix,

 

Thanks for clearing that up for me, makes a lot more sense to me now :) You understood me correctly, yes, and your suggestion worked perfectly, so thank you very much! You've saved me countless hours of smashing my head against a wall!

 

Cheers,

Dave

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.