Forum Moderators: coopster
I'm writing a form where a student checks the modules he's studying, and all items for sale associated with those modules comes up from the database. I have been on this for 3days and getting no where.
<form method = "post" action = "getItems.php">
<u>Modules:</u><br/>
<input name = "module[]" type = "checkbox" value = "BF1101" />BF1101 - Accounting <br/>
<input name = "module[]" type = "checkbox" value = "BF2202" />BF2202 - Marketing <br/>
<input name = "module[]" type = "checkbox" value = "BF3303" />BF3303 - Economics <br/>
<p><input name = "send" type = "submit" value = "Get Items"></p>
</form>
HANDLING FORM
$itemArray = $_POST['module'];
$query = mysql_query("SELECT * FROM item WHERE moduleCode = '$itemArray'");
$result = mysql_query($query);
while ($row = mysql_fetch_array($result))
{
?>
<tr>
<td width = "10%" height = "25">
<?php echo $row["barCode"]; ?>
</td>
<td width = "50%" height = "25">
<?php echo $row["description"]; ?>
</td>
<td width = "10%" height = "25">
<?php echo $row["price"]; ?>
</td>
<td width = "10%" height = "25">
<?php echo $row["stockLeft"]; ?>
</td>
<td width = "10%" height = "25">
<?php echo $row["moduleCode"]; ?>
</td>
</tr>
</table>
You are submitting the query twice, except the second time is incorrect. It should be as follows:
$query = mysql_query("SELECT * FROM item WHERE moduleCode = '".[url=http://www.php.net/mysql-real-escape-string]mysql_real_escape_string[/url]($itemArray)."'");
while ($row = mysql_fetch_array($query))
Notice how I also added mysql_real_escape_string() to help prevent from SQL injection which can be very hazardous to your database and website as a whole.
$query = "SELECT * FROM item WHERE moduleCode = '";
$qstr = ([url=http://www.php.net/is-array]is_array[/url]($itemArray))? [url=http://www.php.net/implode]implode[/url]([url=http://www.php.net/array-map]array_map[/url]("mysql_real_escape_string",$itemArray), "' OR moduleCode = '"): mysql_real_escape_string($itemArray);
$query .= $qstr . "'";
#
$result = mysql_query($query);
while ($row = mysql_fetch_array($result))
[b]echo '<table border="1">';[/b]
while ($row = mysql_fetch_array($result))
{
?>
<tr>
<td width = "10%" height = "25">
<?php echo $row["barCode"]; ?>
</td>
<td width = "50%" height = "25">
<?php echo $row["description"]; ?>
</td>
<td width = "10%" height = "25">
<?php echo $row["price"]; ?>
</td>
<td width = "10%" height = "25">
<?php echo $row["stockLeft"]; ?>
</td>
<td width = "10%" height = "25">
<?php echo $row["moduleCode"]; ?>
</td>
</tr>
[b]<?php } #this closes the while loop ?>[/b]
</table>
>> Are you a PHP/SQL tutor? Or you learnt on your own?
Please keep personal questions in sticky mail as it does not belong in the public forums, but to answer your question quickly, I am self-taught and have been doing it for some years now.