简体   繁体   English

mysqli_query返回false,因为值包含空格字符

[英]mysqli_query returns false because values contain space charcter

I am inserting data from a excel sheet but i receive error and it looks like it is breaking because the value contain a space character in between. 我正在从excel工作表中插入数据,但是我收到错误消息,因为值之间包含空格字符,因此似乎已损坏。 As far as i remember space characters allowed in VARCHAR(200) 据我记得, VARCHAR(200)允许使用空格字符

This is the code i am using 这是我正在使用的代码

//CREATE SQL QUERY FOR INSERTING DATA IN DATABASE
    $sql = "INSERT INTO ".$month."_".$year."(";
    foreach($sheetData[1] as $columnName){
        $sql .= preg_replace('#[ ]#', '_',$columnName). ",";
    }
    $sql = rtrim($sql, ',');//REMOVES COMMA FROM END OF THE STRING
    $sql .= ")";
    //
    $sql .= " VALUES((";
    for($i=2;$i < count($sheetData);$i++){
        foreach($sheetData[$i] as $columnName){
            $sql .= $columnName.",";
        }
        $sql = rtrim($sql,',');//
        $sql .= "),";
    }
    $sql = rtrim($sql,',');//
    $sql .= ")";
    echo $sql;
    $query = mysqli_query($conn,$sql) or die(mysqli_error($conn));

After loops this is how my SQL QUERY look After循环是我的SQL QUERY外观

INSERT INTO December_2015(S_No,Zone,State,City2,VM_Town,Distibutor_Code,Distributor_Name,Dealer_Code,Dealer_Name,Category,Address,Location,Contact,Mobile_No,Visit_1,Visit_2,Visit_3,Visit_4,Visit_5,Visit_6) VALUES( (1,South,Telanagana,Hyderabad,Y,1006704,Sai Santhoshi Enterprises,TG000999,Sree Laxmi Mobiles,A,F4,anthem Arcade,gujarathi Galli,koti ,Koti,Rajesh,8790575680,7-Nov,18-Nov,28-Nov))

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 'Santhoshi Enterprises,TG000999,Sree Laxmi Mobiles,A,F4,anthem Arcade,gujarathi G' at line 1 在第1行的“ Santhoshi Enterprises,TG000999,Sree Laxmi Mobiles,A,F4,anthem Arcade,gujarathi G”附近,查看与MySQL服务器版本相对应的手册以使用正确的语法。

It says near 'Santhoshi Enterprises ... ' before that there is a space character 它说在'Santhoshi Enterprises ... '附近,之前有一个空格字符

您有两个“(”,而不是“ VALUES”之后的一个

Akash , 阿卡什
Didn't you asked a question just a while ago regarding same/similar code with a different error you got, here at: How to loop inside a variable ?! 您不是刚刚才问过一个关于您遇到错误的相同/相似代码的问题,这里是: 如何在变量内部循环

By the looks of it in general you write messy code, and you are having trouble reading/understanding the error messages. 从外观上看,您通常会编写混乱的代码,并且在阅读/理解错误消息时遇到了麻烦。 So I'm gonna guess you are new at this. 所以我想你是新来的。

Here are some good reads for you: 以下是一些适合您的读物:

When all said and done, here is your code broken down into more readable segments: 综上所述,这是将您的代码分解为更易读的段:

// prepare dummy data
$month = date('M');
$year = date('Y');
$sheetData = array(
    array('data00', 'data01')
    ,array('col1', 'col2', 'col3', 'col4', 'col5', 'col6')
    ,array('data20', "data21")
    ,array('data30', 'data31')
    ,array('data40', 'data41')
);

// prepare vars
$tableName = "{$month}_{$year}";
$dataCount = count($sheetData);

// prepare columns
$columnsSQL = "";
foreach ($sheetData[1] as $columnName) {
    // wrap with ` ticks
    $columnsSQL .= '`'. preg_replace('#[ ]#', '_', $columnName).'`'.',';
}
$columnsSQL = rtrim($columnsSQL, ',');

// prepare values
$valuesSQL = "";
for ($i=2;$i < $dataCount;$i++) {
    foreach($sheetData[$i] as $columnValue){
        $valuesSQL .= "'{$columnValue}', ";
    }
}
$valuesSQL = rtrim($valuesSQL, ', ');

$SQL = "
INSERT INTO {$tableName}( {$columnsSQL} )
VALUES ( {$valuesSQL} )";

At the end you end up with something like this: 最后,您将得到如下结果:

INSERT INTO Nov_2015( `col1`,`col2`,`col3`,`col4`,`col5`,`col6` )
VALUES ( 'data20', 'data21', 'data30', 'data31', 'data40', 'data41' )

Additional note and tips: 附加说明和提示:

  • Considering that you said you are reading data from excel sheet... Never trust input data without some tests/checks/validation. 考虑到您说的是您正在从excel表中读取数据...切勿在没有进行某些测试/检查/验证的情况下信任输入数据。 Not just because of security but stability and in general you don't want things breaking. 不仅仅因为安全性,还因为稳定性,总的来说,您不希望事情破裂。 Those excel tables could be manually made which automatically means its prone for human error, so you can't be always 100% sure what are you gonna get. 那些excel表可以手动创建,这自动意味着它很容易出现人为错误,因此您不能总是100%确定要得到什么。

  • Consider using PDO and prepared statements (security reasons, but also good practice) 考虑使用PDO和准备好的语句(出于安全原因,也是一种良好做法)

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

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