954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

dynamic selection of MySQL database based on user input

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..

akshit
Light Poster
46 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

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)

MaxMumford
Posting Whiz in Training
228 posts since Oct 2006
Reputation Points: 32
Solved Threads: 3
 

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['$dbchoice'];
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...

akshit
Light Poster
46 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

Okay :) Glad you got things working.

MaxMumford
Posting Whiz in Training
228 posts since Oct 2006
Reputation Points: 32
Solved Threads: 3
 

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

Narayan2008
Newbie Poster
3 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

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

akshit
Light Poster
46 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

Sori Akshit Yesterday I didn't Answerd U

Narayan2008
Newbie Poster
3 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

<?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.
?><?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.
?> This is Good ay for Multi Selecting

Narayan2008
Newbie Poster
3 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

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..

akshit
Light Poster
46 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You