Implement a PHP and mysqli like button

Asked

Viewed 737 times

0

I’m trying to implement a like button on a system I’m creating for testing. It already changes the value of the likes in the database, but I have to put the value in an input field that I put. So every number I put in the input and click on like it does the update in my database. I would like to know how I do not need to enter the value in the input and each time I click it add another number in the value that already has in the database. Here’s the code:

Index.php

<?php 

  require_once "conecta.php";

  $resultado = mysqli_query($conexao, "select * from curtida");
  $curtida = mysqli_fetch_assoc($resultado);
?>

<!DOCTYPE html>
<html lang="pt-br">
<head>
  <meta charset="UTF-8" />
  <title>Curtir</title>
  <link rel="stylesheet" href="css/claudina.css" />
</head>
<body>
  <section class="recipiente margem-topo-50">
   <form action="envia_curtir.php" method="post">
   <input type="number" name="curtida" />
   <button class="icones icone-polegares-para-cima"></button>
   <?=$curtida['curtidas'];?>
    </form>
  </section>
</body>
</html>  


php.

<?php

  require_once "conecta.php";
  require_once "funcoes.php";

  $curtida = $_POST['curtida'];

  curtir($conexao, $curtida);

  header("Location: index.php");

?>

funcoes.php

<?php

  function curtir($conexao, $curtida) {

    $query = "update curtida set curtidas= $curtida";

    return mysqli_query($conexao, $query);
  }

1 answer

1


You can simply change the update:

function curtir($conexao, $curtida) {

    $query = "update curtida set curtidas = curtidas + 1";

    return mysqli_query($conexao, $query);
  }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.