Page 1 of 1
Simple Problem...
Posted: Sat Aug 07, 2004 3:54 pm
by Joe
I have an order form which uses checkboxes to identify if the product was selected or not. However my code does not seem to work:
Code: Select all
$sql = "SELECT * FROM dbproducts";
$result = mysql_query($sql) or die(mysql_error());
while (true)
{
$row = mysql_fetch_assoc($result);
if ($row == false) break;
$val = $row['product'];
$val1 = $_POST[$val];
if ($val1 > '0')
{
print("<input type='hidden' name='$val' value='$val1'>");
}
}
The checkbox value is set to 0 on each product by default and the script I wrote tests whether the checkbox value is more than 0. Instead of displaying the product, it displays nothing therefore the value is not passing 0.
Any help appreciated

Posted: Sat Aug 07, 2004 5:02 pm
by scorphus
1 - What is the form like? (<form action...>...<input...>...</form>)
2 - What does print_r($_POST); return?
3 - Please give us an example of $row['product'] at one iteration.
Thanks,
Scorphus.
Re: Simple Problem...
Posted: Sat Aug 07, 2004 5:17 pm
by timvw
Joe wrote:Code: Select all
$sql = "SELECT * FROM dbproducts";
$result = mysql_query($sql) or die(mysql_error());
I presume you have a valid mysql connection. (Generally, acquired via mysql_connect and mysql_select_db)
Why do you repeat this loop forever?
I think you want while ($row = mysql_fetch_assoc($result))
Are you sure you don't want $val1 > 0 ?
Posted: Sat Aug 07, 2004 5:25 pm
by scorphus
It is not repeating forever, just look at this line:
Code: Select all
<?php
//...
if ($row == false) break;
//...
?>
Of course this isn't the best way since the compiler tests both the while and the if conditions instead of just the while one, and it is time consuming. Ok, you must show him this, but don't make he think he's doing all wrong.
-- Scorphus
Posted: Sat Aug 07, 2004 5:36 pm
by timvw
Imho, for the sake of clarity, it is wrong. And Tanenbaum agrees with me

Posted: Sat Aug 07, 2004 5:38 pm
by feyd
agreed, using an infinite loop set up is kinda dangerous.. and a bit wasteful of cpu in this case

, well in most cases

Posted: Sat Aug 07, 2004 5:44 pm
by scorphus
Fine, now he knows what is wrong, or _not_right_ following good-programming concepts.
-- Scorphus