I have created a check list of products in an html form. The user selects one or more products (by checking the relevant check-boxes) and submits the form. My code below does create distinct INSERT entries in my sql database for every checked product checkbox and the sql table columns receive the right values for date, pay_method, total_cost, user_ID (which in fact are the same for every INSERT entry created).
However the value of product_ID in the sql table (which should be different for every checked checkbox and determined by the for loop in my php code below, as'".$checkboxl[$i]. "' (ie the value of each checked checkbox) does not work and sql receives a "0" product_ID value (instead of the checkbox value). Any ideas on what is missing?
<?php
// Code to establish connection with sql works fine and is followed by this:
if($_SERVER["REQUEST_METHOD"] == "POST"){
$date = $_POST["date"];
$pay_method = $_POST["payment"];
$total_cost = $_POST["total_cost"];
$user_id= $_POST["am"];
$checkbox1 = $_POST['check'] ;
for ($i=0; $i<sizeof ($checkbox1);$i++) {
$sql = "INSERT INTO project.orders (date, pay_method, total_cost, user_ID, product_ID) VALUES ('$date', '$pay_method', '$total_cost', '$user_id', '".$checkboxl[$i]. "')";
mysqli_query($conn,$sql) or die(mysqli_error());
}
}
// Close connection
mysqli_close($conn);
?>
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<div class="form-group ">
<input type="radio" name="payment" value="CREDIT CARD" checked>
<input type="radio" name="payment" value="INSURANCE" >
</div>
<div class="form-group ">
<input type="checkbox" class="largerCheckbox" id="ch1" name="check[]" value="10"><br>
<input type="checkbox" class="largerCheckbox" id="ch2" name="check[]" value="5"><br>
<input type="checkbox" class="largerCheckbox" id="ch3" name="check[]" value="15"><br>
</div>
<div class="form-group ">
<input type="text" name="date" id="date" class="form-control" readonly value="<?php echo date("Y-m-d");?>">
<input type="text" name="total_cost" id="total_cost" class="form-control" readonly value=" from JavaScript">
<input type="hidden" name="am" class="form-control" value="<?php echo ($_SESSION["id"]) ?>">
</div>
<div class="form-group">
<input type="submit" id="submit" name="submit" class="btn btn-primary" value="Υποβολή">
</div>
</form>
</body>
</html> ```
Aucun commentaire:
Enregistrer un commentaire