2017-01-06 64 views
1

對於我的生活,我無法弄清楚爲什麼張貼到這個網頁不工作,我一直在嘗試從Stackoverflow和互聯網上找到的東西。基於html表格的插入數組

第一部分適用於「id」部分,但更新功能不適用。

PHP message: PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: mixed named and positional parameters'

updatecompany.php(21):FR \ LS :: updateCompany(陣列,NULL)

這些是從錯誤日誌。任何幫助將深表感謝。你的問題的

if (isset($_POST["id"]) && is_numeric($_POST["id"])) { 
    $id = $_POST["id"]; 
    $vid = \Fr\LS::getCompany("id", $id); 
    $vname = \Fr\LS::getCompany("name", $id); 
    $vlogo = \Fr\LS::getCompany("logo", $id); 
    $vinfo = \Fr\LS::getCompany("info", $id); 
    $vsite = \Fr\LS::getCompany("site", $id); 
    $vest = \Fr\LS::getCompany("est", $id); 
} elseif (isset($_POST["update"])) { 
    \Fr\LS::updateCompany(array(
     "name" => $_POST["name"], 
     "logo" => $_POST["logo"], 
     "info" => $_POST["info"], 
     "site" => $_POST["site"], 
     "est" => $_POST["est"] 
    ), 
    $_POST["id"]); 
    echo "<center>Company updated!"; 
    echo "<br><a href='updatecompany.php" . $_POST["id"] ."'>go back</a></center>"; 
} else { 
    die("No server with that id."); 
} 

public static function updateCompany($toUpdate = array(), $company = null) { 
    self::construct(); 
    if(is_array($toUpdate) && !isset($toUpdate['id'])) { 
     if($company == null) { 
      echo "No company ID set!"; 
     } 
     $columns = ""; 
     foreach($toUpdate as $k => $v) { 
      $columns .= "`$k` = :$k, "; 
     } 
     $columns = substr($columns, 0, -2); // Remove last "," 

     $sql = self::$dbh->prepare("SELECT {$columns} FROM companys WHERE `id` = ? ORDER BY `id` LIMIT 1"); 
     $sql->bindValue(":id", $company); 
     foreach($toUpdate as $key => $value) { 
      $value = htmlspecialchars($value); 
      $sql->bindValue(":$key", $value); 
     } 
     $sql->execute(); 
    } else { 
     return false; 
    } 
} 
+1

這是怎麼回事與列定義?我還會在'substr'上使用'rtrim'。 – chris85

+0

..而且你不會在任何地方更新!? – Jeff

回答

3

一個部分是:

"SELECT {$columns} FROM companys WHERE `id` = ? ORDER BY `id` LIMIT 1" 

沒有更新語句。

將其更改爲

"UPDATE companys SET {$columns} WHERE `id` = :id"