gpt4 book ai didi

php - 字符串爆炸,然后使用 foreach 循环爆炸子字符串,然后使用 php 插入到数据库

转载 作者:行者123 更新时间:2023-11-29 06:27:06 25 4
gpt4 key购买 nike

 $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];
}

在这里我分解了一个在 first 中定义的字符串,子字符串使用 foreach 循环分解

我的输出是

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, 其余的东西不在循环中,它们自己得到一个值...

最佳答案

我已经在一个数组中简化了您的代码,您可以在数组中使用简单的 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 ) )

关于php - 字符串爆炸,然后使用 foreach 循环爆炸子字符串,然后使用 php 插入到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30319258/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com