开发者

Getting a mySQL error in php generated query

Let me try this again, because I'm not sure people got that I was running this query with php. The query that the php function outputs runs fine in a regular mySQL program like Navicat but it throws this error, in my browser when it ran from the php script:

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 'DROP TABLE IF EXISTS checklisttest; CREATE TABLE checklisttest ( Incident va' at line 2

Here is some of the query that is ran from within the php script:

    SET FOREIGN_KEY_CHECKS=0;
DROP TABLE IF EXISTS checklisttest;
CREATE TABLE checklisttest (
  Incident varchar(12) NOT NULL,
  TestID mediumint(9) NOT NULL AUTO_INCREMENT,
  Element varchar(12) NOT NULL,
  Name varchar(128) NOT NULL,
  Code varchar(512) NOT NULL,
  Expected varchar(512) NOT NULL,
  Actual varchar(512) NOT NULL,
  AutoVerifyResult varchar(32) NOT NULL,
  QAResult varchar(32) DEFAULT NULL,
  Comments text,
  PRIMARY KEY (TestID)
);

INSERT INTO `checklistTest` VALUES ('20009',NULL,'E02_04','Type of Service Requested','30','911 Response (Scene)','911 RESPONSE (SCENE)','100','Pass',NULL);
INSERT INTO `checklistTest` VALUES ('20009',NULL,'E02_11','EMS Unit/Vehicle  Number','Medic 81','Medic 81','Medic 81','100','Pass',NULL);
INSERT INTO `checklistTest` VALUES ('20009',NULL,'E02_18','Patient Destination  Odometer Reading  of Responding  Vehicle','11','11','Not set!','0','Fail',NULL);
INSERT INTO `checklistTest` VALUES ('20009',NULL,'E03_01','Complaint Reported  by Dispatch','410','Animal Bite','ANIMAL BITE','100','Pass',NULL);
INSERT INTO `checklistTest` VALUES ('20009',NULL,'E04_01','Crew Member ID','EMT302875','EMT302875','EMT302875','100','Pass',NULL);
INSERT INTO `checklistTest` VALUES ('20009',NULL,'E04_01','Crew Member ID','PMD509465','PMD509465','PMD509465','100','Pass',NULL);

Here is the php that generates that query:

        private function toDB(){
        $sql = "
SET FOREIGN_KEY_CHECKS=0;
DROP TABLE IF EXISTS checklisttest;
CREATE TABLE checklisttest (
  Incident varchar(12) NOT NULL,
  TestID mediumint(9) NOT NULL AUTO_INCREMENT,
  Element varchar(12) NOT NULL,
  Name varchar(128) NOT NULL,
  Code varchar(512) NOT NULL,
  Expected varchar(512) NOT NULL,
  Actual varchar(512) NOT NULL,
  AutoVerifyResult varchar(32) NOT NULL,
  QAResult varchar(32) DEFAULT NULL,
  Comments text,
  PRIMARY KEY (TestID)
);
              ";

    //iterate through the records $this->records[10001]
    foreach($this->records as $inc => $record){
        //iterate through the element ids $this->records[10001][E02_04]
        foreach($this->records[$inc]["Elements"] as $elementID => $element){
            //iterate through the element ids $this->records[10001][E02_04][1]
            foreach($element as $key => $val){
                $sql .= "
INSERT INTO `checklistTest` VALUES (\"$inc\",NULL,\"$elementID\",\"$val[name]\",\"$val[code]\",\"$val[expected]\",\"$val[actual]\",\"$val[matc开发者_Go百科h]\",\"$val[QAResult]\",NULL);";
            }
        }
    }
    echo "<textarea style='width:100%;height:400px'>$sql</textarea>";
    mysql_select_db("new",$GLOBALS['local']);
    mysql_query($sql,$GLOBALS['local']) or die(mysql_error());
}


A few errors:

  • You have a type called varchar but this is not allowed. You must specify a length, e.g. varchar(128).
  • The column Actual shouldn't be AUTO_INCREMENT because it is not part of a key, and you already have an auto-increment field.
  • Instead of ('20009',,'E02_04',... you probably mean ('20009',NULL,'E02_04',... (note the extra NULL).


You must specify length for each varchar field. Moreover you have two auto_increment fields

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