php将albumID插入我的音乐商店

时间:2017-01-26 08:11:37

标签: php database mysqli insert

我有这个音乐商店,当我想要添加一个新专辑时它还必须在" artiesten"的表中插入albumID。不知怎的,我不明白。有人有解决方案吗?

以下是我的数据库的图片:https://gyazo.com/64ebabed8655cb204d8d7223f92dfc46

以下是专辑评论的代码:

$db = mysqli_connect($host, $user, $pass,$database);
if($db){
    $h.= "";
    $h.= "<form><table class='table table-striped table-hover'>";
    $h.= "<tr>";
    $h.= "<th>Nr.</th>";
    $h.= "<th>Albums</th>";
    $h.= "<th>Artiest</th>";
    $h.= "<th>Nummer</th>";
    $h.= "<th></th>";
    $h.='<ul class="nav nav-pills" role="tablist">';
    $h.="   <li role='presentation' class='active'><a href='http://localhost:8888/?action=album '>Albums<span class='badge'></span></a></li>";
    $h.='   <li role="presentation"><a href="http://localhost:8888/?action=songs">Songs</a></li>';
    //$h.='<input type="button" class="btn btn-primary style="float: right"><a href="http://localhost:8888/?action=add-album">';
    $h.= "<td style='text-align:right;'><a href='/?action=add-album&albumid=".$_GET['id']."' class='btn btn-primary'>VOEG TOE</a></td>";

    $h.='</ul>';
    $h.="<br>";
    $h.= "</tr>";   

    $sql = mysqli_query($db,"SELECT * FROM albums");
    $sql1 = mysqli_query($db,"SELECT * FROM artiesten");

    if($sql){

    if(mysqli_num_rows($sql)>0) {  //if you have some albums to print...

        while ($row = mysqli_fetch_assoc($sql)) {

            //start printing a row
            $h.= "<tr>";
            $h.= "<td>".$row['id']."</td>";
            $h.= "<td>".$row['albumName']."</td>";

            //now get the artiest for the album by querying the correct albumid
            $albumid = $row['id'];
            $sql1 = mysqli_query($db,"SELECT * FROM artiesten WHERE albumID = '$albumid' ");

            while ($row1 = mysqli_fetch_assoc($sql1)){

                 $h.= "<td>".$row1['artiest']."</td>";  //print the right artiest
            }

            $h.= "<td style='text-align:right;'><a href='/?action=show-songs&id=".$row['id']."'' class='btn btn-primary'>Zie nummers</a>&nbsp";
            $h.= "<style='text-align:right;'><a href='/?action=delete-album&id=".$row['id']."'' class='btn btn-danger'>VERWIJDER</a></td>";
            $h.= "</tr>";

        }   

        }else{

            echo "<tr>No Recore Found</tr>";

        }

        $h.= "</table></form>";

    echo $htop;
    echo $h;
    echo $hbot;

    }

以下是添加新相册时的代码:

    $db = mysqli_connect($host, $user, $pass,$database);

    if($_GET['action2'] == "1"){

        mysqli_query($db, "INSERT INTO artiesten (artiest, albumID) VALUES ('".$_GET['artiestnaam']."')"); 
        mysqli_query($db, "INSERT INTO albums (albumName) VALUES ('".$_GET['albumnaam']."')");                   
        header("Location: /?action=album");

    }

    $h = "";
    $h.= "";

    $h.= "<form><input type='hidden' name='action' value='add-album'><input type='hidden' name='action2' value='1'><input type='hidden' name='id' value='".$_GET['id']."'><table class='table table-striped'>";
    $h.= "  <tr>";
    $h.= "      <td><b>Artiest naam</b></td>";
    $h.= "      <td><input type='text' name='artiestnaam' class='form-control' placeholder='Naam'></td>";
    $h.= "  </tr>";
    $h.= "  <tr>";
    $h.= "      <td><b>Album naam</b></td>";
    $h.= "      <td><input type='text' name='albumnaam' class='form-control' placeholder='Naam'></td>";
    $h.= "  </tr>";
    $h.= "  <tr>";
    $h.= "      <td colspan='2'><input class='btn btn-primary' type='submit' value='UPDATE'></td>";
    $h.= "  </tr>";
    $h.= "</table></form>";

    echo $htop;
    echo $h;
    echo $hbot;

希望任何人都能找到解决方案

0 个答案:

没有答案