简体   繁体   English

如何将实际的 NULL 值插入到可为空的列中?

[英]How to insert an actual NULL value into a nullable column?

function save($gmt, $name, $address, $phone, $remark)
{
    $query= "INSERT INTO `user` (`gmt`, `name`, `address`, `phone`, `remark`) VALUES ('$gmt', '$name', '$address', '$phone', '$remark')";
    mysql_query($query);
}

Here, address, phone, and remark can be NULL .这里,地址、电话和备注可以是NULL I need it to save NULL whenever the variable is set to NULL and the column is nullable, instead of inserting an empty string.每当变量设置为NULL并且该列可以为空时,我需要它来保存NULL ,而不是插入一个空字符串。

How can I insert NULL value into the database using PHP?如何使用 PHP 将NULL值插入到数据库中?

This is PHP solution, but you have to use mysqli because mysql deprecated, please read more about mysqli .这是 PHP 解决方案,但您必须使用 mysqli,因为 mysql 已弃用,请阅读有关mysqli 的更多信息。 Also, you must consider SQL injection此外,您必须考虑SQL 注入

function save($gmt, $name, $address, $phone, $remark)
{
  if(empty($phone)){
   $phone = 'NULL';
  }else{
   $phone = "'".$phone."'";
  }
  if(empty($remark)){
   $remark = 'NULL';
  }else{
   $remark = "'".$remark."'";
  }
    $query= "INSERT INTO `user` (`gmt`, `name`, `address`, `phone`, `remark`) VALUES ('$gmt', '$name', '$address', $phone, $remark)";
    mysql_query($query);
}
//tests
save("a", "b", "c", "", "")."<br>";
save("a", "b", "c", "d", "")."<br>";
save("a", "b", "c", "d", "e")."<br>";
/*
INSERT INTO `user` (`gmt`, `name`, `address`, `phone`, `remark`) VALUES ('a', 'b', 'c', NULL, NULL)
INSERT INTO `user` (`gmt`, `name`, `address`, `phone`, `remark`) VALUES ('a', 'b', 'c', 'd', NULL)
INSERT INTO `user` (`gmt`, `name`, `address`, `phone`, `remark`) VALUES ('a', 'b', 'c', 'd', 'e')
*/
?>

DEMO演示

Try switching to prepared statements (which as a bonus is less prone to SQL injections).尝试切换到准备好的语句(作为奖励,SQL 注入的可能性较小)。

function save($gmt, $name, $address, $phone, $remark)
{
    if(!isset($phone) || empty($phone)) { $phone = null; }
    if(!isset($remark) || empty($remark) { $remark = null; }

    $db = new PDO(...);

    $stmt = $db->prepare("INSERT INTO `user` (`gmt`, `name`, `address`, `phone`, `remark`) VALUES (:gmt, :name, :address, :phone, :remark)");
    $stmt->bindValue("gmt", $gmt, PDO::PARAM_STR);
    $stmt->bindValue("name", $name, PDO::PARAM_STR);
    $stmt->bindValue("address", $address, PDO::PARAM_STR);
    $stmt->bindValue("phone", $phone, PDO::PARAM_STR);
    $stmt->bindValue("remark", $remark, PDO::PARAM_STR);
    $stmt->execute();
}

This will handle the null values correctly in MySQL这将在 MySQL 中正确处理null

PHP doesn't print NULL - it is just an empty string. PHP 不打印 NULL - 它只是一个空字符串。 So in your example you will try to insert '' , which in SQL again is an empty string.因此,在您的示例中,您将尝试插入'' ,它在 SQL 中又是一个空字符串。

You have to use NULL (without quotes).您必须使用NULL (不带引号)。

And the best practice to achieve that is to use an ORM or a PHP framework with a database abstraction layer which does this for you.实现这一目标的最佳实践是使用 ORM 或 PHP 框架以及为您执行此操作的数据库抽象层。

Using ternary operator, you can also use this使用三元运算符,您还可以使用此

$add = ($address == '' ? NULL : $address);
$phn = ($phone == '' ? NULL : $phone);
$rmk = ($remark == '' ? NULL : $remark);

fields can be NULL字段可以为 NULL

-> qtd_aulas_previstas -> qtd_aulas_previstas

-> qtd_aulas_dadas -> qtd_aulas_dadas

$qtd_aulas_previstas = ( empty($qtd_aulas_previstas) ? 'NULL' : "'".$qtd_aulas_previstas."'");
$qtd_aulas_dadas     = ( empty($qtd_aulas_dadas)     ? 'NULL' : "'".$qtd_aulas_dadas."'");


//insere os dados na tabela
$inserir_tb = mysqli_query($conexao, "INSERT INTO tb_turma_bimestre VALUES('', '$cod_turma', '$cod_bimestre', $qtd_aulas_previstas, $qtd_aulas_dadas, '$data_abertura', '$data_encerramento', '$date_time', '$nome_login')")or die("Error2: " .mysqli_error($conexao));


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM