gpt4 book ai didi

php - mysqli_stmt_execute() 需要 1 个参数,给定 0 个参数

转载 作者:行者123 更新时间:2023-11-29 16:49:20 24 4
gpt4 key购买 nike

我正在尝试使用准备好的语句方法将 HTML 表单中的数据保存到数据库 wordpress 表:wp_testdb,但是我在 mysqli_stmt_execute( );

Warning: mysqli_stmt_execute() expects exactly 1 parameter, 0 given inHere is my code:

if(isset($_POST['BtnSubmit'])){
include_once 'dbConnection.php';

if($conn -> connect_error) {
die("connection failed:".$conn-> connect_error);
}

$date = $_POST['date'];
$select_bank = $_POST['select_bank'];
$entry_type = $_POST['entry_type'];
$income_cat = $_POST['income_cat'];
$amount = $_POST['amount'];


$sql = "INSERT INTO wp_testdb (data_one, data_two, data_three, data_four, data_five) VALUES (?, ?, ?, ?, ?);";

$stmt = mysqli_stmt_init($conn);

if (!mysqli_stmt_prepare($stmt,$sql)) {
echo "SQL Error !!";
} else {
mysqli_stmt_bind_param($stmt, "sssss", $date, $select_bank, $entry_type, $income_cat, $amount);
mysqli_stmt_execute();
}
}

dbConnection.php 有以下数据:

<?php

$dbServername = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbName = "wordpress";

$conn= mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);

有什么建议可以解决这个问题吗?

最佳答案

我建议使用面向对象的接口(interface)而不是过程式的。两者都可以工作 - 但是,所有过程函数都需要连接或语句对象作为第一个参数。

根据 documentation ,您需要将语句对象(在您的情况下为 $stmt)作为参数传递。

mysqli_stmt_execute($stmt);

该函数返回一个 bool 值,您可以通过它检查查询是否成功。

if (!mysqli_stmt_prepare($stmt,$sql)) {
echo "SQL Error !!";
error_log(mysqli_error($conn));
} else {
mysqli_stmt_bind_param($stmt, "sssss", $date, $select_bank, $entry_type, $income_cat, $amount);
if (mysqli_stmt_execute($stmt)) {
// Query succeeded!
// Do something
} else {
// Query failed. Log it, and do something?
error_log(mysqli_stmt_error($stmt));
}
}

关于php - mysqli_stmt_execute() 需要 1 个参数,给定 0 个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52896105/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com