Mysqli can't insert multiple rows

╄→гoц情女王★ 提交于 2020-01-30 02:44:28

问题


$allgames = file_get_contents("https://steamspy.com/api.php?request=all");
$decodeall = json_decode($allgames, true);
foreach($decodeall as $game) {



$sql = "INSERT INTO games (name)
VALUES ('{$game['name']}')";

}

if ($conn->multi_query($sql) === TRUE) {
    echo "New records created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();

When i do this only the first row will be added. How do i insert multiple rows?


回答1:


Just get rid of that multi query thing. Use a prepared statement instead

$stmt = $conn->prepare("INSERT INTO games (name) VALUES (?)");
$stmt->bind_param("s", $name);
foreach($decodeall as $game) {
    $name = $game['name'];
    $stmt->execute();
}
echo "New records created successfully";

Note that your current code with multi_query won't work as intended anyway, even with that silly typo fixed. You will have the result of only first query, having no idea what happened to all others.




回答2:


You are overwriting the query each time. Try setting sql to blank then appending it each time in the loop.

Try this:

$sql = array();
foreach($decodeall as $game) {

$sql[] = "INSERT INTO games (name) VALUES ('{$game['name']}')";
}

$sqlInserts = implode(';', $sql);

if ($conn->multi_query($sqlInserts) === TRUE) {
    echo "New records created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}



回答3:


You don't need to perform the query multiple times like that, you can do it all in a single query without multi_query(). You can perform many INSERTs with a single query, like this

// Initialize the query-variable
$sql = "INSERT INTO games (name) VALUES";

// Loop through results and add to the query
foreach ($decodeall as $game) {
    $sql .= " ('".$game['name']."'),";
}

// Remove the last comma with rtrim
$sql = rtrim($sql, ',');

// Perform the query
if ($conn->query($sql) === TRUE) {
    echo "New records created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

This will generate a query resembling

INSERT INTO games (name) VALUES ('One'), ('two'), ('Three')

which will insert the values One, Two and Three into separate rows.

This query will break if your $game['name'] variables contain an apostrophy ', so at the very least you should use $mysqli::real_escape_string(), although a prepared statement takes care of that and prevents SQL injection (so I recommend you go for that instead). See How can I prevent SQL injection in PHP?

Using a prepared statement - the better solution
The preferred method of executing a query is by using a prepared statement.

Fetch all the columns using array_column() and loop the array while calling the execute method until finished.

$stmt = $conn->prepare("INSERT INTO games (name) VALUES (?)");
$stmt->bind_param("s", $name);
foreach (array_column($decode, "name") as $name) {
    $stmt->execute();
}


来源:https://stackoverflow.com/questions/40207175/mysqli-cant-insert-multiple-rows

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!