行插入复选框

时间:2017-06-05 06:14:40

标签: php mysql

我的表格如下:

<form method="post" action="insertassign_process.php">
             <table class="table table-bordered table-responsive">

        <?php
        echo"<thead>
                <tr><th>Tenant ID</th>
                    <th>Tenant ZIP</th>
                    <th>Process server ID</th>
                    <th>Process Server Name</th>
                    <th>Phone Nmuber</th>
                    <th>Select Process Server</th>
                    <th>Assign Proces Server</th>
                    </tr>   
              </thead>";
        include("connection.php");
        extract($_REQUEST);
            $SQLSELECT = "SELECT * FROM users where zip_codes='$zip'";
            $result_set =  mysql_query($SQLSELECT) or die(mysql_error());
            while($row = mysql_fetch_array($result_set))
            {
            ?>

                <tr>
                    <td><input type="text" readonly name="t_id[]" value="<?php echo $t_id; ?>"/></td>
                    <td><input type="text" readonly name="zip[]" value="<?php echo $zip; ?>"/></td>
                    <td><input type="text" readonly name="pr_id[]" value="<?php echo $row['id']; ?> "></td>
                    <td><input type="text" readonly name="fullname[]" value="<?php echo $row['first_name'].' '.$row['last_name']; ?>"</td>
                    <td><?php echo $row['phone_no']; ?></td>
                    <td><input type="checkbox" name="name[]" select="selected" value="<?php echo $row["t_id"]; ?>" /></td>
                    <td><input type="radio" name="assign_process" value="1"  /></td>
                </tr>
            <?php
            }
        ?>

    </table><button type="submit" class="btn btn-primary button-loading" name="submit" data-loading-text="Loading...">Submit</button></form>

现在,当我选中任何复选框(可能是多选)时,该行的所选数据将插入另一个表。我的插入查询是:

        <?php
        session_start();
         include("connection.php");
        extract($_SESSION);
        extract($_POST);
        if(isset($_POST["submit"])==true)
        {
        $counter = count($_POST["name"]); /* COUNT THE PASSED ON NAME */

        for($x=0; $x<=$counter; $x++){

        if(!empty($_POST["name"][$x])){
       $CUSTOMER_NAME = mysql_real_escape_string($_POST["t_id"][$x]);
      $PI_ADDRESS = mysql_real_escape_string($_POST["pr_id"][$x]);


       $qry="INSERT INTO select_process (id,tenant_select_id,process_select_id) VALUES ('','$CUSTOMER_NAME','$PI_ADDRESS')";
      mysql_query($qry);

      } /* END OF CHECKING THE CHECKBOX IF SELECTED */

      } /* END OF FOR LOOP */
      }
      else{

       echo"error";
       }
      ?>

但是,当我选择多行或任何行时,它只插入第一行数据。

1 个答案:

答案 0 :(得分:-1)

您正在尝试将空值插入到ID中,我认为您的id字段是主键,因此它不能重复,我建议将您的id键设置为主要自动增量,并像这样编写代码 -

var empllist = (from c in Db.Company
                        select new Company
                        {
                            CompID = c.ID,
                            CompanyName = c.Name,
                            EmplList =  Db.Employee.where(k=>k.EMPID =c.CompID ).select(m=> new Employees{EMPID = m.EMPID,EMPNAME = m.EMPNAME }).ToList()
                        }).ToList();