2017-11-25 8 views
-2

このスクリプトで何が間違っているのか誰にでも見られますか?私はデータが$ _POST配列にあり、query-stringも正しく形成されていることを確認しましたが、何らかの理由でこれが実行されると、更新される列はデータベースで空になります。PDO UPDATEクエリは更新する代わりに値を削除します

/* Get the ID for the house to be modified by using a hidden-field 'hiddenDescription' */ 
$stmt = $db->prepare('SELECT id FROM talot WHERE kuvaus = :description'); 
$stmt->bindParam(':description', $_POST['hiddenDescription'], PDO::PARAM_STR); 
$stmt->execute(); 
if($row = $stmt->fetch(PDO::FETCH_ASSOC)){ 
    // The column names in the database 
    $col_names = array("kaupunki", "osoite", "pintaAla", "koko", "vuosi", "hinta", "otsikko", "kuvaus", "valittaja"); 
    $comma = ","; 
    $i = 0; 
    $unprepared = 'UPDATE talot SET '; 
    /* Go through the POST -array and add the column name and :$key (for binding) into the query string. Also add comma */ 
    foreach($_POST as $key => $value){ 
     if(!empty($_POST[$key])){ 
      // Skip hiddenDescription 
      if($key != 'hiddenDescription'){ 
       $unprepared .= "$col_names[$i] = :$key".$comma; 
      } 
      // If $key was hiddenDescription decrement $i; 
      else{ 
       $i--; 
      } 
     } 
     $i++; 
    } 
    // chop the last comma. 
    $prepared = chop($unprepared, ','); 
    $prepared .= ' WHERE id = :id'; 
    $stmt = $db->prepare($prepared); 
    $i = 0; 
    /* Go through the POST -array and bind values that are not empty. Again skip hiddenDescription. */ 
    foreach($_POST as $key => $value){ 
     if(!empty($value)){ 
      if($key != 'hiddenDescription'){ 
       $stmt->bindParam(":$key", $value, PDO::PARAM_STR); 
      } 
      else{ 
       $i--; 
      } 
     } 
     $i++; 
    } 
    // Bind the ID received in the first database query. 
    $id = (int)$row['id']; 
    $stmt->bindParam(":id", $id, PDO::PARAM_INT); 
    $result = $stmt->execute(); 
    if($result){ 
     echo 1; 
    } 

ありがとうございます!

+0

は、SQL文をエコー、それが – Akintunde007

+0

を返すかを示しますオブジェクト(PDOStatement)#3(1){ ["que id:id " " "UPDATEタロットセットosoite =:アドレスWHERE id =:id" :UPDATEタロットセットosoite =:アドレスWHERE id =:id –

+0

PHP:echo var_dump($ stmt) 。 ":" $準備しました。 –

答えて

0

申し訳ありませんが、このheheを解決しました。

この:

foreach($_POST as $key => &$value){ 
if(!empty($value)){ 
    if($key != 'hiddenDescription'){ 
     $stmt->bindParam(":$key", $value, PDO::PARAM_STR); 
    } 
    else{ 
     $i--; 
    } 
} 
$i++; 

}

(bindParamが& $変数を必要とする):これに変更

foreach($_POST as $key => $value){ 
    if(!empty($value)){ 
     if($key != 'hiddenDescription'){ 
      $stmt->bindParam(":$key", $value, PDO::PARAM_STR); 
     } 
     else{ 
      $i--; 
     } 
    } 
    $i++; 
} 

関連する問題