-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 に答える