2017-03-04 96 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

如果我理解正确的话,你想要做这样的事情,

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

嗨,谢谢你的回复。我试过这个,但它不起作用 – Kay

+0

这是因为我只给了你一个函数的一般形式,因为你的问题并不清楚你实际上是想知道如何将你的值保存到数据库中。我客串[Ross Wilson](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

谢谢,我已将代码更改为上述内容,现在我获取了每个酒列的不确定索引并发出警告:PDOStatement :: execute():SQLSTATE [HY093]:无效的参数编号:绑定变量的数目不符合令牌的数量。 – Kay

+0

我的不好,我在'foreach'循环中写了'$ _GET'而不是'$ _POST'。 –

+0

我已经更新了。我没有得到任何错误,除了未定义的变量描述,但它没有更新数据库。 – Kay