Jump to content

Problem with while loop


perky416

Recommended Posts

Hi Guys,

 

I am trying update some array values in my database. For some reason using the code below the $checked and $counter values are not working. I have added the echos for debugging purposes and nothing is being echoed.

 

	
$query = mysql_query("SELECT * FROM offers WHERE seller='$username' AND buyer='$buyer'");
while ($row = mysql_fetch_assoc($query))
{ 
$checked = $_POST['checkbox'][$y];
$counter = $_POST['counter'][$y];
echo $checked;
echo $counter;
$date = date("Y-m-d");
$time = date("H:i:s");
mysql_query("UPDATE offers SET offer='$counter',seller_status='Counter Offer Made',buyer_status='Counter Offer Received',date='$date',time='$time',seller_action='0',buyer_action='1' WHERE domain='$checked'");
}

 

When i do the following it echos the $checked and $counter values perfectly so i know that the form is posting ok.

 

foreach ($_POST['checkbox'] as $checked)
{
echo $checked;	
}

foreach ($_POST['counter'] as $counter)
{
echo $counter;
}

 

Can anybody figure out what is wrong with my while loop? Iv been at it for ages and cant seem to crack it.

Thanks

Link to comment
https://forums.phpfreaks.com/topic/233212-problem-with-while-loop/
Share on other sites

Hi

the [$y] is for the array for the form field name <input type='hidden' name='checkbox[$y]' />.

 

Iv just managed to figure out what i was doing wrong. I missed out the $y = 0; before the loop and $y++; in the loop. Using the following makes it work correctly:

 

$query = mysql_query("SELECT * FROM offers WHERE seller='$username' AND buyer='$buyer'");
$y=0;
while ($row = mysql_fetch_assoc($query))
{ 
$checked = $_POST['checkbox'][$y];
$counter = $_POST['counter'][$y];
echo $checked;
echo $counter;
$date = date("Y-m-d");
$time = date("H:i:s");
mysql_query("UPDATE offers SET offer='$counter',seller_status='Counter Offer Made',buyer_status='Counter Offer Received',date='$date',time='$time',seller_action='0',buyer_action='1' WHERE domain='$checked'");
$y++;
}

 

Done it again, been trying to figure it out for hours and as soon as I post on here i manage to figure it out lol. Thats about the 3rd time iv done it.

 

Thanks anyway.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.