Jump to content

Recommended Posts

Hi,

 

SOmething strange seems to be happening when I try to run this query. I want to insert NULL as the customer_id, however the following query inserts '0'

 


$customer_id=NULL;

$query="INSERT INTO tasks (task_id,task,customer_id) VALUES ('','$task','$customer_id')

 

The column has NULL set as the default.

 

If I replace the variable $customer_id with NULL in my SQL it works fine.

 

I'm not sure whats going on. Any clues?

 

Link to comment
https://forums.phpfreaks.com/topic/193471-problem-working-with-null/
Share on other sites

When you assign a variable to null it is actually "false" try just using NULL in your query? or make your variable a string NULL

 

$customer_id='NULL';

$query="INSERT INTO tasks (task_id,task,customer_id) VALUES ('','$task','$customer_id');

 

OR

 

$query="INSERT INTO tasks (task_id,task,customer_id) VALUES ('','$task','NULL')

'NULL' is a string containing the characters N, U, L, and L.

 

NULL (no quotes) is the keyword meaning a null value. Remove the single-quotes from around the value or if your intent is to get the database to use the default value for the column, one way would be to simply not list it in the columns/values -

$query="INSERT INTO tasks (task_id,task) VALUES ('','$task')";

Thanks,

 

Except I should mention that the customer_id is not always NULL. Only in certain cases. Which means I need to assign NULL to the $customer_id variable rather than directly to the SQL.

 

The methods you've both offered work, however what I really need to do is assign NULL to the variable.

 

Any more ideas?

 

Thanks

Exactly what's described, just modify how you write the SQL...

 

$query="INSERT INTO tasks (task_id,task,customer_id) VALUES ('','$task',".(($customer_id == NULL) ? $customer_id : "'$customer_id'").')';

 

[edit]Changed the code as I really screwed it up[/edit]

At some point in your script you you are making a decision to set the vlaue to null before you execur, correct? Just do something like this

 

  
$customer_id_sql = ($a==$b) ? "'{$customer_id}'" : 'NULL';
  
$query="INSERT INTO tasks (task_id,task,customer_id) VALUES ('', '{$task}', {$customer_id_sql})";
  

 

Just replace $a==$b with whatever condition you are using to determine whether you want a null value or not.

Here's the SQL to create the table if it helps.

 

CREATE TABLE `tasks` (
  `task_id` int(10) NOT NULL auto_increment,
  `task` varchar(1024) NOT NULL,
  `customer_id` int(10) default NULL,
  `order_id` int(10) default NULL,
  `assigned_to` int(2) NOT NULL,
  `assigned_by` int(2) NOT NULL,
  `task_added` datetime NOT NULL,
  `due_date` datetime NOT NULL,
  `completed_date` datetime default NULL,
  `task_type_id` int(2) NOT NULL,
  PRIMARY KEY  (`task_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=17 ;

 

I'm not retrieving and displaying the value. Just took a peek in PHPmyadmin and saw that '0' was being inserted instead of NULL.

 

Based on your table definition, any query that does not supply values for the NOT NULL columns will fail (just tested), unless you use the IGNORE keyword or strict mode is off, in which case NULL's will be inserted into the default NULL columns and zero values will be inserted into the NOT NULL columns (just tested as well.) All the queries posted in this thread are likely failing and any rows you have that are showing you a zero value for the customer_id were probably already in your table from previous testing.

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.