Php 如何将与comas分离的数据添加到数据库中
我想将数据插入数据库。我有用逗号分隔的数据 例如,我有如下数据:Php 如何将与comas分离的数据添加到数据库中,php,mysql,implode,Php,Mysql,Implode,我想将数据插入数据库。我有用逗号分隔的数据 例如,我有如下数据: $rand_post = ["3001182708", "3001182713", "3001183215"]; $id_post = '123456'; 这是我在网上搜索后得到的最好答案 $prep = array(); foreach($rand_post as $k => $v ) { $prep[':'.$k] = $v; } print_r($prep); $sth = $db->prepare(
$rand_post = ["3001182708", "3001182713", "3001183215"];
$id_post = '123456';
这是我在网上搜索后得到的最好答案
$prep = array();
foreach($rand_post as $k => $v ) {
$prep[':'.$k] = $v;
}
print_r($prep);
$sth = $db->prepare("INSERT INTO tes (`datas`) VALUES (" . implode('), (',array_keys($prep)) . ")");
$res = $sth->execute($prep);
但它只是为$rand\u post
我想像这样插入我的数据
id_post rand_post
============ ================
123456 3001182708
123456 3001182713
123456 3001183215
试着从飞溅中回答
id_post rand_post
============ ================
123456 2147483647
123456 2147483647
123456 2147483647
在循环中配对
$prep = array();
foreach($rand_post as $v ) {
$prep[] = "($id_post, $v)";
}
print_r($prep);
$sth = $db->prepare("INSERT INTO tes (`id_post`,`datas`) VALUES " . implode(', ', $prep));
// INSERT INTO tes (`id_post`,`datas`) VALUES (123456, 3001182708), (123456, 3001182713), (123456, 3001183215)
$res = $sth->execute($prep);
我得到了这个错误致命错误:未捕获错误:调用C:\xampp\htdocs\ainodorama\jy adm\tes.php中字符串上的成员函数execute():13堆栈跟踪:#0{main}在C:\xampp\htdocs\ainodorama\jy adm\tes.php的第行抛出13@Jazuly现在获取它,我丢失了
$db->prepare
它从$rand_post记录了相同的值,当我尝试它时,只需记录2147483647
并循环它3次。2147483647
甚至不记录我的数据。我甚至不知道它的位置。正如您所看到的,代码从变量$rand\u post和$id\u post获取值