duandingyou3331 2013-08-28 22:58
浏览 42
已采纳

MySQL语法错误将php字符串插入表中

I'm parsing a website's table with QueryPath and trying to put my results into a MySQL database. The table looks like this:

mysql_query("CREATE TABLE Airplanes (
    flightID VARCHAR( 50 ) PRIMARY KEY NOT NULL, 
    flightLink TEXT( 20000 ) NOT NULL,
    orig TEXT( 20 )  NOT  NULL,
    dest VARCHAR( 20 )  NOT  NULL ,
    time VARCHAR( 5 )  NOT  NULL
);
");

I was trying to save airplanes using their flight numbers as IDs.

This is how I extract the table and the echos for shoving the variables' contents.

        $flightData = $row->find('td');
        // $flightID = str_replace(" ", "", $flightData->eq(1)->text());
        $flightID = mysql_real_escape_string( trim( $flightData->eq(1)->text() ) );
        $flightLink = mysql_real_escape_string( $flightData->eq(1)->html() );
        $orig = mysql_real_escape_string( "ROME (FCO)" );
        $dest = mysql_real_escape_string( trim( $flightData->eq(2)->text() ) );
        $time = mysql_real_escape_string( trim( $flightData->eq(4)->text() ) );

        echo '$flightID: ';
        echo var_dump($flightID)."<br>";
        echo '$orig: ';
        echo var_dump($orig)."<br>";
        echo '$dest: ';
        echo var_dump($dest)."<br>";
        echo '$time: ';
        echo var_dump($time)."<br>";

Didn't ask to echo $flightLink, that would have been pretty long. This is the output on the variables:

$flightID: string(7) "JN 5215"
$orig: string(10) "ROME (FCO)"
$dest: string(14) "TEL AVIV (TLV)"
$time: string(5) "23:45" 

This is my SQL-query:

        $insertQuery = mysql_query("INSERT INTO Airplanes (flightID, flightLink, orig, dest, time) VALUES( '$flightID', '$flightLink', '$orig', '$dest', '$time' ) ON DUPLICATE KEY UPDATE;");
        if($insertQuery == false) die("Problem inserting flight data into table. ".mysql_error($connection));

And this is the error message I get on the input query:

Problem inserting flight data into table. 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 '' at line 1

I've seen loads of other guys having trouble feeding MySQL with strings, almost all of them failed on quotation marks, so I bet it's gonna be something about that. Still couldn't find it though. Also grateful for feedback on improving the MySQL-table, just getting into this and not too sure about the data types.

  • 写回答

1条回答 默认 最新

  • dougu1990 2013-08-28 23:21
    关注

    Your INSERT ... ON DUPLICATE KEY UPDATE syntax is invalid because you have to tell what columns you want to update when a duplicate is encountered

    INSERT INTO Airplanes (flightID, flightLink, orig, dest, time) 
    VALUES( '$flightID', '$flightLink', '$orig', '$dest', '$time' ) 
    ON DUPLICATE KEY UPDATE
                           ^^^^ missing part of ON DUPLICATE clause
    

    It should be something like

    INSERT INTO Airplanes (flightID, flightLink, orig, dest, time) 
    VALUES( '$flightID', '$flightLink', '$orig', '$dest', '$time' ) 
    ON DUPLICATE KEY UPDATE flightLink = VALUES(flightLink), 
                                  orig = VALUES(orig),
                                  dest = VALUES(dest),
                                  time = VALUES(time)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据