运行这个查询有问题
//Insert power stats
try {
$STH = $db -> prepare(
"INSERT INTO statsRounds
(
version,
timeFormat,
mapNumber,
mapName,
duration,
startTic,
endTic,
avgEfficiencyPts,
redPowerPercent,
bluePowerPercent
)
VALUES
(
$wdlround->versionNumber,
$wdlround->timeFormat,
$wdlround->mapNumber,
$wdlround->mapName,
$wdlround->durationTics,
$wdlround->startTic,
$wdlround->endTic,
$wdlround->averageEfficiencyPoints,
$wdlround->redPowerPercent,
$wdlround->bluePowerPercent
)"
);
//Execute query
$STH -> execute();
} //try
catch(PDOException $ex) {
die("Failed to run query: " . $ex->getMessage());
}
在执行时,我收到一个错误
Failed to run query: SQLSTATE[42000]: Syntax error or access violation: 1064 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 '.22.20.55.00, 6, Lazarus Revisited - SMARTCTF01, 18193, 0, ' at line 17
我已经检查了,以确保在$wdlround->versionNumber到$wdlround->bluePowerPercent的值存在,他们做。与数据库的连接也很好。我也发布了db表,见这里:https://i.stack.imgur.com/Xarlm.jpg
我不知道我做错了什么!
编辑:添加// ECHO QUERY
$mystr = "INSERT INTO statsRounds( 'version', 'time', 'mapNumber', 'mapName', 'duration', 'startTic', 'endTic', 'avgEfficiencyPts', 'redPowerPercent', 'bluePowerPercent') VALUES ( " . $wdlround->versionNumber . ", " . $wdlround->timeFormat . ", " . $wdlround->mapNumber . ", " . $wdlround->mapName . ", " . $wdlround->durationTics . ", " . $wdlround->startTic . ", " . $wdlround->endTic . ", " . $wdlround->averageEfficiencyPoints . ", " . $wdlround->redPowerPercent . ", " . $wdlround->bluePowerPercent . ") ";
echo "MYSTR = $mystr<br>";
结果
MYSTR = INSERT INTO statsRounds( 'version', 'time', 'mapNumber', 'mapName', 'duration', 'startTic', 'endTic', 'avgEfficiencyPts', 'redPowerPercent', 'bluePowerPercent') VALUES ( 4, 2014.07.22.21.03.11, 6, Lazarus Revisited - SMARTCTF01, 16418, 0, 130136, 107.83333333333, 108.33333333333, 93.333333333333)
被输出你没有准备好正确的方法,你应该这样做:
$STH = $db -> prepare(
"INSERT INTO statsRounds
(
version,
timeFormat,
mapNumber,
mapName,
duration,
startTic,
endTic,
avgEfficiencyPts,
redPowerPercent,
bluePowerPercent
)
VALUES
(
?,
?,
?,
?,
?,
?,
?,
?,
?,
?
)"
);
//Execute query
$STH -> execute(array(
$wdlround->versionNumber,
$wdlround->timeFormat,
$wdlround->mapNumber,
$wdlround->mapName,
$wdlround->durationTics,
$wdlround->startTic,
$wdlround->endTic,
$wdlround->averageEfficiencyPoints,
$wdlround->redPowerPercent,
$wdlround->bluePowerPercent
));
根据您的帖子,您的INSERT
查询看起来像
INSERT INTO statsRounds(version,
time,
......
)
VALUES (
4,
2014.07.22.20.55.00, <-- Here surround the value with ''
6,
......
)
问题是time
是VARCHAR
列(根据DB模式发布的链接),它的值没有引号。您应该传递它的值,如'2014.07.22.20.55.00'
。同样,对于所有VARCHAR
/CHAR
列,用引号将值括起来。
您的INSERT
语句应该看起来像
INSERT INTO statsRounds(
version,
time,
mapNumber,
mapName,
durstion,
startTic,
endTic,
avgEfficiencyPts,
redPowerPercent,
bluePowerPercent)
VALUES (
'4',
'2014.07.22.20.55.00',
6,
'Lazarus Revisited - SMARTCTF01',
18193,
0,
112948,
103.66666666667,
150,
80)