mardi 17 mai 2016

How to insert multiple value using checkbox with for loop?

I have made a form with multiple checkbox.I can check as many as i want and save it into database. But i have some problems with the value that i inserted that is quantity value.

    <?php
    $i=0;
 while ($row = oci_fetch_array($stid))
{
    $medicine_ID=$row["MEDICINE_ID"];
    $medicine_Name=$row["MEDICINE_NAME"];
    $description=$row["DESCRIPTION"];
    $stock_Quantity=$row["STOCK_QUANTITY"];
    $expired_Date=$row["EXPIRED_DATE"];
?>

         <td height="30"><div align="center">
         <input type="checkbox" name="MEDICINE_ID[]" value="<?php echo $row['MEDICINE_ID'] ?>" id="check_item" align="middle" />
          </div></td>

         <th scope="row" >

         <?php
         echo $row ["MEDICINE_ID"];
         ?>        
         </th>  

  <td align="center">
  <?php
    echo $row ["MEDICINE_NAME"];
  ?>      </td>

  <td align="center">
  <?php
    echo $row ["STOCK_QUANTITY"];
  ?>      </td>
  <td><label>
    <input name="quantity[]" type="number" id="quantity" value="" size="10" />
    </label></td>


              </tr>
              <?php
$i++;
}
?>
            <br />
            </table>
            <br />
             <input type="submit"   name="Submit" value="Save" title="Hantar"/> <br />
            </form>

    </table>

This is my process that has for loop process. For example,i have five rows in my checkbox form and when i tick all of them and key in the quantity that i want, the data succesfully inserted into database but when i tick the second and the third data and key in the quantity without tick on the first row, the quantity inserted into database is null. It seems that i cannot misstick my first row data. Can someone help me to fix this?

    <?php
 $conn = oci_connect("megat", "megat", "orcl");
 echo $matric_No = $_POST['matric_No'];
 $medicine_ID = $_POST['MEDICINE_ID'];
 $quantity = $_POST['quantity'];
 echo $dates =  $_POST['dates'];

 $size_medicine=sizeOf($medicine_ID);

 for($i=0;$i<$size_medicine;$i++){
    $statement="insert into stud_med(quantity,matric_No,medicine_ID,dates) 
 VALUES('$quantity[$i]','$matric_No','$medicine_ID[$i]',to_date('$dates','yyyy-mm-dd'))";
    $state = oci_parse($conn,$statement );
    oci_execute($state);

}

echo "<script>alert ('Item insert successfully');     window.location.assign('view1.php');
        </script>";


//oci_close($conn);
     ?>

Aucun commentaire:

Enregistrer un commentaire