dozoqn3347 2018-08-15 00:38
浏览 12

为什么我收到MySQl错误

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.

The query that is producing this error is the query that is in the for loop with numberofcolor and numberofsizes

Error description: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 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条回答 默认 最新

  • doutuanxiao4619 2018-08-15 00:42
    关注
    $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?

    评论

报告相同问题?

悬赏问题

  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False