배열을 통해 MySQL에 여러 값을 삽입하려고하는데 작동하지 않거나 오류 메시지가 표시되어 어디서 잘못 될지 잘 모르겠습니다. 어떤 도움을 주시면 감사하겠습니다. 여기PHP mysqli insert command
내가 이제 기능을
$testArrayList = array();
$testArrayList[] = 'Account_idAccount';
$testArrayList[] = 'firstName';
$testArrayList[] = 'lastName';
$testArrayValues = array();
$testArrayValues[] = $idAccount;
$testArrayValues[] = $firstName;
$testArrayValues[] = $lastName;
$dbManager->insertValues("User", $testArrayList, $testArrayValues);
를 호출 할 경우, 여기입니다 호출되는 funciton insertValues입니다.
public function insertValues($table, array $cols, array $values) {
$mysqli = new mysqli(DBHOST, DBUSER, DBPASSWORD, DBDATABASE);
$colString = implode(', ', $cols); // x, x, x
$valString = implode(', ', array_fill(0, count($values), '?')); // ?, ?, ?
$sql = "INSERT INTO $table ($colString) VALUES($valString)";
if (!$stmt = $mysqli->prepare($sql))
echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
foreach ($values as $v)
if (!$stmt->bind_param('s', $v))
echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
if (!$stmt->execute())
echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
$stmt->close();
$mysqli->close();
}
당신은뿐만 아니라 생성자에서 대신 각 방법에 대해 한 번씩 mysqli 연결을 초기화한다 : 당신이 그 일을해야 기능이 오류의 무리가 있었다
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();
}
그래서 당신이 얻는 무슨 오류? –
전혀 오류가 없습니다. 데이터베이스에 값이 표시되지 않기 때문에 작동하지 않는 것입니다. 이전에 -> bindParam 함수 주위에 따옴표를 사용하여 놀았을 때 오류가 발생하는 방식과 오류가 발생하지 않는 방식을 알았지 만 값이 없습니다. 데이터베이스 – AlexHeuman
디버깅을 한 적이 있습니까? –