如何解决sql错误“第1行的产品组合已经多样化,涵盖了非常成功的多品牌”

时间:2018-08-15 05:59:37

标签: mysql

我是mysql和php的新手。一周前,此代码运行完美,现在我正在尝试显示此错误消息

  

错误:您的SQL语法有错误;检查手册   对应于您的MariaDB服务器版本,以使用正确的语法   near的产品组合已经多元化,涵盖了高度   第1行获得成功的多品牌”

我花了整整一天的时间寻找解决方法,但无法解决。 我在这里在stackoverflow中尝试过类似的问题,但是我一直被困在这里。 一个帮助将非常令人钦佩 下面给出的是我的代码

<?php
    if(isset($_POST['upload']))
{       $company_name =$_POST['company_name'];
        $service =$_POST['service'];
        $email =$_POST['email'];
        $password =$_POST['password'];
        $details =$_POST['details'];

        $fileName = $_FILES['Filename']['name'];
        $fileName1 = $_FILES['Filename1']['name'];
        $fileName2 = $_FILES['Filename2']['name'];
        $fileName3 = $_FILES['Filename3']['name'];
        $fileName4 = $_FILES['Filename4']['name'];


        $target = "company_images/";                
        $fileTarget = $target.$fileName;    
        $fileTarget1 = $target.$fileName1;  
        $fileTarget2 = $target.$fileName2;  
        $fileTarget3 = $target.$fileName3;
        $fileTarget4 = $target.$fileName4;  
        $tempFileName = $_FILES["Filename"]["tmp_name"];
        $tempFileName1 = $_FILES["Filename1"]["tmp_name"];
        $tempFileName2 = $_FILES["Filename2"]["tmp_name"];
        $tempFileName3 = $_FILES["Filename3"]["tmp_name"];
        $tempFileName4 = $_FILES["Filename4"]["tmp_name"];
        $result = move_uploaded_file($tempFileName,$fileTarget);
        $result1 = move_uploaded_file($tempFileName1,$fileTarget1);
        $result2 = move_uploaded_file($tempFileName2,$fileTarget2);
        $result3 = move_uploaded_file($tempFileName3,$fileTarget3);
        $result4 = move_uploaded_file($tempFileName4,$fileTarget4);


        $file = rand(1000,100000)."-".$_FILES['file']['name'];
        $file_loc = $_FILES['file']['tmp_name'];
        $file_size = $_FILES['file']['size'];
        $file_type = $_FILES['file']['type'];
        $folder="pdf_uploads/";

        // new file size in KB
        $new_size = $file_size/1024;  
        // new file size in KB

        // make file name in lower case
        $new_file_name = strtolower($file);
        // make file name in lower case

        $final_file=str_replace(' ','-',$new_file_name);//anthima
        if(move_uploaded_file($file_loc,$folder.$final_file))
        {

            $query = "INSERT INTO company_details( company_name,service, email, password, details,image_path,file_name,image_path1,file_name1,image_path2,file_name2,image_path3,file_name3,file,type,size,image_path4,file_name4) VALUES ('$company_name','$service','$email','$password','$details','$fileTarget','$fileName','$fileTarget1','$fileName1','$fileTarget2','$fileName2','$fileTarget3','$fileName3','$final_file','$file_type','$new_size','$fileTarget4','$fileName4')";
            $con->query($query) or die("Error : ".mysqli_error($con));          


            mysqli_close($con);
}

}   
?>

<?php

以下是测试数据错误

VALUES ('singer','Hardware','singer@gmail.com','singer','Singer has been in Sr' at line 1  

1 个答案:

答案 0 :(得分:1)

因为您从不清理任何东西并将数据直接放入查询中,

    $company_name =$_POST['company_name'];
    $service =$_POST['service'];
    $email =$_POST['email'];
    $password =$_POST['password'];
    $details =$_POST['details'];

...

$query = "INSERT INTO 
    company_details( company_name,service, email, password, details,image_path,file_name,image_path1,file_name1,image_path2,file_name2,image_path3,file_name3,file,type,size,image_path4,file_name4) 
    VALUES (
    '$company_name','$service','$email','$password','$details','$fileTarget','$fileName','$fileTarget1','$fileName1','$fileTarget2','$fileName2','$fileTarget3','$fileName3','$final_file','$file_type','$new_size','$fileTarget4','$fileName4'
    )";

您的问题最有可能在数据中

  

的产品组合已经多元化,涵盖了非常成功的多品牌

也许您的数据中没有转义撇号,所以您有点像是在注入SQL。查询在错误中显示的字符串之前结束。

解决方案是在插入类似以下问题的特殊字符之前How do I escape only single quotes?

在您的情况下,请从详细信息开始

$details = addcslashes($_POST['details'], "'");

$details = addslashes($_POST['details']);

但是请继续为代码添加测试方案。例如。如果公司名称类似于Mc'Donaldson,会发生什么?您要为每个字段接受的字符集是什么?然后,您将知道如何验证那些字段并创建您的函数(或重用某些东西)