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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.