我正在尝试通过数组将多个值插入 MySQL,但它不起作用或传递错误消息,所以我不确定我哪里出错了。任何帮助,将不胜感激。
这是我调用函数的地方
$testArrayList = array();
$testArrayList[] = 'Account_idAccount';
$testArrayList[] = 'firstName';
$testArrayList[] = 'lastName';
$testArrayValues = array();
$testArrayValues[] = $idAccount;
$testArrayValues[] = $firstName;
$testArrayValues[] = $lastName;
$dbManager->insertValues("User", $testArrayList, $testArrayValues);
现在,这里是被调用的 insertValues 函数。
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();
}