0

I need to do a query like:

  UPDATE screening
                 SET maileddate = NOW(),
                     reference = '{$reference[$user_id]}'
                     WHERE user_id IN(....)

And I want to do the judgement, when reference[$user_id] is empty, reference[$user_id] = NULL. My code is :

    if(empty($reference[$user_id]) || $reference[$user_id] == ''){
        $reference[$user_id] = NULL;
     }

But when I execute the query, if $reference[$user_id] is empty, the value of reference in database is empty but not null. What should I do to make it null? thanks

1
  • 2
    The last part is unclear. Could you say it another way? Commented Sep 23, 2011 at 20:13

5 Answers 5

2

You may need to pass NULL as a string to MySQL if the variable is empty. Use a different variable to hold the possibly NULL contents and quote the non-null contents. Don't forget to escape it otherwise:

$refid = empty($reference['user_id']) ? "NULL" : "'" . mysql_real_escape_string($reference['user_id']) . "'";

UPDATE screening SET maileddate = NOW(), reference = '{$refid}'
      WHERE user_id IN(....)
Sign up to request clarification or add additional context in comments.

Comments

0

Just make it a string saying null. If it's null $reference[$user_id] = 'null';

Oh, also with the query you should be using reference IS null {$reference[$user_id]} instead of the equals sign

3 Comments

I got the string value 'null' in database, not NULL
Ohhh, that changes everything. Then just do the if statement to say if it's null set it to 'null' then keep `reference = {$reference[$user_id]}'
wait, now I'm confused... do you want to save the string 'null' into the db or save the value NULL into the db?
0
$reference = 'reference = ';
$reference .= ' empty($reference[$user_id])  ? 'NULL' : "'". mysql_real_escape_string($reference[$user_id]) . "'";

$query = "UPDATE screening
      SET maileddate = NOW(),
      $reference       
      WHERE user_id IN(....)";

Comments

0

Without seeing the rest of your code, my guess is it has something to do with reference = '{$reference[$user_id]}' having {$reference[$user_id]} in single quotes. MySQL is going to see whatever is in there as what should be in the database. So if $reference[$user_id] prints out as nothing (because it's NULL), that bit of your query will be reference = '' rather than reference = NULL. You need to use the keyword NULL rather than the actual value NULL for the variable you use in the query.

As an example:

$query = "UPDATE screening " . 
             "SET maileddate = NOW(), " . 
                 "reference = " . ($reference[$user_id] === NULL ? 'NULL ' : "'{$reference[$user_id]}' ") . 
                 "WHERE user_id IN(....)";

2 Comments

Could you give me an example of what to do?
Edited my answer with an example
-1

Try this:

$user_id = empty($reference[$user_id]) ? 'NULL' : $reference[$user_id];
$sql = "UPDATE screening
                 SET maileddate = NOW(),
                     reference = " . $user_id . "
                     WHERE user_id IN(....)";

1 Comment

This will set the reference column to an empty string rather than NULL

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.