mysql 和 codeginiter 的奇怪插入行为

发布于 2024-09-02 00:50:41 字数 1013 浏览 2 评论 0原文

我有一个相当简单的插入语句,

<...>
if (!empty($attributes)) {
            $sql = 'INSERT INTO `part_attrs` (`part_id`, `attr`, `type`, `list_order`) VALUES (?, ?, ?, ?)';
            foreach($attributes as $key => $attribute) {
                $this->db->query($sql, array($partid, $attribute[0], $attribute[1], $key));
                $attrid = $this->db->insert_id();
                echo $attrid.'<br />';

                if (strlen($attribute[2]) > 0) {
                    $values = explode(',', $attribute[2]);
                    $sql = 'INSERT INTO `attr_values` (`attr_id`, `field_values`) VALUES (?, ?)';
                    foreach ($values as $value) {
                        $this->db->query($sql, array($attrid, trim($value)));
                    }
                }
            }
        }
<...>

奇怪的是只有一两行被插入。我将回显行放入以查看每次插入返回的行 ID,因为我没有收到任何错误。例如,如果我插入三个项目,它将返回类似 18、124、128 的内容。其中 18 id 是下一个预期 id,因此该行将被插入,而其余行则不会。有什么想法可能是错的吗?

I have a fairly simple insert statement

<...>
if (!empty($attributes)) {
            $sql = 'INSERT INTO `part_attrs` (`part_id`, `attr`, `type`, `list_order`) VALUES (?, ?, ?, ?)';
            foreach($attributes as $key => $attribute) {
                $this->db->query($sql, array($partid, $attribute[0], $attribute[1], $key));
                $attrid = $this->db->insert_id();
                echo $attrid.'<br />';

                if (strlen($attribute[2]) > 0) {
                    $values = explode(',', $attribute[2]);
                    $sql = 'INSERT INTO `attr_values` (`attr_id`, `field_values`) VALUES (?, ?)';
                    foreach ($values as $value) {
                        $this->db->query($sql, array($attrid, trim($value)));
                    }
                }
            }
        }
<...>

The odd thing is only one or two of the rows are being inserted. I put that echo line in to see the row id's it was returning for each insert since I'm not getting any errors. If for example I insert three items it will return something like 18, 124, 128. Where the 18 id is the next and expected id so this row gets inserted and then the rest don't. Any ideas what might be wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若相惜即相离 2024-09-09 00:50:41

您正在更改第二个 if 语句中 $sql 的值。 124 和 128 是 attr_values 表中的属性。考虑在 if 语句中使用另一个变量名称,或者将第一个赋值移至 foreach 循环内的 $sql(将其向下移动一行) 。

You are changing the value of $sql inside your second if statement. 124 and 128 is attributes from the attr_values table. Consider using another variable name inside your if statement, or move the first assignment to $sql inside the foreach loop (move it down one line).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文