Налаштовуємо масив $db під свої дані і готово.
Код:
<?php $db['name'] = 'test_db'; $db['host'] = 'localhost'; $db['user'] = 'root'; $db['pass'] = ''; $connect = new PDO("mysql:host=" . $db['host'] . ";dbname=" . $db['name'], $db['user'], $db['pass']); $getAllTableQuery = "SHOW TABLES"; $statement = $connect->prepare($getAllTableQuery); $statement->execute(); $result = $statement->fetchAll(); if(isset($_POST['table'])) { $output = ''; foreach($_POST["table"] as $table) { $showTableQuery = "SHOW CREATE TABLE " . $table; $statement = $connect->prepare($showTableQuery); $statement->execute(); $showTableResult = $statement->fetchAll(); foreach($showTableResult as $showTableRow) { $output .= "\n\n" . $showTableRow["Create Table"] . ";\n\n"; } $selectQuery = "SELECT * FROM " . $table; $statement = $connect->prepare($selectQuery); $statement->execute(); $totalRow = $statement->rowCount(); for($count=0; $count < $totalRow; $count++) { $singleResult = $statement->fetch(PDO::FETCH_ASSOC); $tableColumnArray = array_keys($singleResult); $tableValueArray = array_values($singleResult); $output .= "\nINSERT INTO $table ("; $output .= "" . implode(", ", $tableColumnArray) . ") VALUES ("; $output .= "'" . implode("','", $tableValueArray) . "');\n"; } } $fileName = 'database_backup_on_' . date('y-m-d') . '.sql'; $fileHandle = fopen($fileName, 'w+'); fwrite($fileHandle, $output); fclose($fileHandle); header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename=' . basename($fileName)); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . filesize($fileName)); ob_clean(); flush(); readfile($fileName); unlink($fileName); } ?> <!DOCTYPE html> <html> <head> <title>Створити бекап бази</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous"> </head> <body> <br /> <div class="container"> <div class="row"> <h2 align="center">Створити бекап бази</h2> <br /> <form method="post" id="export_form"> <h3>Оберіть таблиці для бекапу</h3> <?php foreach($result as $table) { ?> <div class="checkbox"> <label><input type="checkbox" class="checkbox_table" name="table[]" value="<?php echo $table["Tables_in_" . $db['name']]; ?>" /> <?php echo $table["Tables_in_" . $db['name']]; ?></label> </div> <?php } ?> <div class="form-group"> <input type="submit" name="submit" id="submit" class="btn btn-info" value="Export" /> </div> </form> </div> </div> </body> </html> <script> $(document).ready(function() { $('#submit').click(function() { var count = 0; $('.checkbox_table').each(function() { if($(this).is(':checked')) { count = count + 1; } }); if(count > 0) { $('#export_form').submit(); } else { alert("Виберіть принаймні одну таблицю для експорту"); return false; } }); }); </script>