The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

PHP/MySQL question

TownieTownie Registered User regular
edited February 2007 in Help / Advice Forum
I'm trying to make a script support multiple databases. I'm doing the:
include ('/absolute/pathway/includes/something.php');
thing for the db info and it works well if I'm setting a specific database. But I'm trying to make something that lets users select from a drop down menu (or populates a radio button group, or something in that vein) of files in the includes folder so they can select which db they want the script including. Below that I've got dynamic items depending on the db. I'm using:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
so it should refresh once the settings are updated. I have a drop down code to list the files in the directory but I'm messing up on getting it to switch to the file selected. Any coding gurus have any ideas?

-Ben
sig4.pngsig1.png
sig3.png sig2.png
Townie on

Posts

  • TownieTownie Registered User regular
    edited February 2007
    Just to clarify - I'm new at coding and when I try to script something to add a variable to the pathway it keeps kicking back about a T_string. I'm sure there's got to be a cleaner way of doing this but I'm barely a novice

    Townie on
    -Ben
    sig4.pngsig1.png
    sig3.png sig2.png
  • RoundBoyRoundBoy Registered User regular
    edited February 2007
    Im not sure what you are getting at... you want to have the user choose between multiple database systems (mysql,mssql,access ?) etc ?

    or select different tables ?

    off the top of my head... i would have a drop down box populate a field with option 1, two, three, etc


    and on your single processing page, branch off depending :
    switch($_POST['optionfield']){
    
        case 'option1':
    /* stuff for option 1 here */
        break;
    
        case 'option2':
    /* stuff for option 2 here */
        break;
    
        case 'option3':
    /* stuff for option 3 here */
        break;
    
    
    }
    

    etc .. as for your T_String Error... you are missing a semicolon or bracket .. look up the line number in question, and search the lines before it as well... make sure your code is kosher.

    The *real* way to support multiple database systems is really code a single database class, where you do a single call to a function, and it is handled depending on the user selection.

    more detail on what you are trying to accomplish ?

    RoundBoy on
    sig_civwar.jpg
    Librarians harbor a terrible secret. Find it.
  • TownieTownie Registered User regular
    edited February 2007
    Im not sure what you are getting at... you want to have the user choose between multiple database systems (mysql,mssql,access ?) etc ?

    or select different tables ?
    Same system, different tables. Different databases, actually. I'm designing it for people who use the Wordpress theme Comicpress to manage their webcomic. It labels their storylines and lets them add/edit/delete the titles. I run two strips on seperate sites with different databases and I was trying to let the user decide which site's db they want to edit by selecting an info file from the drop down. (1.php would hold the user name, password, and db name for the first db; 2.php for the second, and so on) That way they could edit all their sites from one install.
    off the top of my head... i would have a drop down box populate a field with option 1, two, three, etc


    and on your single processing page, branch off depending :
    
    switch($_POST['optionfield']){ 
    
        case 'option1': 
    /* stuff for option 1 here */ 
        break; 
    
        case 'option2': 
    /* stuff for option 2 here */ 
        break; 
    
        case 'option3': 
    /* stuff for option 3 here */ 
        break; 
    
    
    } 
     
    
    That's a little more hard-coded than what I was looking for, though. What I'm getting at is I'd like the user to be able to drop as many different db info files in their folder as they need and, when they select one from the drop down and submit, the page to edit their table entries changes to match the db they set. I found a script that can list the items in a directory, I'm just hung up on getting the page to refresh with the new titles and dates based on the change.
    etc .. as for your T_String Error... you are missing a semicolon or bracket .. look up the line number in question, and search the lines before it as well... make sure your code is kosher.

    The *real* way to support multiple database systems is really code a single database class, where you do a single call to a function, and it is handled depending on the user selection.

    more detail on what you are trying to accomplish ?
    I know the T_String error has been coming from my feeble attempts to combine the result of the the drop down menu to the absolute pathway. I was trying to write something like -
    $ap = '/absoulte/pathway/includes/');
    $dd=$_POST['ResultsFromDropdown'];
    include ($ap+$dd);
    
    But that's not gonna work, hence the error. (I'm still learning how to keep my coding kosher. Most of my knowledge of code has come from online referances and necessity. :P ) Does that clear up what I'm trying to do?

    Townie on
    -Ben
    sig4.pngsig1.png
    sig3.png sig2.png
  • rock217rock217 Registered User regular
    edited February 2007
    $ap = '/absoulte/pathway/includes/');
    $dd=$_POST;
    include ($ap+$dd);

    Firstly, "+" is for addition, "." is for string building

    This is probably what you meant:
    include ($ap . $dd);
    Which is perfectly valid assuming the file exists and is readable by the apache user.

    With that said, this approach is a bad idea, as I could pass "../../path/to/some/evil/script.php", or something along those lines to be malicious.

    If I were trying to accomplish the described functionality, I would probably require your DB conn files to reside in 1 folder, dump a readdir() or something on that folders contents to an array, then see if $_POST was in_array() prior to including, (don't forget you may have to stripslashes() what you extract out of $_POST, depending on your php config)

    rock217 on
    don't draw an ascii penis...don't draw an ascii penis...don't draw an ascii penis...
  • TownieTownie Registered User regular
    edited February 2007
    I knew my lack of familiarity with proper syntax would give itself away. Anyway, security's been a big concern of mine in developing this thing. Any help in making this as secure as possible is certainly appreciated. I'm putting it into a Wordpress plugin that should only be accessable by administrators (which also means I might have to adjust the formatting a little in the final version, since WP has it's own quirks) and I've been trying to divy up the db info and other sensitive data so it's harder to pull from. Edit: I think it'd be better if I just ask a question here.
    If I were trying to accomplish the described functionality, I would probably require your DB conn files to reside in 1 folder, dump a readdir() or something on that folders contents to an array, then see if $_POST was in_array() prior to including, (don't forget you may have to stripslashes() what you extract out of $_POST, depending on your php config)
    The drop down uses something like this -
    $some_variable = opendir($directory_path);
           while (false !== ($file = readdir($some_variable))) {
               echo "<OPTION VALUE=\'$file'>". $file. '</option>';
    
    is that along the lines you're talking about? Would the processing page require something like this?
    $_POST['ResultsFromDropdown'] = $dropdown_results;
    while (false !== ($file = readdir($some_variable))) {
    if($_POST['ResultsFromDropdown'] = $file){
    include ($some_variable . $dropdown_results);
    }
    }
    
    Then I'd have to include some default setting if this is the first time they've loaded the page and haven't selected anything from the dropdown. Any suggestions on how to protect that so somebody can't just open the page and access the default db?

    Townie on
    -Ben
    sig4.pngsig1.png
    sig3.png sig2.png
Sign In or Register to comment.