你好所有我有一个变量$ document_ids = 1,2,4,5,8,9,9,102我需要将它们分别插入到我的数据库表中,如
$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
问题是我如何将值放到数据库中,因为我不会将document_id放在那里,它可以被昏迷分开。
$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
答案 0 :(得分:1)
我假设你真的是指$documement_ids is
数组(1,2,4,5,8,99,102). If it's a string, you can use
爆炸`将它拆分成数组。
您可以创建一个包含所有值对的数组,然后使用implode
将它们与逗号连接。
$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);