基本上,我从 pdf
文件中创建了一个字符串,该字符串包含以逗号分隔的值,我现在需要将这些值插入到表中的不同列中。
我会尝试类似问题的答案 here ,但我没有得到它来满足我的需要。
我已经尝试将该字符串分解为一个数组,因此逗号是每个元素的分隔符,如下所示:
$array = explode(',', $text);
但是因为我在表中只有 3 列,所以我需要将这些值排序到这些列中,这样每个第三个字符串都会进入第一列,所有跟随它进入后续列。我试着这样做:
$duzina=count($array);
for($i=0;$i<$duzina;$i++) {
if ($i = 0 && $i < 3) {
$mysqli->query("INSERT INTO `test` (`name`, `surname`, `email`) VALUES
(
'" . trim($array[$i]) . "',
'" . addslashes($array[$i + 1]) . "',
'" . addslashes($array[$i + 2]) . "'
)
");
}
if ($i/3 == 0) {// has found a 3rd,6th,9th element of an $array
$mysqli->query("INSERT INTO `test` (`name`, `surname`, `email`) VALUES
(
'" . trim($array[$i]) . "',
'" . addslashes($array[$i + 1]) . "',
'" . addslashes($array[$i + 2]) . "'
)
");
} else {
$mysqli->query("INSERT INTO `test` (`name`, `surname`, `email`) VALUES
(
'" . trim($array[$i]) . "',
'" . addslashes($array[$i + 1]) . "',
'" . addslashes($array[$i + 2]) . "'
)
");
}
但它所做的只是插入 $array
的前 3 个元素。请帮忙
最佳答案
除法的其余部分不是 %
而不是 /
,所以替换为:
if ($i/3 == 0) {
与
if ($i%3 == 0) {
关于php - 如何将字符串插入数据库表中的不同列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35554385/