【问题标题】:how to post data from array to database如何将数据从数组发布到数据库
【发布时间】:2013-04-09 19:26:44
【问题描述】:

你好编码员。我在将数据插入数据库时​​遇到问题,您能帮我处理一下控制器功能吗? 这是我的 php 表单:

<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>

这是我的控制器功能:

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);
    }   

这些是我的数据库字段:

route_number   stop_name   am_pm   timing

请给我一种插入查询的方法,将所有这些输入字段发布到数据库中。

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    你的模型已经做好了吗?在 Codeigniter 中,您应该使用模型进行所有直接的数据库活动。您需要了解的有关模型的所有信息都在此处进行了说明:http://ellislab.com/codeigniter/user-guide/general/models.html

    在那里,您可以像这里解释的那样进行插入:http://ellislab.com/codeigniter/user-guide/database/examples.html 尽管我总是使用 Active Record 类方式 (http://ellislab.com/codeigniter/user-guide/database/active_record.html),但它更简单,而且抽象非常有用。

    【讨论】:

    • 我明白,但是您应该使用模型来访问模型可用的所有数据库类方法...否则,您将像没有框架一样进行所有数据库交互:没有数据卫生,价值转义等等……
    【解决方案2】:

    您不能直接在数据库中发布数组... 插入前序列化数据 并在获取后反序列化

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-02
      • 1970-01-01
      • 1970-01-01
      • 2021-10-22
      • 2021-11-13
      • 1970-01-01
      • 2017-02-07
      • 2016-10-22
      相关资源
      最近更新 更多