0

recently I was working on a simple voting system for my movie website and I´m stuck with a simple point counter. I have a button with id="point" and when you click it, it should send an ajax request with id number to PHP file, which should increase a value of score in the database (where id=number).

<button type="button" id="point">Points</button>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<script>
$(document).ready(function(){
    $("#point").click(function(){
       $.ajax({
            type: "POST",
            url: 'update.php',
            data: {id: 22}
        });
    });
});
</script>`

update.php

<?php
$num = $_POST['id']

$con = mysqli_connect("localhost", "username", "password", "movies");
$vote = "UPDATE movies SET score=score + 1 WHERE id='$num'";
$run_vote = mysqli_query($con,$vote);
?>

Unfortunately, this isn´t working and I can´t figure out why. If someone knows, please write it in the comments.

bmt24
  • 259
  • 1
  • 3
  • 6
  • First isolate the problem by finding out whether the problem is that the script is not called at all, called with wrong arguments or is called correctly but doesn't do that it is supposed to do. – RalfFriedl Aug 12 '18 at 12:05
  • why the hard-coded value of 22? Surely the function ought to take a dynamic id from whatever button it is assigned to ? – Professor Abronsius Aug 12 '18 at 12:05
  • Possible duplicate of [PHP parse/syntax errors; and how to solve them?](https://stackoverflow.com/questions/18050071/php-parse-syntax-errors-and-how-to-solve-them) – Qirel Aug 12 '18 at 12:06
  • 2
    `$num = $_POST['id']` missing `;` at the end there. – Qirel Aug 12 '18 at 12:06
  • Check whether or not you are able to connect with your database or not in `mysqli_connect()` function. – Ahmed Numaan Aug 12 '18 at 17:16

1 Answers1

0

It might be syntax error from your side, please try the below code:

<?php
$num = $_POST['id'];

$con = mysqli_connect("localhost", "username", "password", "movies");
$vote = "UPDATE movies SET score=score + 1 WHERE id='$num'";
$run_vote = mysqli_query($con,$vote);
?>
Ray A
  • 1,283
  • 2
  • 10
  • 22