简体   繁体   English

Joomla 3通过多维数组循环进行数据库插入

[英]Joomla 3 loop through multidimensional array for db insert

this is my first question here so please be nice :) 这是我在这里的第一个问题,所以请很好:)

I'm trying to insert multiple records, but I keep getting a mysql error, #1136 Column count doesn't match value count at row 1. I've followed the guide here: http://docs.joomla.org/Inserting,_Updating_and_Removing_data_using_JDatabase 我正在尝试插入多条记录,但我不断收到mysql错误,#1136列数与第1行的值计数不匹配。我在这里按照指南进行操作: http : //docs.joomla.org/Inserting ,_Updating_and_Removing_data_using_JDatabase

Part of my issue is that the columns won't necessarily always be the same in the array. 我的问题的一部分是列在数组中不一定总是相同的。 For example, 'address1' is required, but 'address2' and 'address3' are not. 例如,“ address1”是必需的,但“ address2”和“ address3”不是必需的。 All available fields exist in the db table though, so I just need to specify the columns as keys for each value set. 尽管所有可用字段都存在于db表中,所以我只需要将列指定为每个值集的键。

I feel like I'm missing something quite obvious, but having spent far too many hours at this, I hope someone will be able to easily point it out. 我觉得自己似乎遗漏了一些明显的东西,但是花了太多时间在此上,我希望有人能够轻松指出这一点。

Here's my error: 这是我的错误:
1136 Column count doesn't match value count at row 1 SQL=INSERT INTO jos_supersite_contact 1136列数与第1行的值数不匹配SQL = INSERT INTO jos_supersite_contact

Below is the code I'm using.... 下面是我正在使用的代码。

        foreach ($data[0]['result'] as $results) {
            $db = JFactory::getDbo();
            $db->getQuery(true);
            foreach ($results as $key => $value) {
                $columns[] = $db->quoteName(str_replace('.', '_', $key));
                $rows[] = $db->quote($value);
            }
            $query
                    ->columns($columns)
                    ->values(implode(',',$rows))
                    ->insert($db->quoteName('#__supersite_contact'));
            $db->setQuery($query);
            if (!$db->execute()) {
                throw new Exception($db->getErrorMsg());
            }
        }

You error message itself explain the issue. 您的错误消息本身将说明问题。

The column filed and values fields are not matching in number. 列字段和值字段的编号不匹配。

you already mentioned that address1 is required and address2,3 not so in your query the column have 3 fields every time but value have only address1 when 2 and 3 empty. 您已经提到address1是必需的,而address2,3不是必需的,因此在您的查询中,该列每次​​都具有3个字段,但是当2 and 3空时,value仅具有address1 So make sure all time you have these three fields even those have null value 因此,请确保所有时间都具有这三个字段,即使这些字段具有null

Also I noticed the Query you are using inside the loop it give more load to the DB. 我也注意到循环中使用的查询给数据库带来了更多的负担。 Instead of using like that use like below. 而不是像下面那样使用。

       $db = JFactory::getDbo();
       $columns = "column1,column2,column3";
       //This is what you need to loop.
       $vals    = "('$db->quote($val1)','$db->quote($val2)','$db->quote($val3)')
                  ,('$db->quote($val1)','$db->quote($val2)','$db->quote($val3)')
                  ,('$db->quote($val1)','$db->quote($val2)','$db->quote($val3)')"; 
       $query
               ->columns($columns)
               ->values($vals)
               ->insert($db->quoteName('#__supersite_contact'));
        $db->setQuery($query);
        if (!$db->execute()) {
            throw new Exception($db->getErrorMsg());
        }

This is much effective in multiple row insert situations. 在多行插入情况下非常有效

Hope it make sense.. 希望有道理..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM