0

At first I was able to save values into the table called "tbl_spareparts" but when i added a foreign key it doesnt save anymore.

tbl_spareparts(unit_id) is referenced to tbl_unitdescription(id).

code is for dynamic fields where you can add more input boxes or remove them.

here is my insert code:

     <?php
  $connect = mysqli_connect("localhost", "root", "", "incidentreport");

   if(isset($_POST['submit_data'])) {
   $description = $_POST['description'];
   $qty = $_POST['qty'];
                                          

   foreach ($description as $key => $value) {
   $save = "INSERT INTO tbl_spareparts(unit_id,description,qty) VALUES ('','".$value."', '".$qty[$key]."')";
    $query =    mysqli_query($connect, $save);
      }

     }


    ?>
Dharman
  • 30,962
  • 25
  • 85
  • 135
cptzero67
  • 11
  • 1

1 Answers1

-1

The empty string '' is not equivalent to NULL in mySql. With your insert you try to put the '' value into the colum and it, probably, doesn't reference any id of the tbl_unitdescription table.

If the unit_id column is nullable try with

$save = "INSERT INTO tbl_spareparts(unit_id,description,qty) VALUES (NULL,'".$value."', '".$qty[$key]."')";

or insert an unit_id value which references an existing id in a row in the tbl_unitdescription table (which works in any case).

Massimo Petrus
  • 1,881
  • 2
  • 13
  • 26