1
timgno
Inserting multiple rows in database table
  • 2014/4/19 9:34

  • timgno

  • Module Developer

  • Posts: 1504

  • Since: 2007/6/21


We can't insert in the database of xoops multi row?

I can't find a function that can do it in class objects

I think you should create such a thing with a function of the type insertMultipleRows()

2
Mamba
Re: Inserting multiple rows in database table
  • 2014/4/19 9:50

  • Mamba

  • Moderator

  • Posts: 11366

  • Since: 2004/4/23


Why? What would be the "use case" for it?

You can currently create an array, and then run a loop like "foreach" and insert each element into the DB.
Support XOOPS => DONATE
Use 2.5.10 | Docs | Modules | Bugs

3
timgno
Re: Inserting multiple rows in database table
  • 2014/4/19 10:09

  • timgno

  • Module Developer

  • Posts: 1504

  • Since: 2007/6/21


Unfortunately it does not work

Have a look at this code

if ($table_nbfields 0) {
            
$datas = array();     
            for( 
$i 0$i $table_nbfields$i++ ) {                    
                
$datas = array('field_mid' => $table_mid[$i], 
                                
'field_tid' => $table_id[$i],                                
                                
'field_numb' => $table_nbfields[$i], 
                                
'field_name' => $_POST['field_name'][$i], 
                                
'field_type' => $_POST['field_type'][$i], 
                                
'field_value' => $_POST['field_value'][$i], 
                                
'field_attribute' => $_POST['field_attribute'][$i], 
                                
'field_null' => $_POST['field_null'][$i], 
                                
'field_default' => $_POST['field_default'][$i], 
                                
'field_key' => $_POST['field_key'][$i],    
                                
'field_autoincrement' => (($_REQUEST['field_autoincrement'][$i] == 1) ? '1' '0'),
                                
'field_element' => $_POST['field_element'][$i],                            
                                
'field_inlist' => (($_REQUEST['field_inlist'][$i] == 1) ? '1' '0'),
                                
'field_inform' => (($_REQUEST['field_inform'][$i] == 1) ? '1' '0'),
                                
'field_admin' => (($_REQUEST['field_admin'][$i] == 1) ? '1' '0'),
                                
'field_user' => (($_REQUEST['field_user'][$i] == 1) ? '1' '0'), 
                                
'field_block' => (($_REQUEST['field_block'][$i] == 1) ? '1' '0'), 
                                
'field_main' => (($i == $_REQUEST['field_main']) ? '1' '0'), 
                                
'field_search' =>  (($_REQUEST['field_search'][$i] == 1) ? '1' '0'), 
                                
'field_required' => (($_REQUEST['field_required'][$i] == 1) ? '1' '0')
                                );
                                                    
            }    
            foreach (
$datas as $data => $value) {                
                
$obj->setVars(array($data$value));
                
$fieldsHandler->insert($obj);                        
            }
        }

4
Mamba
Re: Inserting multiple rows in database table
  • 2014/4/19 12:41

  • Mamba

  • Moderator

  • Posts: 11366

  • Since: 2004/4/23


Quote:
Unfortunately it does not work

What doesn't work? What errors are you getting?
Support XOOPS => DONATE
Use 2.5.10 | Docs | Modules | Bugs

5
timgno
Re: Inserting multiple rows in database table
  • 2014/4/19 14:55

  • timgno

  • Module Developer

  • Posts: 1504

  • Since: 2007/6/21


I'm not getting any error ...!

They just do not write the data in the database,

even if I get the message script

"Succerfully saved"

6
Mamba
Re: Inserting multiple rows in database table
  • 2014/4/19 15:39

  • Mamba

  • Moderator

  • Posts: 11366

  • Since: 2004/4/23


Did you try to debug it?

I am not sure I understand the code. You declare the datas as an array, but you don't populate it as an array, and then you try to use it as an array when you populate the DB

Shouldn't it be something like:

$datas[$i] = array('field_mid' => $table_mid[$i],
Support XOOPS => DONATE
Use 2.5.10 | Docs | Modules | Bugs

7
timgno
Re: Inserting multiple rows in database table
  • 2014/4/19 16:05

  • timgno

  • Module Developer

  • Posts: 1504

  • Since: 2007/6/21


Do not change anything!
I'm afraid that's something else

For example, the data are not loaded by the browser

From these pictures you can see what I get

Resized Image

Resized Image

8
zyspec
Re: Inserting multiple rows in database table
  • 2014/4/19 17:38

  • zyspec

  • Module Developer

  • Posts: 1095

  • Since: 2004/9/21


Looks to me like your foreach loop is incorrect.

foreach ($datas as $data => $value) {                 
                
$obj->setVars(array($data$value)); 
                
$fieldsHandler->insert($obj);                         
            }


Should be:
foreach ($datas as $data => $value) {
                
$obj->setVars($value); 
                
$fieldsHandler->insert($obj);                         
            }


It would also help to see your db table definitions and/or the class used to create the $obj object. For instance, do you have a db column that is 'unique' to use as a key? Seems to me like you should really create an object ($obj) in the last foreach loop and then destroy it after you insert it - just to make sure you don't inadvertently insert 'remnants' from the previous object's contents - just a thought.

9
timgno
Re: Inserting multiple rows in database table
  • 2014/4/19 18:55

  • timgno

  • Module Developer

  • Posts: 1504

  • Since: 2007/6/21


Thank you for your suggestion zyspec, but does not work the same

As I said before probable lack of passing data to the browser

I'm not getting any syntax error, so I'll have to use other tools to check

@zyspec

If you want to take a look at the module code is in SVN

WARNING: This module is pre-alpha and not for test

10
Mamba
Re: Inserting multiple rows in database table
  • 2014/4/19 19:54

  • Mamba

  • Moderator

  • Posts: 11366

  • Since: 2004/4/23


Quote:
I'm not getting any syntax error, so I'll have to use other tools to check

Very often syntax is not the issue, but the logic in our thinking....

Just set breaks in your debugger, and compare the values there with what you expect. I am sure that very quickly you'll find a bug in your logic
Support XOOPS => DONATE
Use 2.5.10 | Docs | Modules | Bugs

Login

Who's Online

153 user(s) are online (82 user(s) are browsing Support Forums)


Members: 0


Guests: 153


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Mar 31
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits