这是我的代码
<?php
$checkbox = $_REQUEST['checkbox'];
for($i=0; $i<count($_REQUEST['checkbox']); $i++){
$del_id = $checkbox[$i];
$get_info=$db->prepare("SELECT * FROM `pending_payments` WHERE `id` = ?");
$get_info->bind_param('i', $del_id);
$get_info->execute();
$result = $get_info->get_result();
$info[] = $result->fetch_assoc();
foreach($info as $row){
$amount = $row['amount'];
$email = $row['email'];
echo"
<input type='text' style='height: 35px;' name='Amount[]' value='".$amount."' />
<input type='text' style='height: 35px;' name='EmailAddress[]' value='".$email."' /><br />
";
}
}
?>
我遇到的问题是它复制了它找到的第一行。我有2个条目进入数据库,并且在进行第三行之前重复第一行,当我print_r
$info
它给了我这个
Array ( [0] => Array ( [id] => 1 [username] => ccarson030308 [amount] => 5.00 [processor] => PayPal [email] => ccarson030308@gmail.com [submitted_date] => 1372030166 ) )
然后显示该行的echo
然后重新开始并显示
Array ( [0] => Array ( [id] => 1 [username] => ccarson030308 [amount] => 5.00 [processor] => PayPal [email] => ccarson030308@gmail.com [submitted_date] => 1372030166 ) [1] => Array ( [id] => 2 [username] => tatsu91 [amount] => 5.00 [processor] => PayPal [email] => sheynever@yahoo.com [submitted_date] => 1372030166 ) )
这应该是它开始时唯一显示的内容,我的for
循环中是否有错误?我通常不会循环for
和foreach
,但它似乎是我尝试做的最好的方法,但我失败了。
如果不添加追加[]
,我会收到12个错误
Warning: Illegal string offset 'amount'
Warning: Illegal string offset 'email'
答案 0 :(得分:1)
对于每个查询,您的代码会附加到$info
。因此,在执行foreach($info ...)
时,您将看到此迭代中生成的结果的输出以及所有先前结果。
输出因此如下所示:
-------------- $i = 0
Output from #1
-------------- $i = 1
Output from #1
Output from #2
-------------- $i = 2
Output from #1
Output from #2
Output from #3
-------------- etc
你可能打算写
$info = $result->fetch_assoc(); // direct assignment, no append