简体   繁体   English

为什么我收到 MySQl 错误

[英]why am I getting a MySQl error

I am getting this error when I run the below code.运行以下代码时出现此错误。 I dont know why I am getting this error.我不知道为什么会收到此错误。 I echoed the query to make sure that the syntax is correct I checked the query in a syntax checker to make sure that the query contains no syntax errors.我回显了查询以确保语法正确我在语法检查器中检查了查询以确保查询不包含语法错误。 and even tested in PHP my admin.甚至在我的管理员 PHP 中进行了测试。

The query that is producing this error is the query that is in the for loop with numberofcolor and numberofsizes产生此错误的查询是在 for 循环中使用 numberofcolor 和 numberofsizes 的查询

Error description: You have an error in your SQL syntax;错误描述:你的SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1检查与您的 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>
$add_size_att = "INSERT INTO `attributes`( `id`, `att_type`, `att_value`) VALUES ($product_id, `size`,`$curr_size_value`)";

your using the incorrect quote type here the ` should be ' around the string values:您在此处使用了不正确的引号类型,字符串值周围的 ` 应该是 ':

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

also should size be $size ?大小也应该是$size吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM