I'm trying to create a dynamic function that insert data into database taken from an array by using prepared statement, so I found this code in the Internet, but it doesn't work as intended.
The first function "posts_info" that used to call the "insert function" and send the data to it.
function posts_info($registered_data){
global $connect;
$table = "posts";
$cols =array_keys($registered_data);
$values = $registered_data;
insert($table, $cols, $values);
}
The "insert function" above that used to insert the data into the database
function insert($table, $cols, $values){
global $connect;
$placeholder = array();
for ($i = 0; $i < count($values); $i++)
$placeholder[] = '?';
$sql = 'INSERT INTO '. $table . ' (`' . implode("`, `", $cols) . '`) ';
$sql.= 'VALUES (' . implode(", ", $placeholder) . ')';
$stmt = $connect->prepare($sql);
$stmt-> bind_param("issssss",$a,$b,$s,$d,$u,$o,$p);
$stmt->execute();
$stmt->close();
}
When every I try to insert something I get this error
Please help me to fix this problem.
I adjusted the functions to work with mysqli instead of PDO
The new error I got is
mysqli_stmt::execute() expects exactly 0 parameters, 1 given in /opt/lampp/htdocs/site1/admin/core/function/general.php on line 29
line 29 is
$stmt->execute($values);
In the insert function