doujinai2183 2016-10-09 20:35
浏览 110

在php上点击按钮点击Mysql数据库

I am trying to take backup on button click using following code, on every click I get empty database. I tried all articles , but result is same..Can any one help me solve this.

 <?php
 ob_start();

 $username = "root"; 
 $password = ""; 
 $hostname = "localhost"; 
 $dbname   = "cars";

 $command = "D:\\..\\wamp64\\bin\\mysqldump  --add-drop-table host=$hostname
--user=$username ";
 if ($password) 
    $command.= "--password=". $password ." "; 
 $command.= $dbname;
 system($command);

 $dump = ob_get_contents(); 
 ob_end_clean();

// send dump file to the output
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($dbname . "_" . 
date("Y-m-d_H-i-s").".sql"));

flush();
echo $dump;
exit();
?>
  • 写回答

1条回答 默认 最新

  • duanlun2827 2016-10-09 21:41
    关注

    I was going to go through your code but thought I may as well just give you one I made earlier from one of my sites as it's identical to what your asking for. I can tell you at a quick glance though that the reason why your DB was emptying on click is because you were dropping the database in your exec command using --add-drop-table:

    How to drop and re-populate mysql databases?

    This is the code extracted from one of my backend admin page sites to do random MySQL dumps and backups via broswer when needed. It's very simple but extremly effective if secured properly. The form is set using radio buttons for either download or backup and then executes the local mysqldump from cmdline. Backup will output if it was a success by testing to see if the file has actually backed up and download will download the file which is pretty much put together using the example from the PHP site here. I've shaped it a little so you can add it to your own project

       <?php 
    
    
    $username = "root";
    $password = "root";
    $host = "localhost";
    $dbname = "new";
    $path = "/Applications/MAMP/htdocs/html/test123.sql";
    $backup = exec('/Applications/MAMP/Library/bin/mysqldump --user=' . $username . ' --password='. $password .' --host=' . $host . ' ' . $dbname . ' > ' . $path . '');
    
    if (isset($_POST['backup'])) {
    
            if (file_exists($path)) {
    
                echo "Backup Success";
                echo "<br>";
    
            } else {
    
                echo "Backup failed!";
                echo "<br>";
                echo "Backup of " . $dbname . " does not exist in " . $path;
    
            }
    
    }
    
    if (isset($_POST['download'])) {
    
        if (file_exists($path)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename="'.basename($path).'"');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($path)); //Whoops...Forgot to change variable!
        readfile($path); //Whoops...Forgot to change variable!
        exit;
    } else {
    
        echo "File does not exist!";
    
    }
    
    
    }
    
    ?>
    
    
    <!DOCTYPE html>
    <html>
    <head>
    <title>Backup Page</title>
    </head>
    <body>
    
            <form method="post">
    
                <input type="radio" name="backup"> Backup <br>
                <input type="radio" name="download"> Download <br>
                <input type="submit" value="MySQL Backup">
    
            </form>
    
    </body>
    </html>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思