Thursday 7 February 2013

Insert Multiple Records using Single Query in Joomla

Insert multi-record using single query in Joomla.

 /**
  * Insert the attributes into website.
  *
  * @return boolean If found return true else falase.
  */
 protected function saveAttributes($member_id, $data) 
 {
  // Build the values for multi-row insert.
  $values = array();
  foreach ($data as $attribute_id => $value)
  {
   $values[] = "('".$member_id."','".$attribute_id."','".$value."', '1')";
  }
  $values = implode(',', $values);
  
  // Build the insert query
  $sql = 'INSERT INTO #__bmember_attribute_vals (`mid`, `attribute_id`, `value`, `state`)
    VALUES'.$values;
  
  $db  = $this->getDbo();
  $query = $db->getQuery(true); // Reset the $query variable.
  $db->setQuery($sql);
  $db->query();
 }

No comments:

Post a Comment

Please post any queries and comments here.