search

Home  >  Q&A  >  body text

Uncaught ValueError: mysqli_stmt::execute(): Parameter #1 ($params) must be an array of lists

I'm trying to insert multiple values ​​into my database with prepared statements via these two queries, both queries are failing and returning one of the

Uncaught Error: Call to undefined method mysqli_stmt::bindValue()

First code or

Uncaught ValueError: mysqli_stmt::execute(): Parameter #1 ($params) Must be a list array

the second. When I type list instead of array it shows

Grammatical errors

NoteAll variable, table and column names are simplified variations

I've seen many similar questions, but none of them answer my question. Not even this "repeat" one.

Does anyone have a solution, or alternative?

These are two codes:

if(isset($_POST['ID'], $_POST['atr1'])){

        $sql = $db -> prepare("INSERT INTO some_table (ID, atr_place) VALUES (':fir', ':sec')");
        
        $sql -> bindValue(':fir', $_POST['ID'],);
        $sql -> bindValue(':sec', $_POST['atr1'],);
        $sql -> execute();
}
$sql = $db -> prepare("INSERT INTO some_table (ID, atr_place) VALUES (:fir, :sec)");
$sql -> execute(array(
    ':fir' => $_POST['ID'],
    ':sec' => $_POST['atr1'],
));

P粉502608799P粉502608799303 days ago429

reply all(1)I'll reply

  • P粉373596828

    P粉3735968282024-02-04 14:00:15

    As the error message indicates, you are using the mysqli library to communicate with your database. However, your code seems to be based on an example using PDO, which is a completely different library. Although some functions have superficially similar names, they actually have different APIs and requirements.

    For example, some API differences are:

    • mysqli does not support named parameters, only anonymous placeholders specified using ?
    • PDO has bindParam and bindValue functions, while mysqli has bind_param
    • Prior to PHP 8.1, mysqli did not accept parameter lists provided directly through the execute() function.

    This code should be used with mysqli:

    If you have PHP 8.1 or higher:

    $sql = $db->prepare("INSERT INTO some_table (ID, atr_place) VALUES (?, ?)");
    $sql->execute([$_POST['ID'], $_POST['atr1']]);

    otherwise:

    $sql = $db->prepare("INSERT INTO some_table (ID, atr_place) VALUES (?, ?)");
    $sql->bind_param("ss", $_POST['ID'], $_POST['atr1']);
    $sql->execute();

    reply
    0
  • Cancelreply