Forum Moderators: coopster

Message Too Old, No Replies

Php Mysql Select Where Help!

php,mysql,select,where

         

phil2564

12:59 am on May 18, 2004 (gmt 0)

10+ Year Member



Once again I am back to this board for help I have my database with 2 tables. Database name is models the 2 tables are male and female. I would like to have a few drop down menus for people to choose from. For the sake of this question I am elimanating alot of columns. Lets say they have a choice of male or female on one drop down menu and the other is age. I have been able to get the PHP script to work as long as the values in drop down are single values like age 10 but if its for a spread of ages 10,11,12 ,13 It will not work let me show you what I am working with. BELOW is my HTML FORM and my PHP script please see why they are not working. Another thing say someone does not want to use the age drop down how can it be set to display all ages. Just in case theywant to see all ages. ANY HELP IS APPRECIATED. I am trying real hard to learn and understand this. Thanks for eveyones patience.
Phil

PHP SCRIPT

<?php
$conn = mysql_connect("localhost","","");
mysql_select_db("models",$conn);
$person = $_POST['sex'];
$myage = $_POST['dob'];
$resultID = mysql_query("SELECT * FROM $person WHERE age = $myage ",$conn);
print "<table border = 1><tr><th>id</th>";
print "<th>name_first</th><th>name_last</th>";
print "<th>age</th><th>height</th><th>weight</th>";
while ($row = mysql_fetch_row($resultID))
{
print "<tr>";
foreach ($row as $field)
{
print "<td>$field</td>";
}
print "</tr>";
}
print "</table>";
mysql_close($conn);
?>

_____________________________________________________________
HERE IS MY HTML FORM
_____________________________________________________________

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form name="form1" method="post" action="firstconnect.php">
<p>
<select name="sex" id="sex">
<option>--</option>
<option value="male" selected>male</option>
<option value="female">female</option>
</select>
</p>
<p>
<select name="dob" id="dob">
<option>--</option>
<option value="48">48</option>
<option value="13">13</option>
<option value="10">10</option>
<option value="10,11,12,13">10 - 13</option>
</select>
</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>
<input type="submit" name="Submit" value="Submit">
</p>
</form>

</body>
</html>

thedagda

1:07 pm on May 18, 2004 (gmt 0)

10+ Year Member



There are a few ways I can see you fixing this problem. If you want to keep the options in the form grouped like 10-13, here is what I suggest.

Change the option value from:
<option value="10,11,12,13">10 - 13</option>
to:
<option value="10-13">10 - 13</option>

In your php file delete the " $resultID = mysql_query("SELECT * FROM $person WHERE age = $myage ",$conn); " statement and add the stuff below.

if($myage == '10-13'){
$resultID = mysql_query("SELECT * FROM $person WHERE age>=10 and age<=13",$conn);
}
else {
$resultID = mysql_query("SELECT * FROM $person WHERE age = $myage ",$conn);
}

That should give you the range of ages that you need. This would need to be changed if you wanted to add more ranges like 14-16, 17-19, etc... If you need help with that, let me know.

HTH,

Conor