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粉3735968282024-02-04 14:00:15
As the error message indicates, you are using the mysqli a> 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:
?
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();