drcx71276 2013-06-24 12:40
浏览 33
已采纳

mysql数据库SQL语法错误php

I get the message:

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

relevant code:

$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 when printed looks like this

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 are the exact same ones as found in the table in the database, I set them all to be varchar for now, while trying to figure out where i have made my mistake(s). I have been counting the fields in the db and values to see if that is where i have gone wrong, tried to change the format of the string itself and tried to change and tweak different parts of the code, without any difference in result. What might cause this?

Thank you //Tobias

  • 写回答

1条回答 默认 最新

  • doubo7131 2013-06-24 12:44
    关注

    You need to add quotes around string/date column values in string. So in you case specially you need something like.

    "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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用C语言输入方程怎么
  • ¥15 网站显示不安全连接问题
  • ¥15 github训练的模型参数无法下载
  • ¥15 51单片机显示器问题
  • ¥20 关于#qt#的问题:Qt代码的移植问题
  • ¥50 求图像处理的matlab方案
  • ¥50 winform中使用edge的Kiosk模式
  • ¥15 关于#python#的问题:功能监听网页
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题