简体   繁体   English

有条件的SQL用于CASE更新

[英]SQL conditional for a CASE update

I am updating a form in php/SQL. 我正在更新php / SQL中的表单。 The form successfully updates MYSQL, but it deletes any empty fields. 表单成功更新了MYSQL,但删除了所有空字段。 I wanted to try a CASE statement that updates based on whether the form has an entry. 我想尝试一个CASE语句,该语句根据表单是否具有条目进行更新。

CASE
WHEN (variable is set/is not null?) UPDATE `sales` SET `company` ='$company'
END CASE

I can't find a way to check if a variable is NULL. 我找不到检查变量是否为NULL的方法。 Is that possible? 那可能吗? What is the syntax? 语法是什么?

//extra notes that are probably unnecessary Below is information about what I was doing in php. //可能没有必要的额外注释下面是有关我在php中所做的信息。 This isn't relevant to the question per se, but I have seen that people prefer to get more information. 这与问题本身无关,但是我已经看到人们喜欢获得更多信息。 The form submits to a class and constructor.I was looking into dependency injected classes, but that seems like overkill. 表单提交给一个类和构造函数。我一直在研究依赖注入类,但是这似乎太过分了。

<?php
echo "<html>
<form method='POST'>        
<fieldset>              
<input id='company' name='company' type='text' class='form-control' placeholder='". $company. "'>";
</fieldset>
</form> 
</html>
?>

The post: 帖子:

<?php
if(isset ($_POST ["company"] )){
require 'updatereq.php';
//$update is the constructor
$update= new Salesupdateentry (($_POST ["company"]));
//update is just the SQL update in a php function, which works, but deletes blanks    
$update->__update();
?>

//please note- I have slimmed down to one variable, but there are actually 7. I was trying to make it more readable, but a simple if (isset($_POST["company"])) won't work, because there are 6 other POSTS- that have a constructor that need to POST. ///请注意,我已经缩小为一个变量,但实际上有7个变量。我试图使其更具可读性,但是一个简单的if(isset($ _ POST [“ company”]))无效,因为还有6个其他POSTS-具有需要POST的构造函数。 That was why I was wanting to do it in SQL 这就是为什么我想要用SQL做到这一点的原因

<?php
class Sales 
{
public $company;  
private $db;
public function __construct($company)
{
   $this->company = $company; 
   $this->db =mysqli_connect("localhost", "databasename", "password", "table");
}

public function __save()
{
$this->company=htmlspecialchars($this->company);

$sql = ("INSERT INTO " . "`sales` " . "(`company`)" . " VALUES " . "('{$this->company}') ;");
return mysqli_query($this->db, $sql);
}
}

You cannot check if a PHP variable is set inside MySQL. 您无法检查MySQL内是否设置了PHP变量。

The better and stable way is to check if the variable is null in PHP and execute a corresponding SQL statement. 更好且稳定的方法是检查PHP中的变量是否为null并执行相应的SQL语句。

If your variable is a MySQL variable you can check if it is null like so: 如果您的变量是MySQL变量,则可以像下面这样检查它是否为null:

CASE WHEN TABLE.FIELD IS NOT NULL...

You could also use the IFNULL() function for that purpose. 您也可以为此使用IFNULL()函数。

The NULL value cannot be used with conditional operators in MySQL, so you cannot use <, > or = . NULL值不能与MySQL中的条件运算符一起使用,因此不能使用<, > or = Instead you have to use IS NULL or NOT NULL . 相反,您必须使用IS NULLNOT NULL

Please try the below code. 请尝试以下代码。 I didn't check this. 我没有检查。 But hope this will work 但是希望这会起作用

CASE
WHEN (variable IS NOT NULL) UPDATE `sales` SET `company` ='$company'
END CASE

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

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