User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 391,691 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,206 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 401 | Replies: 8
Reply
Join Date: Jun 2008
Posts: 44
Reputation: akshit is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
akshit akshit is offline Offline
Light Poster

dynamic selection of MySQL database based on user input

  #1  
Jul 1st, 2008
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..
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2006
Location: UK
Posts: 136
Reputation: MaxMumford is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
MaxMumford's Avatar
MaxMumford MaxMumford is offline Offline
Junior Poster

Re: dynamic selection of MySQL database based on user input

  #2  
Jul 1st, 2008
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)
Economizerz Hosting
Price Beater Promise

10% Cheaper Hosting, & Double Bandwidth and Web Space
Click here to apply
Reply With Quote  
Join Date: Jun 2008
Posts: 44
Reputation: akshit is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
akshit akshit is offline Offline
Light Poster

Re: dynamic selection of MySQL database based on user input

  #3  
Jul 1st, 2008
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...
Reply With Quote  
Join Date: Oct 2006
Location: UK
Posts: 136
Reputation: MaxMumford is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
MaxMumford's Avatar
MaxMumford MaxMumford is offline Offline
Junior Poster

Re: dynamic selection of MySQL database based on user input

  #4  
Jul 2nd, 2008
Okay Glad you got things working.
Economizerz Hosting
Price Beater Promise

10% Cheaper Hosting, & Double Bandwidth and Web Space
Click here to apply
Reply With Quote  
Join Date: Jul 2008
Posts: 3
Reputation: Narayan2008 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Narayan2008 Narayan2008 is offline Offline
Newbie Poster

Re: dynamic selection of MySQL database based on user input

  #5  
Jul 2nd, 2008
This is not do so good code this a bad programing bcz code should be short and understandable
Reply With Quote  
Join Date: Jun 2008
Posts: 44
Reputation: akshit is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
akshit akshit is offline Offline
Light Poster

Re: dynamic selection of MySQL database based on user input

  #6  
Jul 2nd, 2008
i know this is not the most efficient method... but nothing else seems to strike me... could you suggest me an alternative??
Reply With Quote  
Join Date: Jul 2008
Posts: 3
Reputation: Narayan2008 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Narayan2008 Narayan2008 is offline Offline
Newbie Poster

Re: dynamic selection of MySQL database based on user input

  #7  
Jul 3rd, 2008
Sori Akshit Yesterday I didn't Answerd U
Reply With Quote  
Join Date: Jul 2008
Posts: 3
Reputation: Narayan2008 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Narayan2008 Narayan2008 is offline Offline
Newbie Poster

Re: dynamic selection of MySQL database based on user input

  #8  
Jul 3rd, 2008
<?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
Reply With Quote  
Join Date: Jun 2008
Posts: 44
Reputation: akshit is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
akshit akshit is offline Offline
Light Poster

Re: dynamic selection of MySQL database based on user input

  #9  
Jul 3rd, 2008
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..
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb PHP Marketplace
Thread Tools Display Modes

Other Threads in the PHP Forum

All times are GMT -4. The time now is 2:33 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC