This is the code that I have
<?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 />
";
}
}
?>
The problem that I am having is that it is duplicating the first row it finds. I have 2 entries into the database, and it is repeating the first row twice before going on the the third, when I print_r
$info
it gives me this
Array ( [0] => Array ( [id] => 1 [username] => ccarson030308 [amount] => 5.00 [processor] => PayPal [email] => ccarson030308@gmail.com [submitted_date] => 1372030166 ) )
then shows the echo
for that line then starts over and shows
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 ) )
which should be the only thing it shows to begin with, do I have something wrong in my for
loop? I don't normally loop for
and foreach
but it seemed to be the best method for what I am trying to do, but I am failing somehow.
Without adding the append []
I get 12 errors of Warning: Illegal string offset 'amount' Warning: Illegal string offset 'email'
Your code appends to $info
for every query made. So when doing foreach($info ...)
you will see output for the results produced in this iteration and for all the previous results.
The output will therefore look like this:
-------------- $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
You probably meant to write
$info = $result->fetch_assoc(); // direct assignment, no append