I have been trying to insert multiple rows into the database with bindParam and not using loop in its execution, but it seems that is not working, i Keep getting duplicate primary key error.
TABLE STRUCTURE
id | name | comment | date
$data = array('orange', 'mango', 'apple', 'banana');
$count = count($data);
$mulvalue = array_fill(0, $count, '(?,?,?,?)');
$break = implode (',', $mulvalue);
$ins = $conn->prepare ("INSERT INTO table (id, name, comment, date) VALUES $break)";
$i = 1;
foreach($data as $da){
$id = rand(111111, 999999);
$name = $da;
$comment = 'test me';
$date = date('Y-m-d H:i:s');
$ins->bindParam ($i++, $id, PDO::PARAM_STR);
$ins->bindParam ($i++, $name, PDO::PARAM_STR);
$ins->bindParam ($i++, $comment, PDO::PARAM_STR);
$ins->bindParam ($i++, $date, PDO::PARAM_STR);
}
$ins->execute ();
That the code but is not working, also my primary key is the id column which must have one unique value.