为什么我会收到MySQl错误

时间:2018-08-15 00:38:13

标签: php mysqli

运行以下代码时出现此错误。我不知道为什么我得到这个错误。我回显了查询以确保语法正确。我在语法检查器中检查了查询,以确保查询不包含语法错误。甚至在我的管理员的PHP中进行了测试。

产生此错误的查询是for循环中具有numberofcolor和numberofsizes的查询

错误描述:您的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在第1行的'1'附近使用

<?php include('includes/db.php'); include('../functions/functions.php');?>
<?php require './p_struct/head.php';?>
<?php require './p_struct/header.php';?>


<?php require './p_struct/sidenav.php';?>
<div class="main" style="font-size: 20px">

    <form action="insert_product.php" method="post" enctype="multipart/form-data" class="form-horizontal" id="chells">
    <table>
            <tr>
                <td>Name</td>
                <td><input type="text" name="product_title" required="required"></td>

            </tr>

            <tr>
                <td>category</td>
                <td><select name="product_category" >
                <option>Select a 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_data']; 


                        echo "

                        <option vallue = '$cat_id' >$cat_title </option>

                        ";
                        }
                ?>


                </select></td>

            </tr>


            <tr>
                <td>featured</td>
                <td><select name="product_featured" >
                <option>featured</option>
                <option>not featured</option>
                </select></td>

            </tr>

            <tr>
                <td>price</td>
                <td><input type="text" name="product_price" required="required"></td>

            </tr>

            <tr>
                <td>image</td>
                <td><input type="file" name="product_image" required="required"></td>

            </tr>

                        <tr>
                <td>big image</td>
                <td><input type="file" name="product_big_image" required="required"></td>

            </tr>

                          <tr>
                <td>cart image</td>
                <td><input type="file" name="product_cart_image" required="required"></td>

            </tr>

            <tr>
                <td>keywords</td>
                <td><input type="text" name="product_keywords" required="required"></td>

            </tr>

                        <tr>
                        <td> Colors</td>
                        <td>
                            <div class="checkbox">
                             <?php

                                $get_colors = "SELECT * FROM `product_colors`";
                                $run_get_colors = mysqli_query($con, $get_colors);

                                while($row_get_color = mysqli_fetch_array($run_get_colors))
                                {
                                    $colorS = $row_get_color['color'];
                                    echo '<label class="checkbox-inline" ><input type="checkbox" name = "mycolor[]" value="'.$colorS.'" >'.$colorS.'</label>';  
                                }
                            ?>
                            </div>
                        </td>
                        </tr>

                             <tr>
                        <td> Sizes</td>
                        <td>
                            <div class="checkbox">
                             <?php

                                $get_sizes = "SELECT * FROM `product_sizes`";
                                $run_get_sizes = mysqli_query($con, $get_sizes);

                                while($row_get_sizes = mysqli_fetch_array($run_get_sizes))
                                {
                                    $sizesS = $row_get_sizes['size'];
                                    echo '<label class="checkbox-inline" ><input type="checkbox" name = "mysizes[]" value="'.$sizesS.'" >'.$sizesS.'</label>';  

                                }
                            ?>
                            </div>
                        </td>
                        </tr>

            <tr>
                <td>description</td>
                <td><textarea name="product_description" cols="20" rows="5" ></textarea></td>

            </tr>

            <tr>
                <td><input type="submit" value="Add Product" name="insert_post"></td>
            </tr>

    </table>
</form>


</body>

</html>

<?php 
    global $con;

        if( isset($_POST['insert_post']))
        {
                        $a = rand(100, 999);
                        $b = rand(100, 999);
            $product_id = $a.$b;    

            $product_title = $_POST['product_title'];
            $product_category = $_POST['product_category'];
            $product_featured = $_POST['product_featured'];
            $product_price = $_POST['product_price'];
            $product_keywords = $_POST['product_keywords'];
            $product_description = $_POST['product_description'];
                        $color_array = $_POST['mycolor'];
                        $sizes_array = $_POST['mysizes'];
            $product_image = $_FILES['product_image']['name'];
                        $product_big_image = $_FILES['product_big_image']['name'];
                        $product_cart_image = $_FILES['product_cart_image']['name'];
            $product_image_tmp = $_FILES['product_image']['tmp_name'];
                        $product_big_image_tmp = $_FILES['product_big_image']['tmp_name'];
                        $product_cart_image_tmp = $_FILES['product_cart_image']['tmp_name'];

            move_uploaded_file($product_image_tmp,"product_images/$product_image");
                        move_uploaded_file($product__big_image_tmp,"product_big_images/$product_big_image");
                        move_uploaded_file($product_cart__image_tmp,"product_cart_images/$product_cart_image");

         $insert_product = " insert into products (product_id,product_cat, product_featured, product_title, product_price, product_desc,product_image,product_big_image,product_cart_image,product_keywords) values ($product_id,'$product_category','$product_featured','$product_title','$product_price','$product_description','$product_image','$product_big_image','$product_cart_image','$product_keywords')";

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

                $number_of_colors = count($color_array);

                for($i=0; $i <$number_of_colors; $i++)
                {
                    $curr_color_value = $color_array[$i];
                    $add_color_att = "INSERT INTO `attributes`( `id`, `att_type`, `att_value`) VALUES ($product_id, 'color','$curr_color_value');";
                    $run_add_color_att = mysqli_query($con, $add_color_att);

                    /*       if($run_add_color_att)
                    {
                       echo "COLOR ADDED!";
                    }

                     else {
                           echo "COLOR NOT ADDED!";
                     }*/

                }

                $number_of_sizes = count($sizes_array);
                for($s=0; $s <$number_of_sizes; $s++)
                {
                    $curr_size_value = $number_of_sizes[$s];
                    $add_size_att = "INSERT INTO `attributes`( `id`, `att_type`, `att_value`) VALUES ($product_id, `size`,`$curr_size_value`)";
                    $run_add_size_att = mysqli_query($con, $add_size_att);

                    /*        if($run_add_size_att)
                    {
                       echo "SIZE ADDED!";
                    }

                     else {
                           echo "SIZE NOT ADDED!";
                     }*/
                }

                if($run_insert_query)
                {
                   echo "PRODUCT ADDED!";
                }

                 else {
                       echo "PRODUCT NOT ADDED!";
                 }




    } ?>

</div>

1 个答案:

答案 0 :(得分:0)

$add_size_att = "INSERT INTO `attributes`( `id`, `att_type`, `att_value`) VALUES ($product_id, `size`,`$curr_size_value`)";

您在此处使用不正确的引号类型时,`应该位于字符串值附近'

 $add_size_att = "INSERT INTO `attributes`( `id`, `att_type`, `att_value`) VALUES ($product_id, 'size','$curr_size_value')";

尺寸也应该为$size吗?