I am having issues getting the record to insert multiple data. It only wants to insert the last item. I have in the while loop quantity
, item_name
, amount
, and item number
. So if there is 3 items I need all 3 to be inserted along with the user information lanId
, employee_name
, department
, cost_center
.
action.php
<form action="test.php" method="post">';
$uid = $_SESSION["uid"];
$sql = "SELECT * FROM cart WHERE user_id = '$uid'";
$run_query = mysqli_query($con,$sql);
while($row=mysqli_fetch_array($run_query)){
$x++;
echo
'<br>'.'<input type="text" name="item_name" value="'.$row["product_title"].'">
<input type="text" name="quantity" value="'.$row["qty"].'">
<input type="text" name="amount" value="'.$row["price"].'">
<input type="text" name="item_number" value="'.$x.'">';
}
echo"<br>
<label>Lan ID</label>
<input type='text' name='lanId' id='lanId' autocomplete='off' class='form-control' >
<label>Employee Name</label>
<input type='text' name='employee_name' id='name' autocomplete='off' class='form-control'>
<label>Department</label>
<select name='department' id='department' class='form-control'>
<option value =''>Select Department...</option>
<option value ='OTHER'> OTHER</option>
</select>
<label>Cost Center</label>
<input type='text' class='form-control' name='cost_center' value=''>
<label>Total amount</label>
<input type='text' class='form-control' name='total_amt' value='$total_amt'>
<br><br><br>
<input type='submit' class='btn btn-primary' value='Submit'>
</form>";
test.php
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
include('db.php');
$item_name = $_POST["item_name"];
$amount = $_POST["amount"];
$quantity = $_POST['quantity'];
$lanId = $_POST["lanId"];
$employee_name = $_POST["employee_name"];
$department = $_POST['department'];
$cost_center = $_POST['cost_center'];
$total_amt = $_POST['total_amt'];
$sql = "INSERT INTO `order`
( `item_name`, `amount`, `quantity`,
`lanId`, `employee_name` , `department`, `cost_center`, `total_amt`)
VALUES ('$item_name', '$amount', '$quantity',
'$lanId', '$employee_name', '$department', '$cost_center','$total_amt')";
$run_query = mysqli_query($con,$sql);
if($run_query){
echo "
<div class='alert alert-success'>
<a href='http://a0319p528/project2/profile.php' class='close' data-dismiss='alert' aria-label='close'>×</a>
<b>data inserted successfully..!</b>
</div>
";
}
?>