Problems executing my vote script js

0

When I execute the input (BTN) it counts all the rows of the products of the DB, it is not telling me when I select only a specific product:

My table is called "products"

id  Nombre  precio  stock     ruta           voto
1   Camisas $5.00   35    img/camisa.jpg     2
2   casacas $55.00  55    img/casacas.jpg    2
3   boxer   $67.00  20    img/boxer.jpg      2
4   zapatos $45.00  15    img/zapatos.jpg    2

PHP script "voto.php"

<?php
require_once("conn.php");
$strSQL_Result  = mysqli_query($connection,"select 'voto' from 
'productos' where id=id");
$row            = mysqli_fetch_array($strSQL_Result);

$voto       = $row['voto'];
if($_POST)
{
if(isset($_COOKIE["counter_gang"]))
{
    echo "-1";
    exit;
}

setcookie("counter_gang", "votos", time()+3600*24);
if(mysqli_real_escape_string($connection,$_POST['up']) == 'voto')
{
    $update = "'voto'='voto'+1";
}

mysqli_query($connection,"update 'productos' set $update where 'id'='id'");
echo 1;
exit;   
}
?>

My JS script "sumarVoto.js"

$(document).ready(function() {
$("#votar").removeAttr("disabled");
$('#votar').click(function(e)
 {
    var val = parseInt($("#votar").val(), 10);
    $.post("voto.php", {up:"voto"},function(data)
    {
        if(data==1)
        {
            $("#status").html("Voto Exitosamente!!");
            val = val+1;
            $("#votar").val(val);
            $("#votar").attr("disabled", "disabled");
            $("#votar").css("background-image","url(voto1.png)");
        }
        else
        {
            $("#status").html("Ya vote!!");
        }
    })
  });
 });

HTML:

<div>
  <input  id="votar" type="submit" value="votar">
  <span id="status">...</span>
</div>

When I execute the normal input, it executes but the error is that it adds up all the rows of the "products" table and the COOKIE does not work for me either

    
asked by Alex 08.03.2018 в 05:27
source

1 answer

0

Your code has several problems. Leaving aside doubts about the best way to do things, and limiting ourselves to direct errors, you have:

  • In your javascript, although you are looking for the value of #votar that I assume should be the id of the product voted (but I'm not sure about it), do not send it to the server. Your call is $.post("voto.php", {up:"voto"}) , that is, you do not send more than the string "vote". The server does not know what you're voting for.
  • On the server, in each query you add a where id=id as the only test. That test is always true, because for all lines, the id column is equal to itself. Therefore, you receive or update all the lines.
  • answered by 08.03.2018 в 12:51