dsfe167834 2018-03-18 02:37
浏览 101

将复选框对应的数量插入数据库

I have multiple checkbox in my form and the person need to input the quantity of the types of item that is selected. Now, my problem is that I can't get the data to be inserted into database.

This is my add_record.php code:

<?php  
include("connect.php");
include("header.php");
$sql_student = "SELECT * FROM student";
$result_student = mysql_query($sql_student); 
 ?> 

<form method="post" id="add_form" action="add_record.php">
 <label>Name</label>
    <input placeholder="Enter Student Name" type="text" name="name" id="name" class="form-control" />
    <br />
    <input placeholder="Enter Student ID" type="text" name="stud_id" id="stud_id" class="form-control" />
    <br />
    <?php 
       $sql_baggage = "SELECT * FROM baggage";
       $result_baggage = mysql_query($sql_baggage);
    ?>
    <label>Bag Types</label></br>
    <table style="border:none;">
    <?php while($row_bag = mysql_fetch_array($result_baggage))
    {
       $baggage_id = $row_bag['baggage_id'];
    ?>
    <tr>
        <td><?php echo $row_bag['baggage_id'];?>
        <td><?php echo $row_bag['baggage_type'];?></td>
        <td><input type="checkbox" name="tick[]" value="<?php echo $baggage_id;?>"/></td>
        <td><input type="text" size="2" name="txt[<?php echo $baggage_id;?>]" placeholder=" "></td>
    <?php
    ?></td></tr>
    </table>
    <br />
    <input type="submit" name="submit" id="submit" value="Add Record" class="btn btn-success btn-secondary pull-right" />
</form>
<?php
if(isset($_POST['submit']))
{
   $name = $_POST["name"]; 
   $stud_id = $_POST["stud_id"];    
   $stu_query = "INSERT INTO student(student_id,student_name) VALUES ('$stud_id','$name')";

   if(mysql_query($stu_query))
   {
      if(!empty($_POST['tick']))
      {
        foreach($_POST['tick'] as $selected)
        {
          $qty = $_POST['txt'][$selected];
          $inv_query = "INSERT INTO inventory (invstu_id,invbag_id,invbag_quantity) VALUES 
          ('$stud_id','$selected', '$qty')";

          if(mysql_query($inv_query))
          {
            echo'<script>alert("A record has been inserted!")</script>';
          }
          else
          {
            echo "Database error";
          }
        } 
      }
      else
      {
        echo'<script>alert("A record has been inserted!")</script>';
      }
  }
}
?>
</body>
</html>

I know that the data is passed through foreach function since I get the echo of database error two times when I tick two of the checkbox. However, the value is not inserted into the database.

  • 写回答

1条回答 默认 最新

  • duanji8615 2018-03-18 07:10
    关注

    Finally solve the issue by echoing the mysql_error(), there is nothing wrong with the code. Just a bit problem at the database. Thanks!!

    评论

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器