gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 13:38:09 24 4
gpt4 key购买 nike

我正在使用 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 表的反馈,刚刚进入这个领域,对数据类型不太确定。

最佳答案

您的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)

关于php - MySQL语法错误将php字符串插入表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18499672/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com