PHP MYSQL - 根据特定类别添加项目

时间:2016-02-15 10:22:42

标签: php mysql

我正在创建一个关于音乐专辑的网站,我有一些关于将我的产品订购到特定类别的问题。我的数据库中有一张名为专辑的表格,在该表格中我只有12张专辑,每个类别分为4张专辑。在我的index.php中,我有3个类别标题,分别是“新版本,热门专辑,特别优惠”。在每个标题中,我每个类别只显示4张专辑。这是我在index.php中的代码:

NEW RELEASES SECTION START HERE
    <section>
            <div class='container dark_grey_color cont_space'>
                <div class='row dark_grey'>
                    <div class='title'>
                        <?php
                        include ("../../includes/database/db.php");
                            $query = mysqli_query ($dbconn, "SELECT id,title FROM sections_titles WHERE id  = 1");
                            $result = mysqli_fetch_assoc($query);
                            if (isset($_GET["id"]))
                            $id = $result["id"];
                            $title = $result["title"];
                            echo "<h3>$title</h3>";
                        ?>
                    </div> 
                    <div class='devider'>
                    </div>
                </div>


                <div class='row'>
                    <?php
                        $query = mysqli_query ($dbconn, "SELECT * FROM albums ORDER BY id DESC LIMIT 4 OFFSET 8");
                        while($result = mysqli_fetch_assoc($query)){
                        $id = $result["id"];
                        $album_name = $result["album_name"]; 
                        $img = $result["image"]; 
                        $artist = $result["artist"]; 
                        $company = $result["company"]; 
                        $genre = $result["genre"]; 
                        $price = $result["price"]; 


                            echo "<div class='col-md-3 col-xs-6'>
                            <a href ='album_page.php?id=$id' target='_blank' class='box_link_hover'><div class='box'>
                            <div class='inside_box'>
                            <div class='small_title'>
                            <h4>$album_name</h4>
                            </div>
                            <div class='photo_box'>
                            <img src=$img class='img_dim'>
                            </div>
                            <div class='info'>
                            <p>Artist: $artist</p>
                            <p>Company: $company</p>
                            <p>Genre: $genre</p>
                            <p>Price: $price</p>
                            </div>
                            </div>
                            <div class='buy_now'>
                            <p>Buy Now</p></a>
                            </div>
                            </div>
                            </div>";
                    }
                    ?>
                </div>
            </div>
            <!-- NEW RELEASES ALBUMS SECTION END HERE -->

            <!-- POPULAR ALBUMS SECTION START HERE -->

            <div class='container light_grey_color cont_space'>
                <div class='row dark_grey'>
                    <div class='title'>
                        <?php
                            $query = mysqli_query ($dbconn, "SELECT id,title FROM sections_titles WHERE id  = 2");
                            $result = mysqli_fetch_assoc($query);
                            if (isset($_GET["id"]))
                            $id = $result["id"];
                            $title = $result["title"];
                            echo "<h3>$title</h3>";
                        ?>
                    </div>
                    <div class='devider'>
                    </div>
                </div>

                <div class='row'>
                    <?php
                        $query = mysqli_query ($dbconn, "SELECT * FROM albums ORDER BY id DESC LIMIT 4 OFFSET 4");
                        while($result = mysqli_fetch_assoc($query)){
                        $id = $result["id"];
                        $album_name = $result["album_name"]; 
                        $img = $result["image"]; 
                        $artist = $result["artist"]; 
                        $company = $result["company"]; 
                        $genre = $result["genre"]; 
                        $price = $result["price"]; 


                            echo "<div class='col-md-3 col-xs-6'>
                            <a href ='album_page.php?id=$id' target='_blank' class='box_link_hover'><div class='box'>
                            <div class='inside_box'>
                            <div class='small_title'>
                            <h4>$album_name</h4>
                            </div>
                            <div class='photo_box'>
                            <img src=$img class='img_dim'>
                            </div>
                            <div class='info'>
                            <p>Artist: $artist</p>
                            <p>Company: $company</p>
                            <p>Genre: $genre</p>
                            <p>Price: $price</p>
                            </div>
                            </div>
                            <div class='buy_now'>
                            <p>Buy Now</p></a>
                            </div>
                            </div>
                            </div>";
                    }
                    ?>

                </div>
                <!-- end of row -->
            </div>
            <!-- POPULAR ALBUMS SECTION END HERE -->

            <!-- SPECIAL OFFERS SECTION START HERE -->
            <div class='container dark_grey_color cont_space'>
                <div class='row dark_grey'>
                    <div class='title'>
                        <?php
                            $query = mysqli_query ($dbconn, "SELECT id,title FROM sections_titles WHERE id  = 3");
                            $result = mysqli_fetch_assoc($query);
                            if (isset($_GET["id"]))
                            $id = $result["id"];
                            $title = $result["title"];
                            echo "<h3>$title</h3>";
                        ?>
                    </div>
                    <div class='devider'>
                    </div>
                </div>
                <div class='row'>


                    <?php
                        $query = mysqli_query ($dbconn, "SELECT * FROM albums ORDER BY id DESC LIMIT 4 ");
                        while($result = mysqli_fetch_assoc($query)){
                        $id = $result["id"];
                        $album_name = $result["album_name"]; 
                        $img = $result["image"]; 
                        $artist = $result["artist"]; 
                        $company = $result["company"]; 
                        $genre = $result["genre"]; 
                        $price = $result["price"]; 


                            echo "<div class='col-md-3 col-xs-6'>
                            <a href ='album_page.php?id=$id' target='_blank' class='box_link_hover'><div class='box'>
                            <div class='inside_box'>
                            <div class='small_title'>
                            <h4>$album_name</h4>
                            </div>
                            <div class='photo_box'>
                            <img src=$img class='img_dim'>
                            </div>
                            <div class='info'>
                            <p>Artist: $artist</p>
                            <p>Company: $company</p>
                            <p>Genre: $genre</p>
                            <p>Price: $price</p>
                            </div>
                            </div>
                            <div class='buy_now'>
                            <p>Buy Now</p></a>
                            </div>
                            </div>
                            </div>";
                    }
                    ?>
                </div>
                <!-- end of row -->
            </div>

