I have a table as so:
TABLE click_count
(
count int(3)
);
which is currently an arbitrary number. I have this php script with an html button which should just increment the number by one. The SQL query works in php my admin but gets an error when it's ran on the page.
<?php
require("config.inc.php");
if(!empty($_POST)){
$query = "UPDATE click_count
SET count = count + :submit_1
";
$query_params_ = array(
'submit_1' => $_POST['count']
);
try {
// These two statements run the query against your database table.
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
$response["message"] = "Database Error. Please Try Again!";
die(json_encode($response));
}
$response["message"] = "Vote Cast!";
echo json_encode($response);
} else {
?>
<form action="vote.php" method="post">
Count:<br />
<input type="number" name="submit_1" value="1" />
<br /><br />
<input type="submit" value="Cast Vote" />
</form>
<?php
}
?>
You have to change your array key to match the one in preparedStatement. Like this:
$query_params_ = array(':submit_1' => $_POST['count']);
Indeed, you are refering to $query_params in the execute() method, but you are defining $query_params_ (with underscore in the end).
count
is a reserved keyword of mysql, see: http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html
try to enclose it into accents, like this:
UPDATE click_count
SET `count` = `count` + :submit_1
";