好了,因此我已经能够成功地采用PHPMyAdmin创建的数据库,并使用php将其备份到客户端。现在,我正在尝试将该数据库还原为同一环境中的新数据库。
我已经找到了可以在mysql中运行的命令,但是我似乎无法使其与客户端从其计算机上载的文件一起使用。
这是我现在拥有的代码:
<?php
// display form if user has not clicked submit
if (!isset($_POST["btn_submit"]))
{
?>
<!--This will be the form that will hold the information of the entire page.-->
<form enctype="multipart/form-data"class="elegant-aero" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<h1>Restore Database</h1>
<p>
<label>
<span>Database File</span>
<input type="file" name="backupFile" value="Upload File">
</label>
<!--Submit Button-->
<label>
<span> </span>
<input type="submit" name="btn_submit" class="button" value="Add Scenario"/>
</label>
</p>
</form>
<?php
} //end if
else
{
if(isset($_FILES['backupFile']['error']) && UPLOAD_ERR_OK == $_FILES['backupFile']['error'])
{
//Format sql file
$file = addslashes(file_get_contents($_FILES['backupFile']['tmp_name']));
} //end if
//Set up the MySQL Server
$servername = "localhost";
$username = "root";
$password = "password";//hide your password
$dbname = "test";
//Create connection to the MySQL server
$conn = new mysqli($servername, $username, $password);
$sql1 = "CREATE DATABASE test";
if($conn->query($sql1) === TRUE)
{
echo "Database created successfully<br>";
} else {
echo "Error creating database: " . $conn->error;
} //end else
$sql2 = "mysql --user={$username} --password={$password} --database={$dbname} < $file";
if($conn->query($sql2) === TRUE)
{
echo "Information uploaded successfully<br>";
} else {
echo "Error uploading information: " . $conn->error;
} //end else
} //end else
?>
您的代码有很多问题(fileScenDetails是从哪里来的?表单中的backupFile发生了什么?),但是根据您的问题:
$sql2 = "mysql --user={$username} --password={$password} --database={$dbname} < $file";
if($conn->query($sql2) === TRUE)
{
echo "Information uploaded successfully<br>";
} else {
echo "Error uploading information: " . $conn->error;
} //end else
您正在尝试运行* nix命令作为数据库查询。代替:
if($conn->query($sql2) === TRUE)
您需要执行以下操作:
if(exec($sql2) === TRUE)
您确实需要在这里仔细阅读您想做的事情,因为这样做真的很危险,因此初学者不应该尝试。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句