如何像其他表一样创建表并复制其数据

时间:2017-03-11 15:40:23

标签: php mysql

我想创建一个表,就像从另一个表创建一个表并复制它的数据。它只创建一个表。有人能帮助我吗?

这是我的代码

    <?php
    $servername = "localhost:3307";
$username = "root";
$password = "";
$dbname = "female"; //database 

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname); //open ng connection 
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
if(isset($_POST['submit'])){

if(isset($_POST['username']))
{
$user = $_POST['username'];
$sql = "CREATE TABLE $user LIKE female";

            if ($conn->query($sql) === TRUE) {
    $sql ="INSERT INTO $user SELECT * FROM female";
            }   
} else {
    echo "Error creating table: " . $conn->error;
}
}
$conn->close();

?>

1 个答案:

答案 0 :(得分:1)

您忘记执行第二个查询。

if ($conn->query($sql) === TRUE) {
     $sql ="INSERT INTO $user SELECT * FROM female";
     $conn->query($sql);
} 

请注意,一点点缩进可以使您的代码更具可读性。