0

我正在使用 QueryPath 解析网站的表格,并尝试将我的结果放入 MySQL 数据库中。该表如下所示:

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
);
");

我试图使用航班号作为 ID 来保存飞机。

这就是我提取表格和回声以推动变量内容的方式。

        $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>";

没有要求回显 $flightLink,那会很长。这是变量的输出:

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

这是我的 SQL 查询:

        $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));

这是我在输入查询中收到的错误消息:

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

我见过很多其他人在用字符串输入 MySQL 时遇到了麻烦,几乎所有的人都在引号中失败了,所以我敢打赌它会是这样的。不过还是找不到。也感谢关于改进 MySQL 表的反馈,只是进入这个并且不太确定数据类型。

4

1 回答 1

1

您的INSERT ... ON DUPLICATE KEY UPDATE语法无效,因为您必须在遇到重复项时告诉您要更新哪些列

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

它应该是这样的

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)
于 2013-08-28T23:21:35.277 回答