使用foreach循环从数据库插入数据

时间:2012-11-29 01:00:06

标签: php mysql

我正在尝试编写一个代码,我可以使用foreach循环将数据插入到数据库中,在没有任何循环的常规数据库插入之后

我的代码的问题是常规数据库插入工作正常但我的foreach循环没有插入数据库

我错过了什么吗?

这是我的代码

      <form class="form form-inline" method = "post">
        <table>
          <tr>
            <td width="80%"><h2 style="display:inline">Create Report</h2></td>
            <td><button type="submit" value = "submit" id="submit" name="submit" class="btn btn-primary pull-right">Save Report</button></td>
          </tr>
        </table>
        <div class="row-fluid">

          <div style="width:490px;padding:10px;background:#ddd;margin:10px 0 20px 0">
            <h6>Report Name</h6><input name = "rname" type="text" style="width:455px" id = "rname" value="">

          </div>
          <h3>Report Fields</h3>
          <table cellpadding='4'>
            <tr>
              <td><label>Field</label></td>
              <td><select name="rfield">
                  <option value="">--Select Field--</option>
                  <?php
                              $sc2=$corelib->get_rf();
                              foreach($sc2 as $sc){
                              ?>
                  <option value="<?php echo $sc[advsearch_name]; ?>"><?php echo $sc[advsearch_name]; ?></option>
                  <?php
                              }
                              ?>
                </select>
                </td>
                <td>
                <button type="submit" value = "submit" id="add" name="add" class="btn pull-right">Add Field</button>
                </td>
            </tr>
          </table>
        </form>

$corelib->view_rfsession();
$corelib->save_rf();

的functions.php

function view_rfsession()
    {
        if(isset($_POST['add']))
        {
            $_SESSION['temp'][]=$_POST['rfield'];   
        }
        if(isset($_SESSION['temp']))
        {
            ?>
            <table width="100%" border="0" class = "table">
            <?php
            foreach($_SESSION['temp'] as $key => $temp)
            { 
            ?>
                <tr>
                <td width="80%">
                <?php echo $temp; ?> 
                </td>
                <td><a href="removerf.php?id=<?php echo $key; ?>" rel="tooltip" title="remove" class="link"><i class="icon-remove"></i></a></td>
                </tr>
            <?php
            }
        ?>
        </table>
        <?php
        }
    }   



    function save_rf()
    {
    if(isset($_POST['submit']))
        {
            $rname = $_POST['rname'];


            $query = "INSERT into reports (report_name) value ('$rname')";
            DB::query($query) or die (mysql_error());

                $report_id = mysql_insert_id();

            foreach($_SESSION['temp'] as $key => $abc)
            { 
            $tempcol= DB::query("SELECT scol_id from searchcolumn where advsearch_name = '$abc'");

                DB::insert("report_fields", array(
                "report_id"=>$report_id,
                "field_id"=>$tempcol,
                "report_order"=>$key,
                ));
            }
        }   
    }

我的函数save_rf()有问题,我似乎无法使其正常工作

我的第一个查询工作“$ query =”INSERT into reports(report_name)value('$ rname')“;             DB :: query($ query)或die(mysql_error());“

但第二个没有。带有foreach循环的那个

感谢

0 个答案:

没有答案
相关问题