-2

我收到消息:

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
'12:00,25.32,1015.19,42.32,200.0,1.29,2.25,7.11,11.58,5.97,3.5,0.0,0.0,0.0,0.0,37'
at line 2

相关代码:

$con = mysql_connect("127.0.0.1", "username", "password") or die(mysql_error());
mysql_select_db("database",$con) or die (mysql_error());

//readFmi() returns an array of strings with values separated by commas
$array =  readFmi($xml);

$cols = 'date, Temperature, Pressure, Humidity, WindDirection, WindSpeedMS,
MaximumWind, WindGust, DewPoint, TotalCloudCover, LowCloudCover, MediumCloudCover,
HighCloudCover, Precipitation1h, PrecipitationAmount, RadiationLW, RadiationGlobal,
RadiationNetTopAtmLW';

foreach($array as $a){
  $a2 = mysql_real_escape_string($a);
  echo $a2."<br>";
  mysql_query("INSERT INTO forecastsFMI ($cols)
  VALUES ($a2)") or die (mysql_error());
}

mysql_close($con);

$a 打印时看起来像这样

2013-06-24 12:00,25.32,1015.19,42.32,200.0,1.29,2.25,7.11,11.58,5.97,3.5,0.0,0.0,0.0,0.0,375.85,550.09,-260.6

$cols 与在数据库中的表中找到的完全相同,我现在将它们全部设置为 varchar,同时试图找出我在哪里犯了错误。我一直在计算数据库中的字段和值,看看是否是我出错的地方,尝试更改字符串本身的格式并尝试更改和调整代码的不同部分,结果没有任何差异。什么可能导致这种情况?

谢谢 //托拜厄斯

4

1 回答 1

2

您需要在字符串中的字符串/日期列值周围添加引号。因此,在您的情况下,您特别需要类似的东西。

"2013-06-24 12:00",25.32,1015.19,42.32,200.0,1.29,2.25,7.11,11.58,5.97,3.5,0.0,0.0,0.0,0.0,375.85,550.09,-260.6
于 2013-06-24T12:44:06.993 回答