Php 仅当列上的值与空值不同时才更新该值

Php 仅当列上的值与空值不同时才更新该值,php,mysql,Php,Mysql,我有这个表单,现在,我只需要更新数据库中变量不为空的部分。 我是说,如果我有这个: $postEmri = filter_var($_POST["postEmri"], FILTER_SANITIZE_EMAIL); $postKlienti = filter_var($_POST["postKlienti"], FILTER_SANITIZE_STRING); $postTelefoni = filter_var($_POST["postTelefoni"], F

我有这个表单,现在,我只需要更新数据库中变量不为空的部分。 我是说,如果我有这个:

$postEmri       = filter_var($_POST["postEmri"], FILTER_SANITIZE_EMAIL);
$postKlienti        = filter_var($_POST["postKlienti"], FILTER_SANITIZE_STRING);
$postTelefoni   = filter_var($_POST["postTelefoni"], FILTER_SANITIZE_STRING);
这是:

$sql="UPDATE forma SET emri='$postEmri', klienti='$postKlienti', telefoni='$postTelefoni'";
如果
$postEmri,$postKlienti
未定义或
为空
,我不希望更新该记录。 我如何使用一些条件来实现它

谢谢

所以我需要这样做

$sql="UPDATE forma SET ";
// add every non-empty field to the query
if (!empty($postEmri)) $sql += " emri='$postEmri',";
if (!empty($postKlienti)) $sql += " klienti='$postKlienti',";
if (!empty($postTelefoni)) $sql += " telefoni='$postTelefoni,'";
// replace the last `,` for `;`
$sql = substr($sql, 0, -1) . ";";
$result=mysql_query($sql) or die(mysql_error()) ;
你可以用

你可以用

更新 你说过如果你有100个post变量,你不想有“100个if语句”。下面是您的操作方法,这将检查所有的$\u POST变量。请记住,这不会检查某些输入字段是否为空,这意味着输入值中可能有空格而不会返回错误

// Here you set how many results there should be from $_POST (total inputs)
$totalInputs=100;

if(count($_POST) != $totalInputs)
{
    echo "You forgot to fill all fields.";
}
else
{
    // Do your MySQL query here
}
希望它能帮助你


旧代码 使用以下PHP代码段:

if(!empty($postEmri) && !empty($postKlienti))
{
     $sql="UPDATE forma SET emri='$postEmri', klienti='$postKlienti', telefoni='$postTelefoni'";
}
PHP函数empty()检查变量是否为空

更多信息请点击此处:

更新 你说过如果你有100个post变量,你不想有“100个if语句”。下面是您的操作方法,这将检查所有的$\u POST变量。请记住,这不会检查某些输入字段是否为空,这意味着输入值中可能有空格而不会返回错误

// Here you set how many results there should be from $_POST (total inputs)
$totalInputs=100;

if(count($_POST) != $totalInputs)
{
    echo "You forgot to fill all fields.";
}
else
{
    // Do your MySQL query here
}
希望它能帮助你


旧代码 使用以下PHP代码段:

if(!empty($postEmri) && !empty($postKlienti))
{
     $sql="UPDATE forma SET emri='$postEmri', klienti='$postKlienti', telefoni='$postTelefoni'";
}
PHP函数empty()检查变量是否为空

更多信息请点击此处:

如果只想更新非空字段:

$sqlStart="UPDATE forma SET ";
$sql="";
// add every non-empty field to the query
if (!empty($postEmri)) $sql .= " emri='$postEmri',";
if (!empty($postKlienti)) $sql .= " klienti='$postKlienti',";
if (!empty($postTelefoni)) $sql .= " telefoni='$postTelefoni,'";
// if any of the fields is non-empty, run the query
if ($sql != "") {
    // replace the last `,` for `;`
    $sql = substr($sql, 0, -1) . ";";
    // run sql command
    $sqlCommand = $sqlStart.$sql;
} else {
    // no fields to update
}

如果只想更新不为空的字段:

$sqlStart="UPDATE forma SET ";
$sql="";
// add every non-empty field to the query
if (!empty($postEmri)) $sql .= " emri='$postEmri',";
if (!empty($postKlienti)) $sql .= " klienti='$postKlienti',";
if (!empty($postTelefoni)) $sql .= " telefoni='$postTelefoni,'";
// if any of the fields is non-empty, run the query
if ($sql != "") {
    // replace the last `,` for `;`
    $sql = substr($sql, 0, -1) . ";";
    // run sql command
    $sqlCommand = $sqlStart.$sql;
} else {
    // no fields to update
}


请先尝试研究:使用和。答案更新,检查它。我几乎给了你相同的答案,只是我检查了是否有更改的字段。如果没有更改任何字段,上面的代码将抛出一个mysql错误。上面的代码可能会导致一个sql字符串$sql=“UPDATE forma SET emri='value',SET klienti='value',SET telefoni='value,我认为这是无效的,因为额外的集合“它们是一对额外的”集合“,我已经更新了问题和答案。首先谢谢:使用和。答案更新,检查它。我给了你几乎相同的答案,只是我检查了是否有更改的字段。如果没有更改任何字段,上面的代码将抛出一个mysql错误。上面的代码可能会导致sql字符串$sql=“UPDATE forma SET emri='value',SET klienti='value',SET telefoni='value,我认为这是无效的,因为额外的集合有两个额外的“集合”,我已经更新了问题和答案。谢谢!empty()还检查isset.empty($var),如果未设置$var,则返回false。!empty()还检查isset.empty($var)如果未设置$var,将返回false。请再次查看我更新的问题,只是为了确认它。!因为所有变量都不可能为ne UNDEFINEDTANKS,但我可能会遗漏一些内容,因为它说查询为空。.可能我需要添加smth。.您确定所有字段都为非空吗?请尝试打印出
$sql
just为了确保它有值并且语法正确,我复制粘贴了它,就像上面一样;当我打印$sql时,我得到以下信息:(;)只是..很奇怪,虽然Oh ya dis是一个更好的1+1;)请再次查看我更新的问题,只是为了确认..!因为所有变量都不可能为ne UndefinedHanks,但我可能遗漏了一些内容,因为它说查询是空的..也许我需要添加smth..你确定所有字段都是非空的吗?请尝试打印出来
$sql
为了确保它有值并且语法正确,我复制粘贴了它,就像上面一样;当我打印$sql时,我得到:(;)只是..很奇怪,虽然Oh ya dis是更好的1+1;)