【问题标题】:How to insert string into different columns in database table?如何将字符串插入数据库表中的不同列?
【发布时间】:2016-06-03 22:09:31
【问题描述】:

基本上我已经从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 的前三个元素。请帮忙

【问题讨论】:

  • 将 $i/3 == 0 转换为 $i%3 == 0
  • 如果你使用$i += 3而不是$i++,你可能只需要1个插入块并且根本不需要条件。不过,我必须查看数据才能确定...

标签: php mysql arrays sql-server string


【解决方案1】:

使用准备好的语句。
重构您的数据以在代码中有意义(对调试有用)
明智地循环(增加 3)

注意,下面的代码未经现场测试,只是概念证明。

$array = loadpdfwhatever();
$length = count($array);

// Restructure the raw data to be meaningful to a human. Can be skipped,
// but this will help you debugging in + 3 weeks from now :-)
$restructured = [];
for($i=0;$i<$length;$i+=3) { // note the $i+3 to increment by 3.
    $restructured[] = ['name'=> $array[$i],
                       'surname'=> $array[$i+1],
                       'email'=> $array[$i+2]];
}


// Using prepared statements we only have to build up the query once.
// Makes code faster and more efficient.
if ($stmt = mysqli_prepare($link, "INSERT INTO `test` (`name`, `surname`, `email`) VALUES ( ?, ?, ? ) ")) {
    foreach($restructured as $insert) {
        mysqli_stmt_bind_param($stmt,'sss',$insert['name'],
                                           $insert['surname'],
                                           $insert['email']); 
        mysqli_stmt_execute($stmt)
    }
}

【讨论】:

  • 我无法告诉你我是多么感谢你的回答 :)
  • 不客气。在导入原始数据时吸取教训,使其对您有意义。不是他的代码。对你来说,当你在 6 个月后回来时:-)。当您采用这种方法时,您会更容易弄清楚。
【解决方案2】:

两件事:

首先使用 if / elseif /else

if ($i = 0 && $i < 3) {
    ...
}
elseif ($i%3 == 0) {
    ...
}
else
{
    ....
}

第二:

它是$i%3 == 0 而不是$i/ 3 == 0

【讨论】:

    【解决方案3】:

    没有其他部门得到%而不是/,所以替换这个:

    if ($i/3 == 0) {
    

    if ($i%3 == 0) {
    

    【讨论】:

    • 它仍在插入第一个树元素,超过 1200 次。而且我不知道我的脚本是如何做到的,因为所有插入都受到$array 中元素数量的限制,这部分代码:for($i=0;$i&lt;$duzina;$i++)$duzinacount($array)..
    • @Ognj3n 你能提供你的数组吗?
    • 或者 $duzina 等于什么?
    • 非常感谢大家,Michael Dibbets 已经解决了我的问题 :)
    猜你喜欢
    • 1970-01-01
    • 2013-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-09
    • 1970-01-01
    • 2019-07-18
    相关资源
    最近更新 更多