anujay kumar
anujay kumar

Reputation: 11

how to post data from array to database

Hello coders. I have a problem with inserting data into a database, can you please help me with the controller function? Here is my php form:

<form method='post' action='<?php echo site_url('a3_bus_system/output')?>'>
    <div class="_25">
        <strong>Route Name/Number</strong>
        <br/>
        <input type="text" name=""></input>
    </div>

        <p>&nbsp;<p>&nbsp;</p></p>

        <p>&nbsp;<p>&nbsp;</p></p>
        </p>

    <div id="div">

    </div>

  <p>&nbsp;</p><div class="_25">

    <p><input type="button" name="button" class="button red" id="button" value="Add"  onclick="generateRow() "/></a></p>
</div>
 <input type='button' value='Remove Button' id='removeButton'>

<p>&nbsp;</p><p>&nbsp;</p></div>

<input type="submit"  class="button blue" id="button" value="Register" />

</form> 
</div>


</div>

<div class="clear height-fix"></div>

        </div></div> <!--! end of #main-content -->
  </div> <!--! end of #main -->
 <script>
 var counter=1;
    function generateRow() {
    var count="<font color='red'>"+counter+"</font>";
   var temp ="  <p>&nbsp;&nbsp;&nbsp;&nbsp;<div class='_25'><input type='textbox' id='textbox' name='stop["+counter+"]' placeholder='Stop Name'></input></div>&nbsp;&nbsp;&nbsp;<div class='_25'><input type='textbox' id='textbox' name='timing["+counter+"]' placeholder='Timing'></input></div>&nbsp;<div class='_25'><select id='ampm' name='ampm["+counter+"]'><option>a.m</option><option>p.m</option></select>  </div>";

var newdiv = document.createElement('div');
newdiv.innerHTML = temp + count;

var yourDiv = document.getElementById('div');

yourDiv.appendChild(newdiv);
counter++;
    }
    </script>

adn this is my controller function :

public function output()
    {
    $common['common']=$this->common;
    $this->load->helper('form');
    $this->load->database();


    foreach ($_POST['stop'] as $stopIndex => $stopValue) {

    if($stopValue!=NULL)
    {

    echo $stopIndex;
    echo $stopValue;

        }
    }
    foreach ($_POST['timing'] as $timingIndex => $timingValue)
    {
    if($timingValue!=NULL)
    {
    echo $stopValue['data'];
    }


}
foreach ($_POST['ampm'] as $ampmIndex => $ampmValue) {

  if($timingValue!=NULL)
    {
  echo $ampmValue;
  }
}

    $this->output->enable_profiler(TRUE);
    }   

And these are my database fields:

route_number   stop_name   am_pm   timing

Please give me a way to insert a query to post all these input fields into a database.

Upvotes: 0

Views: 116

Answers (2)

asvignesh
asvignesh

Reputation: 788

You cant post array in Database directly... Serialize the data before inserting and Unserialize after fetching

Upvotes: 0

jakobhans
jakobhans

Reputation: 876

Do you have the model already made? In Codeigniter you are supposed to do all your direct DB activities with a model. All you need to know about models is explained here: http://ellislab.com/codeigniter/user-guide/general/models.html

Once there you would do the insert like explained here: http://ellislab.com/codeigniter/user-guide/database/examples.html Eventhough I always use the Active Record Class way (http://ellislab.com/codeigniter/user-guide/database/active_record.html), it's simpler and the abstraction is really helpful.

Upvotes: 1

Related Questions