I'm trying to click a picture and change a database value to "0" via an onclick Javascript function. I looked at a few examples but I can't seem to make it work.
How can you use php in a javascript function
How to update data using onclick even CHECKBOX without button submit in php and mysql
Update Database from Javascript onClick By Ajax PHP
This is the index.html file
<script language="javascript">
function update() {
$.post("update.php", { num: 0 }, function(result) {
alert(result);
});
}
</script>
<img src="image.png" onclick="update()">
This is the update.php file.
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$num = $_POST["num"];
$id = 1;
$sql2 = "UPDATE db SET Column1='$num' WHERE id='$id'";
if ($conn->query($sql2) === TRUE) {
echo "Updated";
} else {
echo "Failed";
}
Would someone please point out what I'm doing wrong?