更新PHP准备bindparam

时间:2016-05-10 09:37:10

标签: php mysql mysqli

这是我的PHP代码:

<?php
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        $UpdateChecklist = 'UPDATE checklists SET ADMIN_ID=?, COMPUTER_ID=? WHERE id=?';
        $stmtChecklist = $connection->prepare($UpdateChecklist);
        $stmtChecklist->bind_param('ii', $_POST['ADMIN_ID'], $_POST['COMPUTER_ID']);

        $isUpdate = $stmtChecklist->execute();
        $lastUpdateId = mysqli_insert_id($connection);

        $stmtChecklist->close();

        $UpdateInstalledProgram = 'UPDATE checklist_programs SET CHECKLIST_ID=?, PROGRAM_ID=? WHERE id = ?';
        $stmtProgramId = $connection->prepare($UpdateInstalledProgram);
        $stmtProgramId->bind_param('ii', $lastUpdateId, $programId);
        foreach ($_POST['PROGRAM_ID'] as $program) {
            $programId = $program;
            $stmtProgramId->execute();
        }
        $connection->close();
        if ($isUpdate) {
            header('Location: OverViewCheckList.php');
            exit(0);
        }
    }
?>

并且,我遇到了一些错误,我不知道如何解决它:

  

警告:mysqli_stmt :: bind_param():变量数量与第34行的C:\ xampp \ htdocs \ checklist \ updateChecklist.php中预准备语句中的参数数量不匹配

     

警告:mysqli_stmt :: bind_param():变量数量与第44行的C:\ xampp \ htdocs \ checklist \ updateChecklist.php中预准备语句中的参数数量不匹配

     

警告:mysqli :: query():无法在第57行的C:\ xampp \ htdocs \ checklist \ updateChecklist.php中获取mysqli

     

致命错误:在第59行的C:\ xampp \ htdocs \ checklist \ updateChecklist.php中调用null上的成员函数fetch_assoc()

1 个答案:

答案 0 :(得分:3)

在你的两个查询中,你使用3?但只绑定2个变量。

查询1:

$UpdateChecklist = 'UPDATE checklists SET ADMIN_ID=?, COMPUTER_ID=? WHERE id=?';
$stmtChecklist = $connection->prepare($UpdateChecklist);

$stmtChecklist->bind_param('ii', $_POST['ADMIN_ID'], $_POST['COMPUTER_ID']); /* Here*/

查询2:

$UpdateInstalledProgram = 'UPDATE checklist_programs
SET CHECKLIST_ID=?, PROGRAM_ID=? WHERE id = ?';
$stmtProgramId = $connection->prepare($UpdateInstalledProgram);

$stmtProgramId->bind_param('ii', $lastUpdateId, $programId); /* Here */

您需要添加第三个变量,例如:

$stmtChecklist->bind_param('iii', $_POST['ADMIN_ID'], $_POST['COMPUTER_ID'], $id3);
$stmtProgramId->bind_param('iii', $lastUpdateId, $programId, $id3);