hey frnds...

i am develpoing a program using PHP/MySQL. Here, i have a form which consists of a few text fields etc etc. It also contains a drop down menu.

In the backend, i have a number of databases. The user input (drop down menu) will decide which database is connected to.

Please help me with the code for the same. I want to strict to PHP/TML only, and not use javascript.

pls help.

thx..

Hi,

I just want to say that Iv only been doing php for a few days now but i think i can give you a good answer.

You want a drop down menu which decides which database the next page connects to only using html and php?

Form page:

<form id="connect" name="connect" method="post" action="connect.php">
  <select name="dbchoice" id="dbchoice">
    <option value="db1">db1</option>
    <option value="db2">db2</option>
    <option value="db3">db3</option>
  </select>
</form>

Action page:

<?php
$dbchoice = $_POST['$dbchoice']; 
//the above line gets the value from the drop down box on the previous page

mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("$dbchoice") or die(mysql_error());;
//above this text NOW where it sayd $dbchoice, the server is writting in the value from the drop down form from the previous page. this means that the database chosen is the one with the same name as what the user chose on the form.
?>

I hope that answered your question =]

ps. the only thing im not 100% sure about is the format of this bit:

mysql_select_db("$dbchoice")

So if it dosnt work that is the section that is most likely to be wrong. Maybe ask somebody else or try

mysql_select_db($dbchoice)

hey... thanks for replying....

i think your solution should also work....

but i was trying in the meantime, and came upon a solution.

i make the form as usual (that was never a point f debate)

then, in the ACTION-PAGE of the form, i use an "if-else" clause.

meaning that i first store the value of the drop-down into a variable, and then i use a simple if-elseif-else clause.

CODE

$dbchoice = $_POST;
if($dbchoice == 1)
{
mysql_connect(....db1....)
}


else if($dbchoice ==2)
{
mysql_connect(....db2....)
}

and so on..

this solution has worked for me...

i'll try urs as well, though i dont ssee any eason why it should'nt work...

Edited 3 Years Ago by happygeek: fixed formatting

This is not do so good code this a bad programing bcz code should be short and understandable

i know this is not the most efficient method... but nothing else seems to strike me... could you suggest me an alternative??

<?php
$dbchoice = $_POST;
//the above line gets the value from the drop down box on the previous page

mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("$dbchoice") or die(mysql_error());;
//above this text NOW where it sayd $dbchoice, the server is writting in the value from the drop down form from the previous page. this means that the database chosen is the one with the same name as what the user chose on the form.
?><?php
$dbchoice = $_POST;
//the above line gets the value from the drop down box on the previous page

mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("$dbchoice") or die(mysql_error());;
//above this text NOW where it sayd $dbchoice, the server is writting in the value from the drop down form from the previous page. this means that the database chosen is the one with the same name as what the user chose on the form.
?> This is Good ay for Multi Selecting

well yes. i think your code is more efficient than mine, specially in cases where there are a number of options, as, with my code, you will end up with a large number of if-else statements, which can be totally avoided with ur code.


thx..

This article has been dead for over six months. Start a new discussion instead.