Reputation: 379
hello all i am having a variable $document_ids=1,2,4,5,8,99,102 and i need to insert each of them individually to my database table like
$shaeredata=mysqli_query
($conn,"insert into docs (dcid,pid) values
('$dcid','$pid'),('$dcid2','$pid'),('$dcid3','$pid'),('$dcid4','$pid')");
//dcid is each id from the variable $document_ids=1,1022,4,5,8,99,102
//$dcid=1,dcid2=1022,$dcid3=4 ...... pid is same for all
the problem is that how do i put the values to database as i dont what document_id is going to be there it can be any seperated by coma.
$document_id=Array
(
[0] => 1
[1] => 4
[2] => 5
[3] => 999
[4] => 6
[5] => 7
[6] => 8
) // i have converted it to an array all of these are $dcid $pid=23 for all
Upvotes: 0
Views: 50
Reputation: 781058
I assume you really mean that $documement_ids is
array(1,2,4,5,8,99,102). If it's a string, you can use
explode` to split it into an array.
You can create an array of all the value pairs, and then use implode
to connect them with commas.
$values = implode(',', array_map(function($dcid) use ($pid) {
return "('$dcid', '$pid')";
}, $document_ids);
$sql = "INSERT INTO docs (dcid, pid) VALUES $values";
mysqli_query($conn, $sql);
Upvotes: 1