0

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.

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I understand, but you should use models to access all the database class methods available for models... Otherwise you would be doing all the DB interactions as if without the framework: without data sanitation, value escaping, etc...
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.