Jump to content
Larry Ullman's Book Forums

Uploading To 2 Separate Sql Databases On Different Servers


Recommended Posts

Hi:  I am trying to figure out a way to send data to multiple SQL databases on two separate servers, and I did come across a couple of scripts that I have modified to my scripts, but I still can't get this to work with the mysql_connect.php.  Any ideas how to have multiple database connections on one script?  Thanks in advance.  Here are the scripts I'm using from Larry's book... 

---------------------------------------------------------------------------------------------------------

<?php # Script 12.8 - add_file.php
// This page allows users to upload files to the server.

// Set the page title and include the HTML header.
$page_title = 'Upload a File';
include ('./includes/header.html');
if(!isset($_SESSION['user_id'])){
        session_unset();
        session_destroy();
        header("Location:../login.php");
        exit;
        }

$counter = 1; // Number of files to allow for.

if (isset($_POST['submitted'])) { // Handle the form.

    require_once ('./mysql_connect.php'); // Connect to the database.

    
    for ($i = 0; $i < $counter; $i++) { // Handle each uploaded file.
    
        // Create index names to refer to the proper upload and candidate information.
                
        $firstname = 'firstname' . $i;
        $lastname = 'lastname' . $i;
        $email = 'email' . $i;
        $citizenship = 'citizenship' . $i;
        $location = 'location' . $i;
        $filename = 'upload' . $i;


    
        // Check for a file.
        
                    // Check for a first name (not required).
            if (!empty($_POST[$firstname])) {
                $f = "'" . escape_data($_POST[$firstname]) . "'";
            } else {
                $f = 'NULL';
            }

        
            // Check for a last name (not required).
            if (!empty($_POST[$lastname])) {
                $l = "'" . escape_data($_POST[$lastname]) . "'";
            } else {
                $l = 'NULL';
            }


            
                // Check for a email (not required).
            if (!empty($_POST[$email])) {
                $e = "'" . escape_data($_POST[$email]) . "'";
            } else {
                $e = 'NULL';
            }
            
                    // Check for a citizenship (not required).
            if (!empty($_POST[$citizenship])) {
                $c = "'" . escape_data($_POST[$citizenship]) . "'";
            } else {
                $c = 'NULL';
            }
            
                        // Check for a first name (not required).
            if (!empty($_POST[$location])) {
                $o = "'" . escape_data($_POST[$location]) . "'";
            } else {
                $o = 'NULL';
            }
                    if (isset($_FILES[$filename]) && ($_FILES[$filename]['error'] != 4)) {

            // Add the record to the database.
            $query = "INSERT INTO uploads (file_name, file_size, file_type, firstname, lastname, email, citizenship, location) VALUES ('{$_FILES[$filename]['name']}', {$_FILES[$filename]['size']}, '{$_FILES[$filename]['type']}', $f, $l, $e, $c, $o)";
            $result = mysql_query ($query);
        
            if ($result) {
                
                // Return the upload_id from the database.
                $upload_id = mysql_insert_id();
                
                // Move the file over.
                if (move_uploaded_file($_FILES[$filename]['tmp_name'], "../uploads_stews/$upload_id")) {
                
                    echo '<p><br><h2><strong>Thank you.  Your CV has successfully been uploaded!</h2></strong></p>';

                    
                } else { // File could not be moved.
                
                    echo '<p><font color="red">Your CV could not be uploaded.  Either your image was too large (over 500kb) or you forgot to add the file.  Please try again..</font></p>';
        
                    // Remove the record from the database.
                    $query = "DELETE FROM uploads WHERE upload_id = $upload_id";
                    $result = mysql_query ($query);
                    
                    // Add more detailed error reporting, if desired.
                    
                }
                
            } else { // If the query did not run OK.
                echo '<p><font color="red">Your submission could not be processed due to a system error. We apologize for any inconvenience.</font></p>';
                // Print the query and invoke the mysql_error() function to debug.
            }
            
        } // End of if (isset($the_file)...
        
    } // End of FOR loop.
    
    mysql_close(); // Close the database connection.
        
} // End of the main Submit conditional.
?>
<form enctype="multipart/form-data" action="add_file_thankyou.php" method="post">

    <fieldset><legend>Fill out the form to upload a file:</legend>
    <input type="hidden" name="MAX_FILE_SIZE" value="524288">
    
    <?php // Create the inputs.
    for ($i = 0; $i < $counter; $i++) {
        echo '<p><b>First Name:</b> <input type="text" name="firstname' . $i . '" size="20"</p>
            <p><b>Last name:</b> <input type="text" name="lastname' . $i . '" size="20"></p>
    <p><b>Email Address:</b> <input type="text" name="email' . $i . '" size="20"></p>
<p><b>Nationality:</b> <input type="text" name="citizenship' . $i . '" size="20"></p>
<p><b>Your Current Location:</b> <input type="text" name="location' . $i . '" size="20"></p>
<p><b>Upload Your CV:</b> <input type="file" name="upload' . $i . '" /></p>


    ';
    }
    ?>
    
    </fieldset>
    <input type="hidden" name="submitted" value="TRUE" />
    <div align="center"><input type="submit" name="submit" value="Submit" /></div>

</form>
<?php
include ('./includes/footer.html');

?>

------------------------------------------------------------------------------------------------------------

 

and the add_file script

 

------------------------------------------------------------------------------------------------------------

<?php # Script 12.8 - add_file.php
// This page allows users to upload files to the server.

