Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
仅当post数据使用COALESCE()提供输入时,PHP SQL才会更新_Php_Mysql_Insert_Coalesce - Fatal编程技术网

仅当post数据使用COALESCE()提供输入时,PHP SQL才会更新

仅当post数据使用COALESCE()提供输入时,PHP SQL才会更新,php,mysql,insert,coalesce,Php,Mysql,Insert,Coalesce,嘿,伙计们,我已经读了很多关于这方面的帖子(这些帖子都很老了),我尝试了其中的一切,但我似乎无法解决问题 所以我有一个更新表记录的表单。但是如果没有输入任何名称,那么它不会保留原始名称并进行更新,其余的只是用空字符串替换 我尝试过使用COALESCE(),但它无法将php代码中的任何内容识别为空值 如果不使用引号“”,则会出现SQL错误,但如果使用引号,则不会将其识别为null 例如,如果我在ssh中运行MySQL并键入 选择合并(“”,'test') 然后MySQL只显示空字符串,而不是包含内

嘿,伙计们,我已经读了很多关于这方面的帖子(这些帖子都很老了),我尝试了其中的一切,但我似乎无法解决问题

所以我有一个更新表记录的表单。但是如果没有输入任何名称,那么它不会保留原始名称并进行更新,其余的只是用空字符串替换

我尝试过使用COALESCE(),但它无法将php代码中的任何内容识别为空值

如果不使用引号“”,则会出现SQL错误,但如果使用引号,则不会将其识别为null

例如,如果我在ssh中运行MySQL并键入

选择合并(“”,'test')

然后MySQL只显示空字符串,而不是包含内容的字符串

如果没有引号问题,似乎无法通过使用post变量进行查询

这是我的密码

/* Original query that replaces with empty input feilds...
$update = "UPDATE KIT202_product SET Name='". $_POST['name']
. "', Price='". $_POST['price']
. "', Description='". $_POST['desc']
. "' WHERE ID=". $_POST['id'];
*/

//What I ended up trying but still doesn't work :(...
$name=NULL;
if (!empty($_POST['name'])) $name="'". $_POST['name']. "'";
$price=NULL;
if (!empty($_POST['price'])) $price="'". $_POST['price']. "'";
$desc=NULL;
if (!empty($_POST['desc'])) $desc="'". $_POST['desc']. "'";
这不起作用,因为null只传递nothing而不是null,所以它就像只使用1个值运行coalesce一样。

如果您做2件事会怎么样:1)为字段创建一个小的验证函数;2) 拆分更新以仅更新已通过验证的内容。 因此,不严格地说:

$updatePrefix = "UPDATE KIT202_product SET ";
$updateSuffix = "WHERE ID=". $_POST['id']; // You should first these for injection

$conditions = '';
if (isValid($_POST['name'])) $conditions .= " Name='".$_POST['name']."'";
if (isValid($_POST['price'])) {
   if ($conditions != '') $conditions .= ', ';
   $conditions .= " Price= '".$_POST['price']."'";
}
if (isValid($_POST['desc'])) {
    if ($conditions != '') $conditions .= ', ';
    $conditions .= " Description= '".$_POST['desc']."'";
}

if ($conditions != '') $query = $updatePrefix . $conditions . $updateSuffix;

function isValid($input) {
    return (!(empty($input) && $input == ''));
}
这应该可以满足您的需要

更新:回答您的评论,是的,您可以使用COALESCE。试着这样做:

$name=NULL;
if (!empty($_POST['name'])) $name="'". $_POST['name']. "'";
$price=NULL;
if (!empty($_POST['price'])) $price="'". $_POST['price']. "'";
$desc=NULL;
if (!empty($_POST['desc'])) $desc="'". $_POST['desc']. "'";
$update = "UPDATE KIT202_product SET Name=COALESCE($name, KIT202.Name)"
. "', Price=COALESCE($price,KIT202.Price)"
. "', Description=COALESCE($desc, KIT202.Description)"
. "' WHERE ID=". $_POST['id'];

你能展示一下发出update语句的代码部分吗?很酷,谢谢。。。是的,我最后做了类似的事情。。但只是出于强迫,我需要知道。有没有一种方法可以将NULL值从php传递给sql,以便与coalesce()一起使用??