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

    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置