简体   繁体   中英

insert multiple fields using foreach loop in php

I have a problem when I want to insert multiple fields into one table. it insert only 0 value to each table.

Here's my form:

<table class=table_position>
<?php
for($num_request=$_POST['number_item'];$num_request>0;$num_request--){

echo"<tr>
<th><input type='number' step='1' min='1' name='quantity[$num_request]' id='quantity$num_request' onkeyup='multiply$num_request();' style='text-align:center;width:96.5px;height:25;' required></th>
<th><input type='text' name='unit[$num_request]' align='middle' style='text-align:center;width:65px;height:25;' required></th>
<th><input type='text' name='description[$num_request]' align='middle' style='text-align:center;width:343px;height:25;' required></th>
<th><input type='text' name='stocknum[$num_request]' align='middle' style='text-align:center;width:99px;height:25;' required></th>
<th><input type='number' min='1' name='unitcost[$num_request]' id='unitcost$num_request' onkeyup='multiply$num_request(),add() ' align='middle' style='text-align:center;width:100px;height:25;' required></th>
<th><input type='text'  name='totalcost[$num_request]' id='product$num_request' class='add'  align='middle' style='text-align:center;width:164px;height:25;' readonly></th>
</tr>";
}
?>


</table>

And here's the submit code:`

    $query="SELECT * FROM request WHERE username='$usernames' ORDER BY id DESC LIMIT 1";
$result=mysqli_query($con,$query);
$row = mysqli_fetch_array($result);
$num_row=mysqli_num_rows($result);  
$request_id=$row['id'];

$i = 0;
foreach ($_POST as $val) {
$quantity=$_POST['quantity'][$i];
$unit=$_POST['unit'][$i];
$item_description=$_POST['description'][$i];
$unit_cost=$_POST['unitcost'][$i];
$total_cost=$_POST['totalcost'][$i];

include("dbc1.php");
$sql="INSERT INTO request_items (request_id,quantity,unit,item_description,unit_cost,total_cost) 
VALUES ('$request_id','$quantity','$unit','$item_description','$unit_cost','$total_cost')";
$i++;
if(!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}

}

The query inserts into the database, but inserts 0 values to each table.

Can someone please help me?

Your error is you walk foreach($_POST as....) . Well, $_POST will contain dozens of different variables, much more than rows in your table. This isn't a correct thing to walk through. In your example the correct way would be walking trough $_POST['unit'] for example, as in foreach($_POST['unit'] as ...)

<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "my_db");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}


/* Prepare an insert statement */
$query = "INSERT INTO request_items (request_id,quantity,unit,item_description,unit_cost,total_cost) VALUES (?,?,?,?,?,?)";
$stmt = mysqli_prepare($link, $query);

mysqli_stmt_bind_param($stmt, "iiisii", $request_id, $quantity, $unit,$item_description,$unit_cost,$total_cost);

$count = count($_POST['quantity']);
for($i=$count; $i > 0; $i--){

    $quantity=$_POST['quantity'][$i];
    $unit=$_POST['unit'][$i];
    $item_description=$_POST['description'][$i];
    $unit_cost=$_POST['unitcost'][$i];
    $total_cost=$_POST['totalcost'][$i];

    /* Execute the statement */
    mysqli_stmt_execute($stmt);
}


/* close statement */
mysqli_stmt_close($stmt);

/* close connection */
mysqli_close($link);
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM