在php中使用foreach循环插入多个字段

时间:2015-01-18 16:44:19

标签: php mysql

当我想在一个表中插入多个字段时,我遇到了问题。 它只向每个表插入0值。

这是我的表格:

<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>

这里是提交代码:`

    $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());
}

}

查询插入到数据库中,但是为每个表插入0值。

有人可以帮帮我吗?

2 个答案:

答案 0 :(得分:0)

您的错误是走foreach($_POST as....)。好吧,$ _POST将包含许多不同的变量,远远超过表中的行。这是不正确的事情。在您的示例中,正确的方法是走{4}},例如$_POST['unit']

答案 1 :(得分:0)

<?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);
?>