在我的数据库中,我有3行。产品,数量和费率。
$rate = implode(",", $_POST["myrate"]);
这是费率。这意味着一行可以是" rate1,rate2,rate3"等等。但它与产品和数量相同。
"product1, product2, product3"
"quantity1, quantity2, quantity3"
"rate1, rate2, rate3"
如果我回应它,它将如上所述。但我想要的是:
"product1, quantity1, rate1"
"product2, quantity2, rate2"
"product3, quantity3, rate3"
这怎么可能?
https://www.sanwebe.com/2013/03/addremove-input-fields-dynamically-with-jquery 这就是我正在使用的代码。刚添加了2个字段和2行。
我的PHP代码:
<?php
//Open a new connection to the MySQL server
$mysqli = new mysqli('localhost','root','','invoice-generator');
//Output any connection error
if ($mysqli->connect_error) {
die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}
$capture_field_vals = "";
if(isset($_POST["mytext"]) && is_array($_POST["mytext"])){
$capture_field_vals = implode(",", $_POST["mytext"]);
$quantity = implode(",", $_POST["myquantity"]);
$rate = implode(",", $_POST["myrate"]);
}else{
echo "error";
}
//MySqli Insert Query
$insert_row = $mysqli->query("INSERT INTO invoice_rows (product, quantity, rate) VALUES ('$capture_field_vals', '$quantity', '$rate')");
if($insert_row){
echo 'Success!';
}else{
echo "error2";
}
?>
答案 0 :(得分:1)
$quantity = array("q1","q2","q3","q4");
$product = array("p1","p2","p3","p4");
$rate = array("r1","r2","r3","r4");
for ($i = 0; $i < count($quantity); $i++) {
$new_data[] = array($quantity[$i],$product[$i],$rate[$i]);
}
在循环中获取所有这些并将值转储到一个数组中。
答案 1 :(得分:0)
如果所有变量的数量都相同,那么
if (count($_POST["mytext"]) == count($_POST["myquantity"]) && count($_POST["myquantity"]) == count($_POST["myrate"])) {
for ($i = 0; $i < count($_POST["mytext"]); $i++) {
echo $_POST["mytext"][$i] . "," . $_POST["myquantity"][$i] . "," . $_POST["myrate"][$i] ."<br/>";
}
}
答案 2 :(得分:0)
<table>
<thead>
<tr>
<th>Product</th>
<th>Quantity</th>
<th>Rate</th>
</tr>
</thead>
<tbody>
//Lets say you have the data from your db in $results
<?php foreach($results as $result): ?>
<tr> //loop one whole row
<td><?=$result['product']?></td>
<td><?=$result['quantity']?></td>
<td><?=$result['rate']?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
This way you can show product,quantity and row for each id in your database table in one row...if that is what you want