0

您好,我遇到了一些问题,我必须加载具有 2000 多个属性的 xml 文件,在这里您可以找到文件的结构

http://admin.resales-online.com/live/Resales/Export/CreateXMLFeed.asp?U=RESALES@MOVE2S&P=KKPDRT6986NG&n=1

我正在使用休闲代码

<html>
<head>
<title>Insert Record</title>
</head>
<body>
<?php

ini_set('display_errors', 1);
ini_set('log_errors', 1);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');
error_reporting(-1);

include ('config.php');

$url = "http://admin.resales-online.com/live/Resales/Export/XMLFeed.asp?U=RESALES@MOVE2S&P=KKPDRT6986NG&n=100";

try{
  $xml = new SimpleXMLElement($url, null, true);
}catch(Exception $e){
  echo $e->getMessage();
  exit;
}

$sql = 'INSERT INTO properties (`id`,`status_date`,`status`,`listed_date`,`last_updated`,`price`,`currency`,`country`,`province`) VALUES ';

foreach($xml->property as $property){
  $sql .= sprintf("\n",
    mysql_real_escape_string($property->id),
    mysql_real_escape_string($property->status_date),
    mysql_real_escape_string($property->listed_date),
    mysql_real_escape_string($property->last_updated),
    mysql_real_escape_string($property->price),
    mysql_real_escape_string($property->currency),
    mysql_real_escape_string($property->country),
    mysql_real_escape_string($property->province)
  );
}

$sql = rtrim($sql, ',') . ';';

if(!mysql_query($sql)){
  echo '<h1 style="color: red;">Error</h1><p>', mysql_error(), '</p>';
}

?>
</body>
</html>

我得到了这个错误

错误

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 '' 附近使用正确的语法

如果有人知道可能是什么问题,请在这里回答我:)

谢谢

4

2 回答 2

0

查询中的每个值都缺少单引号,并且您缺少左括号和右括号。INSERT 的正确语法是:

INSERT INTO `table` (`field1`, `field2`, `field3`) VALUES ('value1', 'value2', 'value3')

你的看起来更像:

INSERT INTO `table` (`field1`, `field2`, `field3`) VALUES value1, value2, value3

最简单的调试方法是回显你的$sql变量,这样你就可以看到它的样子,这样你就可以快速发现明显的错误。

于 2013-06-05T16:42:50.183 回答
0

这是 INSERT 的格式:

INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);

VALUES 的左括号和右括号在哪里?

于 2013-06-05T16:41:15.560 回答