1

Hi i have an app on Joomla + php + Mysql. The array passed into function where it will be insert into mysql database. Following are my code.

public function setItems($params,$type)
{
$search_id = 1;
$query = $db->getQuery( true );
$columns = array('query_type','query_name','search_id');
foreach($params['banks'] as $key=>$value){
            $values = array($db->quote('bank'), $db->quote($value), $db->quote($search_id));
            $query->insert($db->quoteName('#__bank_parameters'))
              ->columns($db->quoteName($columns))
              ->values(implode(',',$values));
             $db->setQuery($query);
             $db->query();

        }

}

The output of params as follow:

Array
(
    [0] => bank1
    [1] => bank2
)

The issues now, i am only getting one data insertion, not 2 as how the params above has. What might be missing here? Thanks

3
  • 1
    don't know this query builder but might it be possible that you need to call $query = $db->getQuery( true ); again inside the loop before the second go round? Commented May 23, 2013 at 3:03
  • Where is $db defined? Commented May 23, 2013 at 3:09
  • A lot of times actually you want to do $query->clear(); Commented May 26, 2013 at 0:15

3 Answers 3

2

Insert and Columns should be used once, Values multiple times:

// Create the base insert statement.
$query
   ->clear()
   ->insert(    $db->qn('#__bank_parameters'))
   ->columns(   array('query_type', 'query_name', 'search_id'))
;

foreach ($params['banks'] as $key => $value)
{
    $query->values(implode(', ', $db->q(array('bank', $value, $search_id))))
}

// Set the query and execute the insert.
$db->setQuery($query);
Sign up to request clarification or add additional context in comments.

Comments

0

Found an solution for above problems.

foreach($params['banks'] as $key=>$value){
            $query = $db->getQuery( true );
            $columns = array('query_type','query_name','search_id');
            $values = array($db->quote('bank'), $db->quote($value), $db->quote($search_id));
            $query->insert($db->quoteName('#__bank_parameters'))
              ->columns($db->quoteName($columns))
              ->values(implode(',',$values));
             $db->setQuery($query);
             $db->query();

        }

Thanks to Orangepill

Comments

0

Create object of db $db = JFactory::getDbo();

  public function setItems($params,$type)
        {
        $search_id = 1;
        $db = JFactory::getDbo();
        $query = $db->getQuery( true );
        $columns = array('query_type','query_name','search_id');
        foreach($params['banks'] as $key=>$value){
                    $values = array($db->quote('bank'), $db->quote($value), $db->quote($search_id));
                    $query->insert($db->quoteName('#__bank_parameters'))
                      ->columns($db->quoteName($columns))
                      ->values(implode(',',$values));
                     $db->setQuery($query);
                     $db->query();

                }

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.