Code: Select all
$x = NULL;
$query = "INSERT INTO table_name (x_col) VALUES ('$x')";
$result = @mysql_query($query);P.S. - x_col is DEFAULT NULL
Moderator: General Moderators
Code: Select all
$x = NULL;
$query = "INSERT INTO table_name (x_col) VALUES ('$x')";
$result = @mysql_query($query);Code: Select all
$x = 'NULL';
$query = "INSERT INTO table_name (x_col) VALUES ($x)";
$result = mysql_query($query);Code: Select all
<?php
foreach ($row as $col => $val) {
if ($val == '0') {
$replacementRow[$col] = NULL;
} else {
$replacementRow[$col] = $val;
}
}
?>Code: Select all
<?php
if ($x !== NULL) {
$x = '\'' . $x . '\'';
} else {
$x = 'NULL';
}
?>Code: Select all
if (!empty($_POST['last_name']))
$ln = $_POST['last_name'];
else
$ln = NULL;
$query = "INSERT INTO table_name (last_name) VALUES ('$ln')";
$result = @mysql_query($query);Code: Select all
if (!empty($_POST['last_name']))
$ln = $_POST['last_name'];
else
$ln = NULL;
$query = "INSERT INTO table_name (last_name) VALUES ('$ln')";
$result = @mysql_query($query);I think there was a miscommunication with some of this earlier... when you say $x = NULL, you are say that the variable is null, not that you are trying to make the field value = NULL. saying that $x = 'NULL' actually makes the insert query equalJenk wrote:To insert NULL into the DB, you need to enter the SQL syntax for NULL, which is (string) 'NULL'
Also, don't use the suppress operator ('@') when developing, errors are a devlopers best friend. Handle them appropraitely, don't hide themCode: Select all
$x = 'NULL'; $query = "INSERT INTO table_name (x_col) VALUES ($x)"; $result = mysql_query($query);
Code: Select all
$query = "INSERT INTO table_name (x_col) VALUES (NULL)";Code: Select all
$x = 'NULL';
$query = "INSERT INTO table_name (x_col) VALUES ('$x')";
// written out is..
// $query = "INSERT INTO table_name (x_col) VALUES ('NULL')";
$result = mysql_query($query);I think this should be...seodevhead wrote:Hey Jenk,
Sorry about that... $ln was upposed to be the value in the INSERT. The correct code I use is:Code: Select all
if (!empty($_POST['last_name'])) $ln = $_POST['last_name']; else $ln = NULL; $query = "INSERT INTO table_name (last_name) VALUES ('$ln')"; $result = @mysql_query($query);
Code: Select all
if (!empty($_POST['last_name']))
$ln = "'".$_POST['last_name']."'"; // setting $ln to equal the value of $_POST['last_name'] with single quotes (') around it. ('value')
else
$ln = 'NULL';
$query = "INSERT INTO table_name (last_name) VALUES ($ln)"; // remove the single quotes around $ln, otherwise the $ln having a value of NULL would be put into the database as the string "NULL" and not having a NULL value itself.
// plus, having the single quotes around the POST variable allows you to not require an if statement on having the single quotes in the query as well.
$result = mysql_query($query);