// Set the page title and include the HTML header.
$page_title = 'Upload a File';
include ('./includes/header.html');
if(!isset($_SESSION['user_id'])){
        session_unset();
        session_destroy();
        header("Location:../login.php");
        exit;
        }

$counter = 1; // Number of files to allow for.

if (isset($_POST['submitted'])) { // Handle the form.

    require_once ('./mysql_connect.php'); // Connect to the database.

    
    for ($i = 0; $i < $counter; $i++) { // Handle each uploaded file.
    
        // Create index names to refer to the proper upload and candidate information.
                
        $firstname = 'firstname' . $i;
        $lastname = 'lastname' . $i;
        $email = 'email' . $i;
        $citizenship = 'citizenship' . $i;
        $location = 'location' . $i;
        $filename = 'upload' . $i;


    
        // Check for a file.
        
                    // Check for a first name (not required).
            if (!empty($_POST[$firstname])) {
                $f = "'" . escape_data($_POST[$firstname]) . "'";
            } else {
                $f = 'NULL';
            }

        
            // Check for a last name (not required).
            if (!empty($_POST[$lastname])) {
                $l = "'" . escape_data($_POST[$lastname]) . "'";
            } else {
                $l = 'NULL';
            }


            
                // Check for a email (not required).
            if (!empty($_POST[$email])) {
                $e = "'" . escape_data($_POST[$email]) . "'";
            } else {
                $e = 'NULL';
            }
            
                    // Check for a citizenship (not required).
            if (!empty($_POST[$citizenship])) {
                $c = "'" . escape_data($_POST[$citizenship]) . "'";
            } else {
                $c = 'NULL';
            }
            
                        // Check for a first name (not required).
            if (!empty($_POST[$location])) {
                $o = "'" . escape_data($_POST[$location]) . "'";
            } else {
                $o = 'NULL';
            }
                    if (isset($_FILES[$filename]) && ($_FILES[$filename]['error'] != 4)) {

            // Add the record to the database.
            $query = "INSERT INTO uploads (file_name, file_size, file_type, firstname, lastname, email, citizenship, location) VALUES ('{$_FILES[$filename]['name']}', {$_FILES[$filename]['size']}, '{$_FILES[$filename]['type']}', $f, $l, $e, $c, $o)";
            $result = mysql_query ($query);
        
            if ($result) {
                
                // Return the upload_id from the database.
                $upload_id = mysql_insert_id();
                
                // Move the file over.
                if (move_uploaded_file($_FILES[$filename]['tmp_name'], "../uploads_stews/$upload_id")) {
                
                    echo '<p><br><h2><strong>Thank you.  Your CV has successfully been uploaded!</h2></strong></p>';

                    
                } else { // File could not be moved.
                
                    echo '<p><font color="red">Your CV could not be uploaded.  Either your image was too large (over 500kb) or you forgot to add the file.  Please try again..</font></p>';
        
                    // Remove the record from the database.
                    $query = "DELETE FROM uploads WHERE upload_id = $upload_id";
                    $result = mysql_query ($query);
                    
                    // Add more detailed error reporting, if desired.
                    
                }
                
            } else { // If the query did not run OK.
                echo '<p><font color="red">Your submission could not be processed due to a system error. We apologize for any inconvenience.</font></p>';
                // Print the query and invoke the mysql_error() function to debug.
            }
            
        } // End of if (isset($the_file)...
        
    } // End of FOR loop.
    
    mysql_close(); // Close the database connection.
        
} // End of the main Submit conditional.
?>
<form enctype="multipart/form-data" action="add_file_thankyou.php" method="post">

    <fieldset><legend>Fill out the form to upload a file:</legend>
    <input type="hidden" name="MAX_FILE_SIZE" value="524288">
    
    <?php // Create the inputs.
    for ($i = 0; $i < $counter; $i++) {
        echo '<p><b>First Name:</b> <input type="text" name="firstname' . $i . '" size="20"</p>
            <p><b>Last name:</b> <input type="text" name="lastname' . $i . '" size="20"></p>
    <p><b>Email Address:</b> <input type="text" name="email' . $i . '" size="20"></p>
<p><b>Nationality:</b> <input type="text" name="citizenship' . $i . '" size="20"></p>
<p><b>Your Current Location:</b> <input type="text" name="location' . $i . '" size="20"></p>
<p><b>Upload Your CV:</b> <input type="file" name="upload' . $i . '" /></p>


    ';
    }
    ?>
    
    </fieldset>
    <input type="hidden" name="submitted" value="TRUE" />
    <div align="center"><input type="submit" name="submit" value="Submit" /></div>

</form>
<?php
include ('./includes/footer.html');

?>

Link to comment
Share on other sites

One of the simplest ways to do this is to initilize two database connection variables in your mysql.php script. When you perform queries on the second database, use the new variable instead.

<?php # mysqli.php

$dbc = mysli_connect( ... ); // You know the drill.

$other = mysqli_connect( ... ); // The details for the new connection

Inside your scripts, you can now use both $dbc and $other

require "mysql.php"; // Require connection script

$query = "SELECT * from someTable";
$result = mysqli_query($dbc, $query); // Using the "standard" connection

$query = "SELECT * from someOtherTable";
$result = mysqli_query($other, $query); // Using the "other" connection

Keep in mind that the mysql functions are deprecated. You should switch to mysqli. Most functions are almost identical, though.

Link to comment
Share on other sites

 Share

×
×
  • Create New...