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 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值