如果我将Category和Brand留空,SQL只接受我的输入

时间:2016-07-16 17:59:41

标签: php html mysql

您好我正在尝试通过构建电子商务网站来掌握php css和html。每当我输入将其提交到数据库所需的所有字段时,页面就会刷新。但是当我将类别和品牌留空(默认选择类别/品牌)时,数据库将接受它。我已经待了好几个小时了,现在我的头很痛。这是我的代码:

insert_product.php

<form action="insert_product.php" method="post" enctype="multipart/form-data">

            <table align="center" width="700" border="2" bgcolor="orange">

                <tr align="center">
                    <td colspan="7"><h2>Insert New Post Here</h2></td>
                </tr>

                <tr>
                    <td align="right"><b>Product Title:</b></td>
                    <td><input type="text" name="product_title" size="60" /></td>
                </tr>

                <tr>
                    <td align="right"><b>Product Category</b></td>
                    <td>
                        <select name="product_cat" >
                        <option>Select a Category</option>
                            <?php
                            //calls the database in Category List

                            $get_cats = "select * from categories";

                            $run_cats = mysqli_query($con, $get_cats);

                            while ($row_cats=mysqli_fetch_array($run_cats)){
                            $cat_id = $row_cats['cat_id'];
                            $cat_title = $row_cats['cat_title'];

                            echo "<option value='$cat_id'>$cat_title</option>";
                            }
                        ?>
                        </select>
                    </td>
                </tr>

                <tr>
                    <td align="right"><b>Product Brand</b></td>
                    <td>
                        <select name="product_brand" >
                        <option>Select a Brand</option>
                            <?php
                            //copy pasted and edited from getcats

                            $get_brands = "select * from brands";

                            $run_brands = mysqli_query($con, $get_brands); //see $con above and getbrand (executing query)

                            while ($row_brands = mysqli_fetch_array($run_brands)){ //loop fetching data or query + saving record in row brands

                            $brand_id = $row_brands['brand_id'];
                            $brand_title = $row_brands['brand_title']; //fetches brand id and title from the table


                            echo "<option value=$brand_id'>$brand_title</option>"; //connected to ul id="brands"
                        }

                        ?>
                        </select>

                    </td>
                </tr>

                <tr>
                    <td align="right"><b>Product Image</b></td>
                    <td><input type="file" name="product_image" required/></td>
                </tr>

                <tr>
                    <td align="right"><b>Product Price</b></td>
                    <td><input type="text" name="product_price" required/></td>
                </tr>

                <tr>
                    <td align="right"><b>Product Description</b></td>
                    <td><textarea name="product_desc" cols="20" rows="10" ></textarea></td>
                </tr>

                <tr>
                    <td align="right"><b>Product Keywords</b></td>
                    <td><input type="text" name="product_keywords" size="50" required/></td>
                </tr>


                <tr align="center">
                    <td colspan="7"><input type="submit" name="insert_post" value="Insert Product Now" /></td>
                </tr>
            </table>

</body>

</html>

<?php
    //If insert post is clicked, it will accept the data inputted
    if(isset($_POST['insert_post'])){

            $product_title = $_POST['product_title'];
            $product_cat = $_POST['product_cat'];
            $product_brand = $_POST['product_brand'];
            $product_price = $_POST['product_price'];
            $product_desc = $_POST['product_desc'];
            $product_keywords = $_POST['product_keywords'];

            //getting the image file
            $product_image = $_FILES['product_image']['name'];
            $product_image_tmp = $_FILES['product_image']['tmp_name'];

            move_uploaded_file($product_image_tmp,"product_images/product_image");


            $insert_product = "insert into products (product_cat,product_brand,product_title,product_price,product_desc,product_image,product_keywords) values ('$product_cat','$product_brand','$product_title','$product_price','$product_desc','$product_image','$product_keywords')";

            $insert_pro = mysqli_query($con, $insert_product);

            if($insert_pro) {

            echo "<script>alert('Product has been inserted!')</script>";
            echo "<script>windows.open('insert_product.php','_self')</script>";
            }
    }
?>

1 个答案:

答案 0 :(得分:0)

对不起,这是我第一次回答:

 <select name="product_brand" >
                        <option>Select a Brand</option>
                            <?php
                            //copy pasted and edited from getcats

                            $get_brands = "select * from brands";

                            $run_brands = mysqli_query($con, $get_brands); //see $con above and getbrand (executing query)

                            while ($row_brands = mysqli_fetch_array($run_brands)){ //loop fetching data or query + saving record in row brands

                            $brand_id = $row_brands['brand_id'];
                            $brand_title = $row_brands['brand_title']; //fetches brand id and title from the table


                            echo "<option value=$brand_id'>$brand_title</option>"; //connected to ul id="brands"
                        }

问题是我忘了在$ brand_id变量中放入一个撇号。