Reputation: 101
I'm having trouble running this query
//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());
}
Upon execution I receive an error
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
I've checked to make sure the values exist in $wdlround->versionNumber to $wdlround->bluePowerPercent and they do. The connection to the db is fine as well. I've posted the db table as well, see here: https://i.sstatic.net/Xarlm.jpg
I'm not sure what I am doing wrong here!
EDIT:
Adding
// 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>";
Results in
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)
Being output
Upvotes: 0
Views: 197
Reputation: 77846
Per your post your INSERT
query looks like
INSERT INTO statsRounds(version,
time,
......
)
VALUES (
4,
2014.07.22.20.55.00, <-- Here surround the value with ''
6,
......
)
Problem is time
is a VARCHAR
column (per the posted link for DB schema) and the value for it have no quotes. you should pass the value for it like '2014.07.22.20.55.00'
. Likewise for all VARCHAR
/CHAR
column surround the value with quote.
Your INSERT
statement should look like
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)
Upvotes: 1
Reputation: 45490
You are not preparing the right way, here is how you should do it:
$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
));
Upvotes: 2