-1

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.

Aire
  • 61
  • 1
  • 1
  • 3
  • 1
    `echo mysql_error();` instead of `Database error` to see what's the problem – Taki Mar 18 '18 at 02:55
  • 1
    You also have syntax error: https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display – Michael Eugene Yuen Mar 18 '18 at 03:01
  • 1
    Stop using `mysql_`, use `mysqli_` or PDO instead to avoid SQL injection: https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – Edward Mar 18 '18 at 03:19

1 Answers1

0

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

Aire
  • 61
  • 1
  • 1
  • 3
  • If you're using PHP's insecure and deprecated mysql_ API, then I would suggest that there's something wrong with the code – Strawberry Mar 18 '18 at 07:51