What I want to be able to do is create a prepared statement that would insert values based on an inputted array, the array would be dynamic. What I mean by dynamic is the ability to just change the array $table
by including an extra line such as the commented out one below and it properly send and insert the values into the table regardless how many are present. Because of my new use of prepared statements I cannot just use my previous method of:
INSERT INTO table ($fields) VALUES ($data)
Because that is insecure and is open for SQL Injection.
$table = array();
$table['Favorite_Chocolate'] = 'Milk';
$table['Favorite_Cake'] = 'Vanilla';
$table['Favorite_IceCream'] = 'Neapolitan';
//$table['Favorite_Candy'] = 'Taffy';
function magic_insert ($table, $dbc) {
if ($stmt = mysqli_prepare($dbc, "INSERT INTO mytable (?, ?, ?) VALUES (?, ?, ?)")) {
mysqli_stmt_bind_param($stmt, "ssssss", $1, $2, $3, $4, $5, $6);
$1 = array_keys($table)[0];
$2 = array_keys($table)[1];
$3 = array_keys($table)[2];
$4 = array_values($table)[0];
$5 = array_values($table)[1];
$6 = array_values($table)[2];
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
}
}