unfulat valueerror:mysqli_stmt :: execute():参数#1($ params)必须是列表数组

发布于 2025-01-28 02:15:53 字数 1007 浏览 3 评论 0原文

我正在尝试通过这两个查询在我的数据库中插入已准备好的多个值,这两个查询都出现故障,返回

未定的错误:致电未定义的方法mysqli_stmt :: bindvalue()

第一个代码或

undurew valueerror:mysqli_stmt :: execute():参数#1($ params) 必须是列表数组

第二个列表阵列。当我键入list而不是array时,它说

语法错误

注意所有变量,表和列名是一个简化的变体

我已经看到了很多类似的问题,但是它们都没有回答我的问题。 的“重复”。

甚至不是有人有解决方案

这是两个代码:

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'],
));

I'm trying to insert prepared statemant multiple values in my database through these two queries, which are both malfunctioning, returning either

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

for the first code or

Uncaught ValueError: mysqli_stmt::execute(): Argument #1 ($params)
must be a list array

for second one. When I type list instead of array, it says

syntax error

notice all variable, table and column names are a simplified variant

I've seen a lot of similar questions, but none of them answer my problem. Not even the one that this is the 'duplicate' of.

Does anyone have a solution, or, maybe, an alternative?

Here are the 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'],
));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

提赋 2025-02-04 02:15:53

由于错误消息明确,您正在使用 mysqli 库与您的数据库进行通信。但是,您的代码似乎是基于使用 pdo 的示例完全是一个不同的库。尽管从表面上讲,某些函数名称相似,但实际上它们具有不同的API和要求。

例如,某些API差异是:

  • mySQLI不支持命名参数,只有
  • PDO指定的匿名占位符具有BindParam和BindValue函数,而MySQLI
  • 在PHP 8.1之前没有Bind_Param,MySQLI才接受A参数参数参数参数参数参数参数。直接通过execute()函数提供的列表。

此代码应与MySQLI一起使用:

如果您有PHP 8.1或更高:

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

否则:

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

As the error messages make clear, you're using the mysqli library to communicate with your database. However your code seems to be based on examples which use PDO, which is a different library entirely. While superficially some of the function names are similar, they actually have different APIs and requirements.

For example, some API differences are:

  • mysqli doesn't support named parameters, only anonymous placeholders specified with ?
  • PDO has bindParam and bindValue functions, whereas mysqli has bind_param
  • until PHP 8.1, mysqli did not accept a parameter list provided directly via the execute() function.

This code should work with mysqli:

If you have PHP 8.1 or above:

$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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文