How can i pass a “NULL” or “String” in a sql query.
For example (city is optional and NULL can be used in mysql)
<?php
$var = !empty($_POST['city']) ? $_POST['city'] : NULL;
$query = mysql_query("INSERT INTO table VALUES('".$_POST['firstname']."','".$var."')");
?>
This is not working, beacuse NULL and ‘NULL'(as String) are not the same for mysql.
Don’t surround NULL with quotes in the query:
Now, when
$_POST['city']is not specified, your query will look something like this: