PHP Prepared语句foreach循环

时间:2014-03-04 17:48:22

标签: php prepared-statement

我有这个准备好的声明,我不知道如何迭代,它看起来像这样:

    <?php
    //if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        require('../includes/db_connect.php');
        $i = 40;
                foreach ($_POST['item'] as $value) {

            /* Register a prepared statement */
            if ($stmt = $mysqli->prepare('

            UPDATE house_room1 SET z = ? WHERE object_id = ?

            ')) {
                /* Bind parametres */
                $stmt->bind_param('ii', $object_id, $i);

                /* Insert the parameter values */
                $object_id = 1;
                $i = $i;

                /* Execute the query */
                $stmt->execute();

                /* Close statement */
                $stmt->close();

            } else {
                /* Something went wrong */
                echo 'Something went terribly wrong' . $mysqli->error;
            }
            $i++;
        }
    //}
?>

我希望它在z上添加一个新值,但现在它甚至不知道$i是什么。嗯,任何想法,建议或建议?提前谢谢。

1 个答案:

答案 0 :(得分:0)

改变这个:

require('../includes/db_connect.php');

/* Insert the parameter values */
$object_id = 1;
$i = 40;

foreach ($_POST['item'] as $value) {

    /* Register a prepared statement */
    if ($stmt = $mysqli->prepare('UPDATE house_room1 SET z = ? WHERE object_id = ?')) {
        /* Bind parametres */
        $stmt->bind_param('ii', $i, $object_id);

        /* Execute the query */
        $stmt->execute();

        /* Close statement */
        $stmt->close();

    } else {
        /* Something went wrong */
        echo 'Something went terribly wrong' . $mysqli->error;
    }
    $i++;
}