简体   繁体   中英

PHP Prepared statements foreach loop

I have this prepared statement that I don't know how to iterate over, it looks like this:

    <?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++;
        }
    //}
?>

I want it to add a new value on z but right now it doesn't even know what $i is. Uhm, any ideas, suggestions or advice? Thanks in advance.

change this:

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++;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM