可以自动创建多个表吗?

时间:2018-03-16 23:59:05

标签: php mysql sql

我正在创建一个音乐网站。在这个网站上,我在表格之间建立了关系:专辑,乐队和“音乐”。 当我向相册添加歌曲时,这个“musics” 表会自动创建 并带有相册名称,如: allhopeisgone Table

但是,它只会自动创建一个表。可以自动多个吗?

这是我的代码:

$db = mysqli_connect('localhost', 'root', '', 'datamusic');
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if (isset($_GET['id'])) {

    $id    = $_GET['id'];
    $Music = $_POST['Music'];


    $sqlName = "SELECT * FROM albums WHERE idAlbums='$id';";

    $queryName = mysqli_query($db, $sqlName);


    $Album;
    $bandId;
    $idAlbum;
    while ($row = mysqli_fetch_array($queryName)) {
        $Album   = $row['Album'];
        $bandId  = $row['albumId'];
        $idAlbum = $row['idAlbums'];

        if (preg_match('/\s/', $Album)) {
            $Album = preg_replace('/\s+/', '', $Album);
            $sql   = "CREATE TABLE `datamusic`.`$Album` (
                      `idMusic` INT NOT NULL AUTO_INCREMENT,
                      `Music` VARCHAR(45) NULL,
                      `bandId` INT NULL,
                      `albumId` INT NULL,
                      PRIMARY KEY (`idMusic`),
                      INDEX `albumId_idx` (`albumId` ASC),
                      INDEX `bandId_idx` (`bandId` ASC),
                      CONSTRAINT `albumId`
                        FOREIGN KEY (`albumId`)
                        REFERENCES `datamusic`.`albums` (`idAlbums`)
                        ON DELETE NO ACTION
                        ON UPDATE NO ACTION,
                      CONSTRAINT `bandId`
                        FOREIGN KEY (`bandId`)
                        REFERENCES `datamusic`.`bands` (`idBand`)
                        ON DELETE NO ACTION
                        ON UPDATE NO ACTION);";

            mysqli_query($db, $sql);
            $sqlNewMusic = "INSERT INTO $Album  VALUES(null,'$Music','$bandId','$idAlbum');";
            mysqli_query($db, $sqlNewMusic);

        }


        else {
            $sql = "CREATE TABLE `datamusic`.`$Album` (
                  `idMusic` INT NOT NULL AUTO_INCREMENT,
                  `Music` VARCHAR(45) NULL,
                  `bandId` INT NULL,
                  `albumId` INT NULL,
                  PRIMARY KEY (`idMusic`),
                  INDEX `albumId_idx` (`albumId` ASC),
                  INDEX `bandId_idx` (`bandId` ASC),
                  CONSTRAINT `albumId`
                    FOREIGN KEY (`albumId`)
                    REFERENCES `datamusic`.`albums` (`idAlbums`)
                    ON DELETE NO ACTION
                    ON UPDATE NO ACTION,
                  CONSTRAINT `bandId`
                    FOREIGN KEY (`bandId`)
                    REFERENCES `datamusic`.`bands` (`idBand`)
                    ON DELETE NO ACTION
                    ON UPDATE NO ACTION);";

            mysqli_query($db, $sql);
            $sqlNewMusic = "INSERT INTO $Album  VALUES(null,'$Music','$bandId','$idAlbum');";
            mysqli_query($db, $sqlNewMusic);

        }

    }

    header(" Location: editAlbum.php?id=$id");

}

2 个答案:

答案 0 :(得分:0)

您不需要像这样动态创建表格。您应该编写数据库脚本,以便可以在每个新环境中发布它。除此之外,您应该在使用应用程序时插入/更新/删除行。

答案 1 :(得分:0)

我同意nitsram,(以及上述所有内容)在MYSQL中创建表,然后使用您的代码进行更新和编辑。您不希望每个相册都有一个单独的表,这会导致许多表只包含一行。而是创建一个名为music的表,其中包含Album,Artist,Song ......等列。