0

On my page I have a few textboxes and two of those, first name and last name, are not required. If nothing is entered in one or both of those fields I need to insert NULL as nothing rather than NULL as a string value. Currently it is entering them as string values. What do I need to do so it inserts NULL values instead of a string?

PHP Code

<?php
// SQL connection and other variables...

if (!empty($_POST['firstname'])) {
    $firstname = $_POST['firstname'];
} else {
    $firstname = 'NULL';
}
if (!empty($_POST['lastname'])) {
    $lastname = $_POST['lastname'];
} else {
    $lastname = 'NULL';
}

$sp = "exec sp_test_proc '$street1', '$street2', '$firstname', '$lastname'";
$res = odbc_exec($conn, $sp);
?>

If I pass in NULL instead of the variable for either of the parameters it works fine. Not sure what is causing the issue by using a variable.

Brian
  • 1,184
  • 2
  • 21
  • 38

2 Answers2

3

Write NULL instead of 'NULL'

You're making it a string by using quotes.

Jessica
  • 7,075
  • 28
  • 39
1

You need to pass NULL as unquoted string. This is because you are building plain SQL. So make sure the statement is valid one.

For parameterized query it should be NULL value instead of NULL string.

Shiplu Mokaddim
  • 56,364
  • 17
  • 141
  • 187
  • Not to be rude but you're saying the same thing. For plain SQL OR a prepare statement with parameters you would use `NULL` not `'NULL'` – Jessica Jun 17 '13 at 20:24
  • Well I meant null value = `$var=null;` and null string = `$var='null';`. But I agree there was no formatting as its quite hard in cell phone. – Shiplu Mokaddim Jun 17 '13 at 22:29
  • It has nothing to do with the formatting - you are saying do X for plain SQL and do X for parameter. My point is it doesn't matter which he is doing, because both require X. Your answer is confusing because of that. – Jessica Jun 18 '13 at 01:30