By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,890 Members | 1,039 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,890 IT Pros & Developers. It's quick & easy.

Having trouble or possibly i am just TOO stupid to understand

keyvanrahmadi
P: 57
I have created a form which is as Follow:

[HTML]

<form action="fleet-of-car.php" method="post">
<br />SELECT A FORM TO SEE :
<select name="FORM">
<option value="ALL CARS"/>All Cars
<Option Value="CARS BY MANUFACTURER"/>CARS BY MANUFACTURER
<Option Value="CARS BY FUEL TYPE"/>CARS BY FUEL TYPE
</select>
<br />
<input type="submit" value="Show INFO" />
</form>
</select>

[/HTML]

Now what i am trying to achieve is as follow:

1: By submitting the first option in this case All cars, the form should connect to DB and run the query, which if i am not wrong is as follow:

[PHP]

<?php

//make a connection

mysql_connect("server", "user", "password");
mysql_select_db ('db', $connection);

//Run the Sql
$results = mysql_query("SELECT * FROM car
");

?>

[/PHP]

The question i have is, what happens if the user selcets option 2 or even 3? The query will be different since it will read from different Mysql table to get the result, but what is the script required for this type query.

I though by using IF(isset), i might get away with it but i am not too familiar with it and i have no idea even where to start, WHILE statement should do it but i am just getting more confused.

Any help would be great guys, and thx well in advance

keyvan
Feb 16 '07 #1
Share this Question
Share on Google+
1 Reply


Atli
Expert 5K+
P: 5,058
Hi.

If Im understanding you correctly, you would need to do something like this.

[PHP]
<?php

$option = $_POST['Name'];

// Find the selected option
if($option == "whatever")
{
$query = "SELECT * FROM table1";
}
else if($option == "somethingelse")
{
$query = "SELECT * FROM table2";
}
// ....
else
{
$query = "";
}

// Run the query
if($query != "")
{
$DB = mysql_connect("h", "u", "p") or die("omg it didnt work!");
mysql_select_db("d", $DB) or die("aaah! so close!");

$result = mysql_query($query) or die("<p>MySQL Error: <br />". mysql_error() ."</p>");

// Do something with results
}
else
{
// Error msg if the option is invalid
echo "Invalid option";
}

?>
[/php]

Hope this helps.
Feb 16 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.