填充数据不插入mysql数据库

时间:2016-07-13 11:18:41

标签: php html mysql forms

我是php新手。提交表单时,数据不存储在数据库中而不会导致代码出错。以下是我的代码

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

<table width="700" align="center" >
    <tr>
        <td><h1>Insert New Product</h1></td>

    </tr>
    <tr>
        <td><b>Product Title</b></td>
        <td><input type="text" name="product_title"></td>           
    </tr>
    <tr>
            <td><b>Product Category</b></td>
        <td>
            <select name="product_cat">
                <option>Select Category</option>
                <?php

              $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><b>Product Brand</b></td>
        <td>
         <select name="product_brand">
            <option>Select Brand</option>
              <?php 
                $get_brands = "select * from brands";
                $run_brands = mysqli_query($con , $get_brands);
                while ($row_brands = mysqli_fetch_array($run_brands)) {

                        $brand_id = $row_brands['brand_id'];
                        $brand_title = $row_brands['brand_title'];

                        echo "<option value='$brand_id'>$brand_title</option>";

                }


            ?>
            </select>
        </td>           
    </tr>
    <tr>    <td><b>Product Image1</b></td>
        <td><input type="file" name="product_img1"></td>            
    </tr>
    <tr><td><b>Product Image2</b></td>
        <td><input type="file" name="product_img2"></td>            
    </tr>
    <tr><td><b>Product Image3</b></td>
        <td><input type="file" name="product_img3"></td>            
    </tr>
    <tr>
    <td><b>Product Price</b></td>
        <td><input type="text" name="product_price"></td>           
    </tr>
    <tr>
        <td><b>Product Info</b></td>
        <td> <textarea name="product_desc">Easy! You should check out MoxieManager!</textarea></td>         
    </tr>
    <tr>
        <td><b>Product Keyword</b></td>
        <td><input type="text" name="product_keyword"></td>         
    </tr>
    <tr>
        <td><input type="submit" name="insert_product" value="Insert"></td>         
    </tr>
</table>

 </form>

和它的PHP代码

    <?php 

if (isset($_POST['insert_product'])) {

    $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'];
    $status = 'on';
    $product_keyword = $_POST['product_keyword'];

    //image names//
    $product_img1 = $_FILES['product_img1']['name'];
    $product_img2 = $_FILES['product_img2']['name'];
    $product_img3 = $_FILES['product_img3']['name'];
    //temp image names
    $temp_name1 = $_FILES['product_img1']['tmp_name'];
    $temp_name2 = $_FILES['product_img2']['tmp_name'];
    $temp_name3 = $_FILES['product_img3']['tmp_name'];


    if ($product_title=='' OR $product_cat=='' OR $product_brand=='' OR $product_price=='' OR $product_desc=='' OR $product_keyword=='' OR $product_img1=='' ) {

        echo "<script>alert('Please Fill Everything')</script>";
        exit();
    }

    else {

        //uploading images to its folder
        move_uploaded_file($temp_name1, "product_images/$product_img1");
        move_uploaded_file($temp_name2, "product_images/$product_img2");
        move_uploaded_file($temp_name3, "product_images/$product_img3");

        $insert_product = "insert into products (cat_id,brand_id,date,product_title,product_img1,product_img2,product_img3,product_price,product_desc,status)

         values ('$product_cat','$product_brand',NOW(),'$product_title','$product_img1','$product_img2','$product_img3','$product_price','$product_desc','$status')";

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

         if ($run_product) {

            echo "<script>alert('Product Inserted')</script>";
         }

    }



}

?>

直到alert(fill everything)正在工作意味着当我点击提交而不填写表格时警报即将到来,但之后我填写所有内容并提交。但是警报没有到来,数据也没有保存在数据库中。请帮我。添加我的数据库结构也。感谢

enter image description here

0 个答案:

没有答案