Welcome Guest, Not a member yet? Register   Sign In
How to batch insert multiple rows with different data
#1
Smile 
(This post was last modified: 04-28-2018, 07:49 PM by lnzbuyao.)

Hello!  Can someone help me out?  I'm trying to batch insert into my sponsors table.  I have visited multiple help sites, but none of their solutions seem to work on mine.  I'm trying to input the following:

sponsor_id - PK (AUTO_INCREMENT)
firstname
middlename
lastname
baptism_id - FK 

Here's my code for the VIEW
PHP Code:
<div class="tab-pane" id="tab5">
 
   <section id="sponsor-field">
 
       <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 ">
 
           <br>
 
           <div class="col-lg-6">
 
               <label for="number of sponsors">Select the number of your sponsor/s:</label>
 
           </div>
 
           <div class="col-lg-3">
 
               <select id="txtboxnum" name="txtboxnum" class="form-control input-sm col-sm-4 col-md-4 col-lg-4" required>
 
                   <option value="1">1</option>
 
                   <option value="2">2</option>
 
                   <option value="3">3</option>
 
                   <option value="4">4</option>
 
                   <option value="5">5</option>
 
                   <option value="6">6</option>
 
               </select>
 
           </div>
 
           <br/>
 
           <hr>
 
           <div id="txtboxgen" class="form-group">
 
           </div>

 
       </div>
 
   </section>
</
div

Oh yeah, also, my textboxes are generated based on the number selected by the user.  It is done by Javascript.  Here's the code for that as well.

Code for TEXTBOX GENERATION via Javascript
PHP Code:
$("#txtboxnum").change(function(e) {
 
           'use strict';
 
           create($(this).val());
 
       });

 
       function create(param) {
 
           'use strict';
 
           var i;
 
           $("#txtboxgen").empty();
 
           for (0param+= 1) {

 
               $('#txtboxgen').append('<label for="Sponsor#' '">Sponsor #  ' '</label><input type="text" class="form-control input-sm" name="sponsor[' ']["firstname"]" required placeholder="First Name"><br/><input type="text" class="form-control input-sm" name="sponsor[' ']["middlename"]" placeholder="Middle Name"><br/><input type="text" class="form-control input-sm" name="sponsor[' ']["lastname"]" required placeholder="Last Name"><br/>');

 
           }
 
       

Here's my code for the CONTROLLER
PHP Code:
$size $this->input->post('txtboxnum');
 
           $data $this->input->post('sponsor');
 
                       
            $sponsors_query 
$this->baptism_model->insert_sponsors($data);
 
           
            if
($sponsors_query){
//                echo '<script>alert("Successfully added reservation!");</script>';
//                redirect('./home-page/Main/servicerequest');
 
               echo 'YES';
 
           }else{
//                echo '<script>alert("There appears to be something wrong.");</script>';
 
               echo 'NO';
 
           

Here's for my MODEL
PHP Code:
   function insert_sponsors($sponsors){
 
       
        $query 
$this->db->insert_batch('tbldummy'$sponsors);                
        if
($query){
 
            return true;
 
       }else{
 
           return false;
 
       }
 
      
    



Any help is very much appreciated!  Thank you!  Smile
Reply
#2

In your controller, temporarily insert these lines after $data = $this->input->post('sponsor');
PHP Code:
echo '<pre>';
print_r($data);
echo 
'</pre>';
die(); 

I guess one the elements in this array is "txtboxnum". Does your database table have a field with that name? If not, insert_batch won't be able to insert the records.
Reply
#3

No txtboxnum is not a field in my database.  I will try your suggestion and then report back what happens.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB