-1

我有这个查询,我用它来更新一个表。问题是,我只需要更新与“未定义”不同的值

在这里有人的帮助下,我得到了这个查询:

$sqlStart="UPDATE forma SET ";
$sql="";
if (!empty($postDyqani_pergjegjes)) $sql += " dyqani_pergjegjes='$postDyqani_pergjegjes',";
if (!empty($postEmri)) $sql += "  emri='$postEmri',";
if (!empty($postKlienti)) $sql += "  klienti='$postKlienti',";
if (!empty($postTelefoni)) $sql += "  telefoni='$postTelefoni,'";
if (!empty($postMontim)) $sql += "  montim='$postMontim',";
if (!empty($postAdresa)) $sql += "  adresa='$postAdresa',";
if (!empty($postData_e_shitjes)) $sql += "  data_e_shitjes='$postData_e_shitjes',";
if (!empty($postDifekti)) $sql += "  difekti='$postDifekti',";
if (!empty($postTekniku_emer)) $sql += "  tekniku_emer='$postTekniku_emer',";
if (!empty($postTekniku_mesazh)) $sql += "  tekniku_mesazh='$postTekniku_mesazh',";
if (!empty($postData_fillim)) $sql += "  data_fillim='$postData_fillim',";
if (!empty($postData_mbarim)) $sql += "  data_mbarim='$postData_mbarim',";
if (!empty($postData)) $sql += "  data='$postData',";
if (!empty($postStatus)) $sql += "  status='$postStatus',";
// replace the last `,` for `;`
if ($sql != "") {
$sql = substr($sql, 0, -1) . ";";

    // replace the last `,` for `;`
    // run sql command
    echo $sqlCommand = $sqlStart.$sql;
    $result=mysql_query($sqlCommand) or die(mysql_error()) ;

} else {
}

虽然它不会执行..请帮我解决这个问题..如果我打印变量大多数结果是undefined 谢谢

4

1 回答 1

0

您的方法有很多问题;首先, += 不会做你认为它对 PHP 字符串所做的事情,并且可能会使所有内容最终成为值 0。使用 .= 将字符串连接到现有字符串:

$foo = '123';
$foo .= 'abc';

// $foo == '123abc'

其次,如果没有更多关于变量来自何处的信息,很难说哪里出了问题。如果您有一个名为“Dyqani_pergjegjes”的字段,则引用它的正确方法是使用$_POST['Dyqani_pergjegjes'].

此外,您需要转义要在 SQL 查询中使用的字符串,以避免 SQL 注入漏洞和其他可能的错误(或在 mysqli 中使用准备好的语句或使用 PDO),如果您想正确解决它):

difekti='". mysql_real_escape_string($postDifekti) . "'

您还缺少 UPDATE 语句的条件,因此在当前形式下,它将更改数据库中的所有行。可能不是你想要的。而且您不需要添加“;” 在语句的末尾 - 这在执行查询时是隐含的,通常不建议这样做。

于 2012-12-12T10:14:00.720 回答