Reputation: 1746
I am trying to save data from an HTML form into database wordpress
table: wp_testdb
using Prepared Statements method, But I am getting error against line mysqli_stmt_execute();
Warning: mysqli_stmt_execute() expects exactly 1 parameter, 0 given in Here 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 has below data:
<?php
$dbServername = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbName = "wordpress";
$conn= mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);
Any suggestions to resolve this?
Upvotes: 0
Views: 825
Reputation: 26450
I will recommend using the object oriented interface instead of the procedural. Both will work - however, all the procedural functions require the connection or statement object to be the first parameter.
As per the documentation, you need to pass the statement-object, in your case $stmt
, as a parameter.
mysqli_stmt_execute($stmt);
This function returns a boolean, which you can check if the query was successful or not.
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));
}
}
Upvotes: 1