我正在更新php/SQL中的表單。該表單成功更新MYSQL,但它會刪除所有空字段。我想嘗試一個CASE語句,根據表單是否有條目進行更新。有條件的SQL更新CASE
CASE
WHEN (variable is set/is not null?) UPDATE `sales` SET `company` ='$company'
END CASE
我找不到檢查變量是否爲NULL的方法。那可能嗎?什麼是語法?
//可能不需要的額外註釋 下面是關於我在做什麼的信息。這與問題本身無關,但我看到人們更喜歡獲取更多信息。表單提交給一個類和構造函數。我正在研究依賴注入類,但這似乎是矯枉過正。
<?php
echo "<html>
<form method='POST'>
<fieldset>
<input id='company' name='company' type='text' class='form-control' placeholder='". $company. "'>";
</fieldset>
</form>
</html>
?>
的職位:
<?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();
?>
//請注意 - 我已經瘦了一個變量,但實際上有7我試圖使其更具可讀性,但如果簡單( isset($ _ POST [「company」]))將不起作用,因爲還有6個其他POSTS-具有需要POST的構造函數。這就是爲什麼我想這樣做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);
}
}
你也可以用insert語句顯示'Salesupdateentry'的部分嗎? – Fabricator
發出查詢的實際php代碼也是相關的。看到你在你的SQL中插入字符串,可以肯定地猜測你正在使用一個標有日期的數據庫接口sans prepared statements。在這種情況下,來自PHP的NULL值將以空字符串結尾。 – mario
好的,我編輯了它。 –