【问题标题】:Update Database from Offline从离线更新数据库
【发布时间】:2015-06-24 12:53:34
【问题描述】:

我刚刚使用 PHP Mysql 创建了一个 Web 应用程序,我想将此系统安装在本地服务器和在线 Web 服务器中。但是当 Not Net Connection 时,它存储在本地 mysql 中并自动更新或同步在线 mysql 数据。

请帮帮我怎么可能..我刚刚使用过这个但不工作。

            <form action ='#' method ='post' enctype='multipart/form-data'>
                            <div class="form-group">

                                        <input type='hidden' class="form-control" value='<?php echo $dbname;?>' name='dbname' required>
                            </div>

                            <div class="form-group">
                                        <label>Upload Photograph </label>
                                        <input type="file" name='backup_file'>
                            </div>
                            <input type="submit" class="btn btn-primary" value='Restore' name='submit'>
            </form>


<?php

include_once('conn.php');
// ------------ Upload Process  -------------------------------//
if(isset($_FILES['backup_file']))
{                               
$file = $_FILES ['backup_file'];
$name1 = $file ['name'];
$type = $file ['type'];
$size = $file ['size'];
$tmppath = $file ['tmp_name']; 



if (!is_dir('upload')) 
{
    mkdir('upload');     
}

if( !move_uploaded_file ($tmppath, 'upload/'.$name1))
{
Echo ("Error In File Upload");
}


# UPDATING PROCESS STRAT HERE #

$mysqli = new mysqli('localhost', 'database_user', 'database_password', 'dbname');

if (mysqli_connect_error()) {
    die('Connect Error (' . mysqli_connect_errno() . ') '
            . mysqli_connect_error());
}

echo 'Success... ' . $mysqli->host_info . "<br />";
echo 'Retrieving dumpfile' . "<br />";

$sql = file_get_contents('upload/'.$name1);
if (!$sql){
    die ('Error opening file');
}

echo 'processing file <br />';

mysqli_multi_query($mysqli,$sql);

echo "<h4> Backup Updated Successfully. </h4>";
echo "<h2> Thanks for Using ..GURU DAKSHINA</h2>";

$mysqli->close();

}
?>

【问题讨论】:

    标签: php mysql webserver localhost


    【解决方案1】:
    <?php
    $dbname = $_COOKIE['database'];
    
    backup_tables('localhost','user','password',$dbname);
    
    /* backup the db OR just a table */
        function backup_tables($host,$user,$pass,$dbname,$tables = '*')
        {
            $return='';
            $link = mysql_connect($host,$user,$pass);
            mysql_select_db($dbname,$link);
    
            //get all of the tables
            if($tables == '*')
            {
                $tables = array();
                $result = mysql_query('SHOW TABLES');
                while($row = mysql_fetch_row($result))
                {
                    $tables[] = $row[0];
                }
            }
            else
            {
                $tables = is_array($tables) ? $tables : explode(',',$tables);
            }
    
            //cycle through
            foreach($tables as $table)
            {
                $result = mysql_query('SELECT * FROM '.$table);
                $num_fields = mysql_num_fields($result);
                $return.= 'DROP TABLE IF EXISTS '.$table.';';
                $row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
                $return.= "\n\n".$row2[1].";\n\n";
    
                for ($i = 0; $i < $num_fields; $i++) 
                {
                    while($row = mysql_fetch_row($result))
                    {
                        $return.= 'INSERT INTO '.$table.' VALUES(';
                        for($j=0; $j<$num_fields; $j++) 
                        {
                            $row[$j] = addslashes($row[$j]);
                            $row[$j] = ereg_replace("\n","\\n",$row[$j]);
                            if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
                            if ($j<($num_fields-1)) { $return.= ','; }
                        }
                        $return.= ");\n";
                    }
                }
                $return.="\n\n\n";
            }
    
    
        //save file
    
        $handle = fopen('backup-'.$dbname.'-'.date('d-m-y').'.sql','w+');
    
        $backup = 'backup-'.$dbname.'-'.date('d-m-y').'.sql';
    
        fwrite($handle,$return);
        fclose($handle);
    
            function uploadfile($file)
            {
                $ftp_server = "ftp.domain.com";
                $ftp_user_name = "user@domain.com";
                $ftp_user_pass = "ftp_password";
                // open some file for reading
                //$file = $bname;
                $fp = fopen($file, 'r');
    
                // set up basic connection
                $conn_id = ftp_connect($ftp_server);
    
                // login with username and password
                $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
    
                // try to upload $file
                if (ftp_fput($conn_id, $file, $fp, FTP_ASCII)) {
                    echo "Backup Successfully Uploaded \n";
                } else {
                    echo "There was a problem while uploading $file\n";
                }
    
                // close the connection and the file handler
                ftp_close($conn_id);
                fclose($fp);
            }
    
        uploadfile($backup);
    
        $link ='http://domain.com/update.php?bname='.$backup.'&dbname='.$dbname;
        //echo "<br><a href='http://domain.com/update.php?bname=$backup&dbname=$dbname' target='_blank'> Click Here </a> to Update Database online";
    
        echo("<div class='alert alert-success alert-dismissable'>
                                    <button type='button' class='close' data-dismiss='aler' aria-hidden='true'>&times;</button>
                                    <i class='fa fa-inbox fa-2x'> </i> <a href='$link' target='new' class='alert-link'>Click Here </a>to Update Database Online.
                                </div> ");
        }
    
    ?>
    </div>
    </div>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-09
      相关资源
      最近更新 更多