0

这是我拥有的代码

<?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循环中是否有问题?我通常不循环forforeach但这似乎是我想做的最好的方法,但我以某种方式失败了。

在不添加附加的情况下,[]我得到 12 个错误 Warning: Illegal string offset 'amount' Warning: Illegal string offset 'email'

4

1 回答 1

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
于 2013-06-26T22:16:07.580 回答