0

I searched almost whole internet, but could not find something similar, yet my question looks so simple.

I have a php code like so:

$id = 1;    
if (!isset($_POST['port1'])) {
        $port1 = null;
    }

... which simply checks if submitted value is empty or not, if it is empty, then variable $port1 = null;

then, further in the code, I need to insert this value/update it in the database

$sql_update = ("UPDATE names_10 SET `digName1`=$port1 WHERE `device_id`='$id'");

...which should set the "digname1" to null. but it won't! I tried every combination, every type of quotes, but every time I got UPDATE error..

any ideas?

15
  • 4
    null in a string in php is not the same as NULL as a value in a query. I think you should solve this by using parameters rather than munging the string. Commented Apr 5, 2017 at 10:44
  • stackoverflow.com/questions/9314353/set-value-to-null-in-mysql Commented Apr 5, 2017 at 10:45
  • @GordonLinoff What do you mean by "using parameters"? I need to use this variable Commented Apr 5, 2017 at 10:45
  • @SahilGulati no, this will insert null as string Commented Apr 5, 2017 at 10:46
  • update your error Commented Apr 5, 2017 at 10:47

1 Answer 1

1

Try this:

$id = 1;    
if (!isset($_POST['port1'])) {
        $port1 = "NULL";
}

$sql_update = ("UPDATE names_10 SET `digName1`= $port1 WHERE `device_id`='$id'");

I would rather suggest you to use PDO when you plan to bind something like this. There are a lot of benefits using PDO that would amaze you!

Sign up to request clarification or add additional context in comments.

7 Comments

Okay, i will read about it. And try What? Looks identical to my code ...
case sensitive 'NULL' (that is string and not NULL)
Okay, i will try
Do you want null as a string in your database?
@SatishSaini boom, after you updated the answer i tried... Now it works!!!!!! Thanks!!!!
|

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.