Forum Moderators: coopster

Message Too Old, No Replies

Checkbox query question

         

ScriptJnr

5:44 pm on Mar 21, 2008 (gmt 0)

10+ Year Member



Please help me if you can.

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>

surrealillusions

6:15 pm on Mar 21, 2008 (gmt 0)

10+ Year Member



Are you actually connecting to the database..as i cant see that in your 'handling form'..?

eelixduppy

6:18 pm on Mar 21, 2008 (gmt 0)



Welcome to WebmasterWorld! :)

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.

ScriptJnr

6:24 pm on Mar 21, 2008 (gmt 0)

10+ Year Member



Yes, I am connecting to the database, I just didnt include that part there.

Eelixduppy, I'll try that now.

Thanks.

ScriptJnr

6:32 pm on Mar 21, 2008 (gmt 0)

10+ Year Member



No luck.

eelixduppy

6:40 pm on Mar 21, 2008 (gmt 0)



The code is correct if $_POST['moduleCode'] wasn't containing an array, which I have seemed to miss the first time i read through it. How are you storing the values in the database in the moduleCode field? If multiple checkboxes are selected, should the different values be separated by commas, for example?

ScriptJnr

6:44 pm on Mar 21, 2008 (gmt 0)

10+ Year Member



If multiple checkboxes are selected, it should just display all the info related to that moduleCode in a table.

ScriptJnr

6:47 pm on Mar 21, 2008 (gmt 0)

10+ Year Member



I just understood you,

moduleCode is stored as 'BF1101' , 'BF2202', 'BF3303' etc

so if you check 'BF1101', items relating to that should show

eelixduppy

6:56 pm on Mar 21, 2008 (gmt 0)



You are going to have to generate your query correctly. Try the following code, which is untested:

$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))

ScriptJnr

7:03 pm on Mar 21, 2008 (gmt 0)

10+ Year Member



I'll try that thanks.

Am I making a mistake like maybe the moduleCode value passed from the checkboxes dont match moduleCode entries in the table?

eelixduppy

7:06 pm on Mar 21, 2008 (gmt 0)



If that is the case then you should definitely check that out. A manual check is usually a good way to find out; run the query through the command line manually with values you insert yourself instead of taking them from form input. If you get results from that manual query, then your error lies somewhere in your php code, although I believe what I gave you above should be it and should work correctly for you. :)

ScriptJnr

7:14 pm on Mar 21, 2008 (gmt 0)

10+ Year Member



You my friend, are a genius. It works.

Only its not bringing it in tabular form. Just in a line.

eelixduppy

7:19 pm on Mar 21, 2008 (gmt 0)



Make sure you format your HTML correctly. It should be like this:

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

ScriptJnr

7:21 pm on Mar 21, 2008 (gmt 0)

10+ Year Member



Thanks again.

Are you a PHP/SQL tutor? Or you learnt on your own?

eelixduppy

7:24 pm on Mar 21, 2008 (gmt 0)



You're welcome :)

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

ScriptJnr

7:30 pm on Mar 21, 2008 (gmt 0)

10+ Year Member



Thanks anyway.