Wholesale Drop Ship Suppliers - Credit Card - Insurance - Free Advertising - Loans

PDA

View Full Version : database help (mySQL, dropdown menu)


Tehy
Oct 13th 2007, 12:59 am
I have mySQL database called phones and I have created table


+-----------+-------+-------+
| Phone |design | weight|
+-----------+-------+-------+
| S500i |slider | 90 g |
| P990i |bar | 120 g |
| M600i |bar | 100 g |
+-----------+-------+-------+


then I have a dropdown menu:

<FORM NAME="myform1">
<SELECT NAME="mylist1">
<OPTION VALUE="s500i">Sony Ericsson S500i
<OPTION VALUE="p990i">Sony Ericsson P990i
<OPTION VALUE="m600i">Sony Ericsson M600i
</SELECT>
</FORM>

So, what I want to do is that when user for instanse selects "Sony Ericsson S500i" from the dropdown menu I want get the data from my mySQL database and show it.

I'm new with php and mySQL but if I can have some help, I'm sure I can do this :)

What is the best way to do this?

Lordy
Oct 14th 2007, 4:38 am
i'm trying to learn php and mysql, so, this may not be the best answer. (would also like critique by someone if this does not work at all).

i think first you need to grab the value for the mysql db
$mylist1=$_POST['mylist1'];
after you get the submitted answer (i assume there is a submit button? or are you trying to get this with AJAX? if so, i didnt read anything about php+ajax yet, so i can't help you there)
select the key from the database - also you dont have your table name specified
$info = mysql_query("SELECT * FROM Phone table_name WHERE Phone='$mylist1'");
if (mysql_num_rows($info) > 0) {
echo "<table cellpadding=10 border=1>";
while ($row = mysql_fetch_assoc($info)) {
echo "<tr>";
echo "<td>".$info['Phone']."</td>;
echo "<td>".$info['design']."</td>;
echo "<td>".$info['weight']."</td>;
echo "</tr>";
}

unsure how that will work :\

ste.richards
Oct 14th 2007, 1:46 pm
You have several options. Firstly, and my favourite :), would be to use AJAX and have the data displayed from the database without a page reload.

The easiest way would be to have a PHP if condition.

EG:

if($_POST) {

$query = mysql_query("SELECT * FROM phones WHERE Phone = '{$_POST['mylist1']}'");

while($r = mysql_fetch_array($query)) {
echo $r['Phone']." ".$r['design']." ".$r['weight']."<br>";
}

}

Regards,

Tehy
Oct 14th 2007, 10:36 pm
Thanks! These helped me to get started! I'll let you know if I need more help :)