dpmir1988 2017-01-06 02:15
浏览 34
已采纳

php插入基于html表单的数组

For the life of me I can't figure out why posting to this page is not working, i have been trying things I have found from all over Stackoverflow and the internet.

The first part works for the "id" section but the update function does not.

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(Array, NULL)

These are from the error logs. Any help would be deeply appreciated.

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条回答 默认 最新

  • dotn30471 2017-01-06 02:38
    关注

    One part of your problem is:

    This

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

    is no update statement.

    Change it to

    "UPDATE companys SET {$columns} WHERE `id` = :id"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证