2017-03-04 3 views
0

データベースの詳細を更新する関数にプロパティを渡したいと思います。私は、フォームに選択されたすべての列を関数に渡す必要があります。率直に言って、私は何をすべきかわかりません。

私のコードは次のとおりです:

if (isset($_POST["updateWineButton"])) { 

    $wineID = $_POST["wineID"]; 
    $wineCountryID = $_POST["wineCountryID"]; 
    $wineSizeID = $_POST["wineSizeID"]; 
    $wineRatingID = $_POST["wineRatingID"]; 
    $wineColourID = $_POST["wineColourID"]; 
    $packageID = $_POST["packageID"]; 
    $wineCategoryID = $_POST["wineCategoryID"]; 
    $wineCode = $_POST["wineCode"]; 
    $price = $_POST["price"]; 
    $description = $_POST["description"]; 
    $wineRating = $_POST["wineRating"]; 
    $wineIMG = $_POST["wineIMG"]; 

    updateWine($updateWine); 
    $status = "$description has been updated."; 
} 

更新ワイン機能

function updateWine($wineUpdate) 
{ 
    global $pdo; 
    $statement = $pdo->prepare("UPDATE WINE SET wineID=?, wineCountryID=?, wineSizeID=?, wineRatingID, wineColourID=?, 
          packageID=?, wineCategoryID=?, wineCode=?, price=?, description=?, wineRating=?, wineIMG=? 
          WHERE wineID=?"); 

    $statement->execute([$wineUpdate->wineID, 
     $wineUpdate->wineCountryID, 
     $wineUpdate->wineSizeID, 
     $wineUpdate->wineRatingID, 
     $wineUpdate->wineColourID, 
     $wineUpdate->packageID, 
     $wineUpdate->wineCategoryID, 
     $wineUpdate->wineCode, 
     $wineUpdate->price, 
     $wineUpdate->description, 
     $wineUpdate->wineRatingID, 
     $wineUpdate->wineIMG]); 

    $statement->fetch(); 
} 
+2

'$ updateWine'が定義されていません。 'updateWine()'関数を表示できますか? –

+0

@RossWilsonはい、それは私の問題です。私は$ _POSTをupdateWine関数に渡したいが、それは許されていない。私は投稿を更新しました。 – Kay

+0

妥当性確認の心配はありますか?つまり、ユーザーがフォームの値の1つを入力しないとどうなりますか? –

答えて

1

私はあなたがこのような何かをしたい、正しく理解していれば、

if (isset($_POST["updateWineButton"])) { 
    $result = updateWine($_POST); 
    if($result){ 
    $status = "$description has been updated."; 
    }else{ 
    $status = "An error occurred."; 
    } 
} 

//your function woud then look like ... 

function updateWine($postdata){ 
    $wineID = $postdata["wineID"]; 
    $wineCountryID = $postdata["wineCountryID"]; 
    $wineSizeID = $postdata["wineSizeID"]; 
    $wineRatingID = $postdata["wineRatingID"]; 
    $wineColourID = $postdata["wineColourID"]; 
    $packageID = $postdata["packageID"]; 
    $wineCategoryID = $postdata["wineCategoryID"]; 
    $wineCode = $postdata["wineCode"]; 
    $price = $postdata["price"]; 
    $description = $postdata["description"]; 
    $wineRating = $postdata["wineRating"]; 
    $wineIMG = $postdata["wineIMG"]; 
    //udpate your database with the above values 
    //check if update is successful 
    return true; 
    //else if there was an error 
    return false; 
} 
+0

こんにちは、応答ありがとうございます。私はこれを試してみましたが、実際にはDBに値を保存する方法を知りたがっていることがあなたの質問にはっきりしていないので、一般的な形式の関数しか与えられていないので、 – Kay

+0

です。私はゲスト[ロスウィルソン](http://stackoverflow.com/users/1033654/ross-wilson)あなたの質問に答えた。 – Aurovrata

+0

[Ross Wilson](http://stackoverflow.com/users/1033654/ross-wilson)の答えがあなたの望むものなら、正解として彼の返信を選択してください。 – Aurovrata

2

次のようなものあなたのために働くはずです:

function updateWine() 
{ 
    global $pdo; 

    $keys = [ 
     "wineID", "wineCountryID", "wineSizeID", "wineRatingID", "wineColourID", "packageID", "wineCategoryID", 
     "wineCode", "price", "description", "wineRating", "wineIMG", 
    ]; 

    $results = []; 

    foreach ($keys as $index) { 
     if (isset($_POST[$index])) { 
      $results[$index] = $_POST[$index]; 
     } 
    } 

    $statement = $pdo->prepare("UPDATE WINE SET " . implode('=?, ', array_keys($results)) . "=? WHERE wineID =?"); 

    $statement->execute(array_merge(array_values($results), [$_POST['wineID']])); 

    $statement->fetch(); 
} 

if (isset($_POST["updateWineButton"]) && isset($_POST['wineID'])) { 
    updateWine(); 
} 

希望します。

+0

ありがとう、私は上記の私のコードを変更して、今私はワインの列と警告のそれぞれについてundfefinedインデックスを取得しています:PDOStatement :: execute():SQLSTATE [HY093]:無効なパラメータ番号:トークンの数に一致しません。 – Kay

+0

私は悪いですが、 'foreach'ループに' $ _POST'ではなく '$ _GET'を書いています。 –

+0

今更新しました。私は定義されていない変数の記述を除いてエラーは出ませんが、データベースを更新していません。 – Kay

関連する問題