...

View Full Version : Help! Mysql results based on combo box.



amg182
05-30-2011, 02:15 PM
Hi Guys. Hopefully someone can help me with this...New to coding and pretty lost on this.
I have a Mysql database which is displaying results to my webpage with no problems. However I would like to be able to add a combo box to my webpage that would update the mysql database results based on the combo box selection. For example if Ford is chosen from the combo box, the webpage would refresh and show all the results for Ford in the webpage. Can someone please help me?

Here is the code I have at the moment that works just fine. But results of the database are based on the WHERE statement.


<?php
$con = mysql_connect("server","database","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("a5525005_cars", $con);

$result = mysql_query("SELECT * FROM `cars` WHERE Makel='Ford'");

echo "<table class='ex1' border='0' width='113%' style=text-align:center; cellpadding='6' cellspacing='0'>

</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr style=font-family:verdana;font-size:80%;>";

echo "<td width=13%>" . $row[""] . "<img src=\"" . $row["Photo"] . "\"></a>";
echo '<td width="14%"><a class="mylink" href="' . $row['URL'] . '">' . $row['Model'] . '</a></td>';
echo '<td width="5%"><a class="mylink" href="' . $row['URL'] . '">' . $row['Year'] . '</a></td>';
echo '<td width="4%"><a class="mylink" href="' . $row['URL'] . '">' . $row['Fuel'] . '</a></td>';

echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>


:confused:

Fou-Lu
05-30-2011, 04:57 PM
User input from a form can be made available via the _GET or _POST superglobal, depending on the form method (typically POST). Selection menus are named, and the selected item(s) are sent by the name, so you can retrieve it from $_POST['yourComboName'];. This will be an array if you allow multiple selections.
This data can then be used in the query's where. Personally I'd write a table of makes so that I can pass around a numerical ID, but if you stick with a string you must use mysql_real_escape_string on it to prevent a SQL injection.

You won't need to refresh the page beyond this. The where provided by the submit would be used to control the data that the page will display.

amg182
05-30-2011, 06:57 PM
Thanks for reply, I have tried playing around based on what you have said, but my lack of knowledge and experience is letting me down...:(

I have tried the following but no luck, is this on the right track?

Combobox name/id = "combobox"


...................

}

mysql_select_db("b5525035_cars", $con);

$result = mysql_query("SELECT * FROM `cars` WHERE Make=$_POST['combobox'];");

echo "<table>

........


Thanks again, I do appreciate it!

amg182
05-31-2011, 01:25 AM
Got it working thanks anyway!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum