繁体   English   中英

SQL INSERT:PHP中的语法错误

[英]SQL INSERT: syntax error in PHP

我有以下代码段,假定是为我生成一个查询字符串以插入到我的mysql表中。 经过多次更改后,这些更改始终产生相同的错误结果,我要求提供一些帮助来诊断问题。

请看一下代码

foreach($this->csv2sqlInsertMultiLine as $csv2sqlInsertMultiLine)
            {
                $field_values = explode(',',$csv2sqlInsertMultiLine);

                $fv = 0;
                 $sqlLine = "";
                 $theInsertSQL[$fv] = "INSERT INTO {$_SESSION['WorkTable']} SET \n";
                //$theInsertSQL .= $csv2sqlInsertMultiLine;
                 foreach($this->tableFields as $Fields)
                 {
                     // $Fields.''.$field_values;
                     $Fields = mysqli_real_escape_string($MySQLDnCxn->MySQLCxn, $Fields);
                        $letters = array("(''", "'')","'");//
                        $Quotes   = array('', '');
                        $value  = str_replace($letters, $Quotes, $field_values[$fv]);

                     if($value == "'('" or $value == "')'" or $Fields == 'id' or $Fields == 'cDate') {}
                     elseif($sqlLine === "`id` = '',") { $sqlLine = " ";  }
                     else
                     {
                          $sqlLine .= "`$Fields` = '".$value."',";
                     }

                     $fv++;
                 }
                 $theInsertSQL[$fv] .= $sqlLine;
                $theInsertSQLnew = join(',', $theInsertSQL);
                $theInsertSQLrt = rtrim($theInsertSQLnew,',');


                 //EXECUTE INSERT TO MySQL $TABLE QUERY
                if ($insert = mysqli_query($MySQLDnCxn->MySQLCxn, $theInsertSQLrt))
                {
                    $show .= 'Contacts Have been successfully inserted...'; 
                } 

                else
                {
                    $show .= "<p>could not insert into db <br> &nbsp; ";
                    $show .= mysqli_error($MySQLDnCxn->MySQLCxn);
                    //.'</p><br> Columns count: <b>'.$this->fields_Column.'</b>';       
                }

无论我做什么,我都会不断收到以下错误; 无法插入数据库

您的SQL语法有误; 检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在'Title` ='','First Name` ='',`Middle Name` ='',`Last Name` ='',`附近使用第2行的后缀

那产生的查询:

INSERT INTO nfcontactsdb SET ,`Title` = ' ',`First Name` = ' ',`Middle Name` = ' ',`Last Name` = ' ',`Suffix` = ' ',`Company` = ' ',`Department` = ' ',`Job Title` = ' ',`Business Street` = ' ',`Business Street 2` = ' ',`Business Street 3` = ' ',`Business City` = ' ',`Business State` = ' ',`Business Postal Code` = ' ',`Business Country/Region` = ' ',`Home Street` = ' ',`Home Street 2` = ' ',`Home Street 3` = ' ',`Home City` = ' ',`Home State` = ' ',`Home Postal Code` = ' ',`Home Country/Region` = ' ',`Other Street` = ' ',`Other Street 2` = ' ',`Other Street 3` = ' ',`Other City` = ' ',`Other State` = ' ',`Other Postal Code` = ' ',`Other Country/Region` = ' ',`Assistant\'s Phone` = ' ',`Business Fax` = ' ',`Business Phone` = ' ',`Business Phone 2` = ' ',`Callback` = ' ',`Car Phone` = ' ',`Company Main Phone` = ' ',`Home Fax` = ' ',`Home Phone` = ' ',`Home Phone 2` = ' ',`ISDN` = ' ',`Mobile Phone` = ' ',`Other Fax` = ' ',`Other Phone` = ' ',`Pager` = ' ',`Primary Phone` = ' ',`Radio Phone` = ' ',`TTY/TDD Phone` = ' ',`Telex` = ' ',`Account` = ' ',`Anniversary` = '00-0-0',`Assistant\'s Name` = ' ',`Billing Information` = ' ',`Birthday` = '00-0-0',`Business Address PO Box` = ' ',`Categories` = ' ',`Children` = ' ',`Directory Server` = ' ',`E-mail Address` = 'electricbox@gmail.com',`E-mail Type` = 'SMTP',`E-mail Display Name` = ' electricbox@gmail.com',`E-mail 2 Address` = ' ',`E-mail 2 Type` = ' ',`E-mail 2 Display Name` = ' ',`E-mail 3 Address` = ' ',`E-mail 3 Type` = ' ',`E-mail 3 Display Name` = ' ',`Gender` = 'Unspecified',`Government ID Number` = ' ',`Hobby` = ' ',`Home Address PO Box` = ' ',`Initials` = ' ',`Internet Free Busy` = ' ',`Keywords` = ' ',`Language` = ' ',`Location` = ' ',`Manager\'s Name` = ' ',`Mileage` = ' ',`Notes` = ' ',`Office Location` = '',`Organizational ID Number` = '',`Other Address PO Box` = '',`Priority` = '',`Private` = '',`Profession` = '',`Referred By` = '',`Sensitivity` = '',`Spouse` = '',`User 1` = '',`User 2` = '',`User 3` = '',`User 4` = '',`Web Page` = ''

在标题前删除逗号

INSERT INTO nfcontactsdb SET `Title` = ' '...

因此,在您的PHP更改行中:

$theInsertSQLnew = join(',', $theInsertSQL);

$theInsertSQLnew = join(' ', $theInsertSQL);

似乎在此处添加了逗号,因此我们只是在开始时不添加它。

还有一个,之间SET和在SQL语句中的第一个名称/值对。 这是无效的SQL语法

我认为您的问题已经迫在眉睫

$theInsertSQLnew = join(',', $theInsertSQL);

这将插入,其中\\n是你的语句之前。 我建议使用

$theInsertSQLnew = join('', $theInsertSQL);

请尝试以下操作:

更改此行:

$theInsertSQL[$fv] = "INSERT INTO {$_SESSION['WorkTable']} SET \n";

附:

$_prequery = "INSERT INTO {$_SESSION['WorkTable']} SET \n";

然后更改以下行:

$theInsertSQL[$fv] .= $sqlLine;
$theInsertSQLnew = join(',', $theInsertSQL);
$theInsertSQLrt = rtrim($theInsertSQLnew,',');

$theInsertSQL[$fv] .= $sqlLine;
$theInsertSQLnew = join(',', $theInsertSQL);
$theInsertSQLrt = $_prequery.$theInsertSQLnew;

暂无
暂无

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

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