3

So i made this database table called votes where i added 2 columns: likes and dislikes. In html i have 2 buttons: like button and dislike button. When i press the like button i want to increment the likes column value by 1, but i seem to be doing it wrong.

PHP:

 if(isset($_POST['like'])) {

    $sql = 'UPDATE votes';  
    $sql .= 'SET likes = likes + 1';    
    $sql .= 'WHERE id = 1';

    // prepare
    $stmt = $pdo->prepare($sql);

    // execute
    $result = $stmt->execute();
 }

HTML:

<form action="" method="post">
<input type="submit" value="Like" name="like">
<input type="submit" value="Dislike" name="dislike"">
</form>
2
  • 5
    No space between votes and SET, and +1, and WHERE. Your expression is basically UPDATE votesSET likes = likes + 1WHERE id = 1 - you can validate this by echoing $sql. Besides, such a short query can just be put in one line. Commented May 22, 2016 at 14:17
  • where is the error output ? Commented May 22, 2016 at 14:17

1 Answer 1

4

It's a typo, you are missing spaces between each row.

$sql = 'UPDATE votes ';  
$sql .= 'SET likes = likes + 1 ';    
$sql .= 'WHERE id = 1';
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.