I have a series of hash
in the form of a,b,c,d,e
and I do it through the following
$hashes = array();
while(($row = mysql_fetch_assoc($sql))) {
$hashes[] = $row['hash'];
}
$_SESSION['hashes'] = implode(',', $hashes); // a,b,c,d,e
My question is how can I add a multiple Insert like below?
INSERT INTO alerts_data (alerts_data_id, alerts_data_hash)
VALUES
('$last insert id', 'hash 1'),
('$last insert id', 'hash 2')
('$last insert id', 'hash 3')
Use Prepared Statements in MySQLi and loop the insert query to insert multiple records.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// prepare and bind
$stmt->prepare("INSERT INTO alerts_data (alerts_data_id, alerts_data_hash) VALUES (?, ?)");
$hashes = array();
while(($row = mysql_fetch_assoc($sql))) {
$hashes[] = $row['hash'];
}
foreach($hashes as $key => $hashe)
{
$stmt->bind_param($key, $hashe['hash']);
$stmt->execute();
}
$stmt->close();
$conn->close();
?>