• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do multiple inserts in database?

#1
[eluser]Unknown[/eluser]
Hello, I can't do a multiple inserts in database from array.

The array $user['user'] is confromed like a list. No other data inside the array only the niks.

So I need insert that list in database.

I have:
Code:
$users = $_POST['users'];
$users = trim(strtolower($users));
$users = preg_replace("/[^a-z0-9, \"']/", '', $users);
$users = preg_replace("/,[^a-z0-9]*,/", ',', $users);
$users = rtrim($users, ',');
$users = explode(',', $users);
        
$this->load->model('add_users','', TRUE);
$this->add_users->insert_users($users);

Code:
function insert_users($users)
    {
        //INSERT USERs
        foreach ($users as $user):
        $data = array(
               'user_id' => 'NULL' ,
               'user_nick' => $user
            );

        $this->db->insert('table_users', $data);
        endforeach;
    }

#2
[eluser]Mirage[/eluser]
Sooo - what's the problem? Any errors?

-m

#3
[eluser]drewbee[/eluser]
Active record doesn't support inserting multiple records with one query. you have to loop over it each time and insert. Or just run a normal query if you are worried about performance and multiple inserts.

#4
[eluser]Unknown[/eluser]
It's solved x3.

Thanks anyway.

PD: It's possible insert multiple records with one query
Code:
foreach ($users as $user):
        $data = array(
               'image_id' => $id,
               'image_tag' => $user
            );

        $this->db->insert('table_users', $data);
        endforeach;

#5
[eluser]drewbee[/eluser]
Sorry. I didn't mean to confuse you. What your doing above is inserting multiple records with multiple inserts (multiple queries), not with one query. However if there are not that many records to insert and it doesn't happen all that often, this is just fine doing it this way. If you were worried about performance though that's when it would come into play.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.