php - 如果我将类别和品牌留空,SQL 仅接受我的输入

标签 php html mysql

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

插入_产品.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>";
            }
    }
?>

最佳答案

抱歉,这是我第一次回答:

 <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 变量中添加一个撇号。

关于php - 如果我将类别和品牌留空,SQL 仅接受我的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38414030/

相关文章:

javascript - php 中的分页。 AJAX、PHP、MYSQL

html - 将内联 img 元素更改为像 block 元素一样

html - CSS 列表样式类型不起作用

javascript - 表单提交处理程序未被调用

mysql - 无法使用flask-sqlalchemy连接到aws RDS上的MySQL

php - (1/1) AbstractProvider.php 中的 InvalidStateException(第 209 行)

php - 如果表不存在则创建表,否则修改表

php - 将一列中的页面分组

php - 使用 mysql 中的连接选择最近添加的数据

php - Gearman - 客户