I am trying to insert multiple values into MySQL via an array, but It's not working or delivering an error message so I'm not sure where I'm going wrong. Any help would be appreciated.
Here is where I call the function
$testArrayList = array();
$testArrayList[] = 'Account_idAccount';
$testArrayList[] = 'firstName';
$testArrayList[] = 'lastName';
$testArrayValues = array();
$testArrayValues[] = $idAccount;
$testArrayValues[] = $firstName;
$testArrayValues[] = $lastName;
$dbManager->insertValues("User", $testArrayList, $testArrayValues);
Now, here is the insertValues funciton being called.
public function insertValues($table, $cols, $values) {
foreach ($cols as $col)
$colString .= $col.',';
foreach ($values as $value)
{
$valueAmount .= '?,';
$valueType .= 's';
$valueParam .= $value.",";
}
$colString = substr($colString, 0, -1);
$valueAmount = substr($valueAmount, 0, -1);
$valueParam = substr($valueParam, 0, -1);
$mysqli = new mysqli(DBHOST, DBUSER, DBPASSWORD, DBDATABASE);
$sql = "INSERT INTO $table ($colString) VALUES($valueAmount)";
/* Prepared statement, stage 1: prepare */
if (!($stmt = $mysqli->prepare($sql))) {
echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
print_r($valueParam);
/* Prepared statement, stage 2: bind and execute */
if (!$stmt->bind_param("$valueType", $valueParam)) {
echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
}
if (!$stmt->execute()) {
echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}
/* explicit close recommended */
$stmt->close();
$mysqli->close();
}