Forum Moderators: coopster

Message Too Old, No Replies

Checkbox Option - No Output

php/mysql - can't get query to/from dbase.. HELP!

         

omfLondon

8:25 pm on Mar 31, 2007 (gmt 0)

10+ Year Member



Hi, back again - I think I've progressed a little bit; but I'm still not getting anything back from the database musicdb1. In a table called TYPE, there is a row also called type which gives the diff types of music:

I've done the checkbox form - where they can choose music type:

<form action="page4.php" method="post">
<input type="checkbox" name="type[]" value="blues">
<input type="checkbox" name="type[]" value="r&b">
<input type="checkbox" name="type[]" value="pop">
<input type="checkbox" name="type[]" value="rock">
<input type="submit" name="submit" value="Compare">
</form>

I'VE DONE THE HTML pointing to the music type (I think..) but can't get any output - any ideas? I suspect its the coding but i've got lost somewhere...!

<?php
mysql_connect("localhost","","") or die("Unable to connect to SQL server");
@mysql_select_db("musicdb1") or die("Unable to select database");

$type = $_POST['type'];

if (!empty($type))
{
for ($i=0; $i<count($search); $i++)
{
$sql = "select * from type WHERE type =" . $_POST['type'] . " and type='" . $_POST['type'] . "'";
echo $sql;
$query = mysql_query($sql);
while ($row = mysql_fetch_array($query)) {
echo "<p>",$row['id'],": ",$row['type'];
}
}
?>

scriptmasterdel

12:07 am on Apr 1, 2007 (gmt 0)

10+ Year Member



<?
$type = $_POST['type'];
if (!empty($type))
{
for ($i = 0; $i < count($type); $i++)
{
$sql = "SELECT * FROM `type` WHERE `type` = '".$type[$i]."'";
echo $sql;
$query = mysql_query($sql) or die("Oops an error: ".mysql_error());
while ($row = mysql_fetch_array($query))
{
echo "<p>",$row['id'],": ",$row['type'];
}
}
?>

In my humble opinion, this is not the best way of doing this but it should work ok.

Please note: untested code ;-)

If you have any more problems please let me know ...

Del

whoisgregg

1:00 pm on Apr 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Using the error suppression directive ("@") isn't a good idea when you are trying to troubleshoot a problem with your code. Remove it, add some error reporting, and let us know what errors you receive:

error_reporting(E_ALL); // add this as the first line of your script

In addition to scriptmasterdel's suggestions, also please make use of the mysql_real_escape_string [php.net] function to prevent SQL injection attacks [php.net].