Form的My Backend代码是:

echo"<form action='albums/add_album.php' method='post' name='albums'>
    <table border='0' cellpadding='5' cellspacing='0'>
        <tr>
            <td width='120'>Album Name:</td>
            <td><input type='text' name='album_name' value=''></td>
        </tr>
        <tr>
            <td width='120'>Image:</td>
            <td><input type='text' name='img' value=''></td>
        </tr>
        <tr>
            <td width='120'>Artist:</td>
            <td><input type='text' name='artist' value=''></td>
        </tr>
        <tr>
            <td width='120'>Company:</td>
            <td><input type='text' name='company' value=''></td>
        </tr>
        <tr>
            <td width='120'>Genre:</td>
            <td><input type='text' name='genre' value=''></td>
        </tr>
        <tr>
            <td width='120'>Price:</td>
            <td><input type='text' name='price' value=''></td>
        </tr>
        <tr>
            <td width='120'>Category:</td>
            <td>
                <select name ='category'>
                    <option value = 'new releases'>New Releases</option>
                    <option value = 'popular songs'>Popular Songs</option>
                    <option value = 'special offers'>Special Offers</option>
                </select>
            </td>
        </tr>
        <tr>
        <tr>
            <td align='center' colspan='2'><input type='submit' value='Add Album'></td>
        </tr>   
    </table>
</form>";

我的后端add_album.php是这段代码:

<?php
include('../../includes/database/db.php');
$id = $_GET["id"];
$album_name = $_POST['album_name'];
$img = $_POST['image'];
$artist = $_POST['artist'];
$company = $_POST['company'];
$genre = $_POST['genre'];
$price = $_POST['price'];
$category = $_POST['category'];


    if ($album_name!='' && $artist!='' && $company!='' && genre!='' && $price!=''  && $category!='') {
    $sql = mysqli_query ($dbconn, "INSERT INTO albums (album_name, image, artist,company,genre,price) VALUES ('$album_name','$img','$artist','$company','$genre','$price','".$category."') ");
    echo'<meta http-equiv="refresh" content="0;url=../index.php?page=albums&action=list">';
}else{
    echo'<meta http-equiv="refresh" content="0;url=../index.php?page=albums&action=add&msg=empty">';
}

?>

我想要发生的是根据类别插入新的音乐专辑,例如,如果我在后端选择新版本,仅在该行上显示专辑,或者我选择流行歌曲作为我的类别来显示它在第二部分,如果我选择特别优惠以在该类别中显示它。希望我已经很好地解释了我想要实现的目标,并希望有人能得到答案。

谢谢

1 个答案:

答案 0 :(得分:0)

也许如果我在add_album.php中有这样的东西:

<?php
include('../../includes/database/db.php');

$id = $_GET["id"];
$album_name = $_POST['album_name'];
$img = $_POST['image'];
$artist = $_POST['artist'];
$company = $_POST['company'];
$genre = $_POST['genre'];
$price = $_POST['price'];
$category = $_POST['category'];


    if ($category == 'new releases') {
    $sql = mysqli_query ($dbconn, "INSERT INTO albums (album_name, image, artist,company,genre,price, category) VALUES ('$album_name','$img','$artist','$company','$genre','$price', '$category') WHERE id BETWEEN 1 AND 4 ) ");
    echo'<meta http-equiv="refresh" content="0;url=../index.php?page=albums&action=list">';
}elseif ($category == 'popular albums'){
    $sql = mysqli_query ($dbconn, "INSERT INTO albums (album_name, image, artist,company,genre,price, category) VALUES ('$album_name','$img','$artist','$company','$genre','$price', '$category') WHERE id BETWEEN 5 AND 8 ) ");
    echo'<meta http-equiv="refresh" content="0;url=../index.php?page=albums&action=list">';
}elseif ($category == 'special offers'){
    $sql = mysqli_query ($dbconn, "INSERT INTO albums (album_name, image, artist,company,genre,price, category) VALUES ('$album_name','$img','$artist','$company','$genre','$price', '$category') WHERE id BETWEEN 9 AND 12 ) ");
    echo'<meta http-equiv="refresh" content="0;url=../index.php?page=albums&action=list">';
}else{
    echo'<meta http-equiv="refresh" content="0;url=../index.php?page=albums&action=add&msg=empty">';
}

?>

这不起作用我只是觉得这样的事情可能有效