Forum Moderators: coopster
On page 1 I create a list from a database of checkboxes, quantities, and items, and I want to pass several of the variables onto page 2 depending on whether or not the checkbox has been ticked. It looks like it should be so simple (and I'm sure it is!). If anyone could suggest anything I would appreciate it.
In the following code on page 2 it is the $value and $qty that don't seem to contain anything.
Many thanks
PAGE 1
======
<?php
session_start();
$connection = mysql_connect($host, $user,$password)
or die ("Couldn't connect to server.");
$db = mysql_select_db($database, $connection)
or die ("Couldn't select database.");
$result=mysql_query("SELECT item, itemid, qty FROM tbl where username = '$logname'");
?>
<table width='95%' border='0' cellspacing='0' cellpadding='0'>
<form action="addselected.php" method="post">
<?php
while ($row=mysql_fetch_array($result))
extract($row);
}
?>
<tr>
<td><INPUT TYPE='checkbox' NAME='itemid[]'</td>
<td><b><?php echo $qty?></b></td>
<td><?php echo $item?></td>
<td><input type=hidden name='qty[]' value='<?php echo $qty?>'></td>
</tr>
<?php
}
?>
<tr><td>
<input type='submit' value='Add selected'>
</form>
</td></tr>
</table>
PAGE 2
======
<?php
session_start();
$connection = mysql_connect($host, $user,$password)
or die ("Couldn't connect to server.");
$db = mysql_select_db($database, $connection)
or die ("Couldn't select database.");
foreach ($HTTP_POST_VARS as $key => $value)
{
echo "$key, <b>$value</b>, $qty<br>";
}
?>
Does it work on the first page before it passes to page 2?
I was looking at the output loop and there may be some mismatched braces.
<?
while ($row=mysql_fetch_array($result))
extract($row);
}
?>
html output stuff
<?
} // what's this brace for? seems to be orphaned
?>
you may also want to look at this thread for similarities. It is about deleting not adding but the premise is very similar.
Deleting Multiple Items Using Checkboxes! [webmasterworld.com]
<?php
while ($row=mysql_fetch_array($result))
{ //moved to here from...
extract($row);
//...here
//add echo statements
echo "<tr>";
echo "<td><INPUT TYPE='checkbox' NAME='itemid[]'</td>";
echo "<td><b>$qty</b></td>";
echo "<td>$item</td>";
echo "<td><input type=hidden name='qty[]' value=\"$qty\"></td>";
echo "</tr>";
}
?>
<edit>long day, fixed obvious errors above :-)<edit>
As I couldn't crack the above I moved onto this tactic.. Forgeting the qty value altogether and just looping through the checkboxes to run a select query on each pass to get the qty value again but I keep getting 'Parse error: parse error, unexpected $ in /home/wwwnutr/public_html/menu1addselected.php on line 22'
Any suggestions gratefully recieved!
Here is the simple code from page 2 where I try to run the query..
<?php
session_start();
$connection = mysql_connect($host, $user,$password)
or die ("Couldn't connect to server.");
$db = mysql_select_db($database, $connection)
or die ("Couldn't select database.");
foreach ($itemid as $key => $value)
{
$result=mysql_query("select qty from menus where itemid='$value' and menunumb='1');
}
?>
foreach ($itemid as $item)
{
mysql_query("Select qty FROM menus WHERE itemid = '$item' and menunumb='1'");
}
an alternate method could be similar to the one in the other thread. Build your query using IN ('val1','val2','val3') and then execute it after.
$which = 0;
foreach ($itemid as $itemida) {
if ($which!=0) $itemlist .= ",";
$itemlist .= "'" . $itemida . "'";
$which++;
}
$selq = "Select qty FROM menus WHERE itemid in (" . $itemlist . ")" and menunumb='1';
$result=mysql_query($selq);
while ($row = mysql_fetch_row($result))
{
$qty=$row[0];
}
I think that is right. Keeping db calls to a minimum is always a bonus. Queries can, usually, be made more complex and created dynamically to decrease the number of db calls and better exploit the power of mysql.