$stringitem2= soap|2|15|30&soap|1|5|10&soap|20|150|300
$array = explode('&', $stringitem2);
// Now explode each on ,
foreach ($array as &$substring) {
$substring = explode('|', $substring);
echo $substring[0];
echo "<br/>";
echo $substring[1];
echo "<br/>";
echo $substring[2];
echo "<br/>";
echo $substring[3];
}
在这里,我正在使用foreach循环
爆炸一个在first和substring explodes中定义的字符串我的输出是
soap
2
15
30
soap
1
5
10
soap
20
150
300
我正在使用以下代码将数据插入数据库...
$bill_no = $stringnew[2];
$bill_amount = $stringtotal[1];
$item_name = $substring[0];
$quantity = $substring[1];
$rate = $substring[2];
$amount = $substring[3];
$discount = $stringtotal[0];
try {
$dbh = new PDO('mysql:host=' . DB_SERVER . ';dbname=' . DB_NAME . '', DB_USER, DB_PASS);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
} catch (PDOException $e) {
echo $e->getMessage();
die();
}
try {
$query = $dbh->prepare("INSERT INTO billing (bill_no, bill_amount, item_name, quantity, rate, amount, discount) VALUES (:bill_no, :bill_amount, :item_name, :quantity, :rate, :amount, :discount);");
$query->setFetchMode(PDO::FETCH_ASSOC);
$query->execute(array(
"bill_no" => $bill_no,
"bill_amount" => $bill_amount,
"item_name" => $item_name,
"quantity" => $quantity,
"rate" => $rate,
"amount" => $amount,
"discount" => $discount
));
} catch (PDOException $ex) {
echo $ex->getMessage();
}
当我发送输入时,它的最终值仅插入到表格中......
soap
20
150
300
我想插入item_name, quantity, rate, amount,
这四个值正在循环...
soap ---insert into item_name
2 ---insert into quantity
15 ---insert into rate
30 ---insert into amount
soap ---insert into item_name
1 ---insert into quantity
5 ---insert into rate
10 ---insert into amount
soap ---insert into item_name
20 ---insert into quantity
150 ---insert into rate
300 ---insert into amount
.............................
.............................
.............................
.............................
循环可能会变长很长一段时间...我希望将上述值作为循环插入到相关字段中,如上所述......通过使用上面的mysql插入查询,它只取最后一个循环值...如何插入所有值。 ...
注意:除了这四个item_name, quantity, rate, amount,
剩下的东西不在循环中,它们自己得到一个值...
答案 0 :(得分:1)
我已经简化了数组中的代码,您可以使用简单的foreach
$stringitem2= 'soap|2|15|30&soap|1|5|10&soap|20|150|300';
$string_arr = array('item_name', 'quantity', 'rate', 'amount');
$result = array();
foreach(explode('&',$stringitem2) as $key => $value){
$result[$key] = array_combine($string_arr,explode('|',$value));
}
print_r($result);//Array ( [0] => Array ( [item_name] => soap [quantity] => 2 [rate] => 15 [amount] => 30 ) [1] => Array ( [item_name] => soap [quantity] => 1 [rate] => 5 [amount] => 10 ) [2] => Array ( [item_name] => soap [quantity] => 20 [rate] => 150 [amount] => 300 ) )
答案 1 :(得分:1)
试试这个
//Place inside loop
$values[] = "($bill_no, $bill_amount, $item_name, $quantity, $rate, $amount, $discount)";
// place this code after loop
try{
$params = implode(",", array_fill(0, count($values), "?"));
$query = $dbh->prepare("INSERT INTO billing (bill_no, bill_amount, item_name, quantity, rate, amount, discount) VALUES (".$params.")");
$query->setFetchMode(PDO::FETCH_ASSOC);
$query->execute($values);
}
} catch (PDOException $ex) {
echo $ex->getMessage();
}