Increase sql field by pressing HTML button using PHP

0

I'm doing a website with PHP and HTML in which users upload photos with a title, an address, location and a description.

In addition, there is a button to vote on said photo.

The problem I'm having on that button. I can not get the SQL field to increase by 1 when I press the button.

Try using the post method as a form and have another PHP document handle the query and update the value in the MySQL table.

HTML:

<form name="relevanteBoton" method="post" action="rel.php">
    <input type="submit" value="Votar!">
</form>

PHP:

$id = $_POST['id'];
mysql_query("UPDATE 'fotos' SET 'relevancia=relevancia + 1' WHERE 'id = $id'");

I still do not understand deeply the consultations prepared with PDO. This did not work for me. I do not know if I will be doing anything or have some agreement. I do not know much about PHP or MySQL.

I also tried using AJAX and did not get good results.

<script type="text/javascript">
            <!--
                function incrementar()
                {
                $.ajax({
                    url: "rel.php",
                    context: document.body
                    }).done(function() {
                    alert('incremented');
                    });
                    return false;
                }
-->

HTML:

<span id="button" onclick="javascript:return inc_counter();">Votar!</span>

How could I solve it?

    
asked by Yamil Vernet 06.08.2017 в 02:04
source

1 answer

0

You need to first recover the counter where the photo is going. In your PHP

$id = $_POST['id'];

function dameRelevancia($id)
{
$sql = "SELECT RELEVACIA FROM FOTOS WHERE ID = $id";//PREPARA SQL
$result = mysqli_query($conexion,$sql);//EJECUTA LA SQL Y GUARDA
$cont = mysqli_fetch_array($result);//SACA LOS VALORES DE LO QUE REGRESO LA CONSULTA
return inval($cont["RELEVANCIA"]); //REGRESA TU VALOR PARSEANDOLO EN ENTERO
}


$voto = dameRelevancia($id) + 1;
mysqli_query($conexion,"UPDATE fotos SET relevancia = $voto WHERE 'id = $id'");

I recommend you use mysqli or PDO since mysql does not have support already.

Greetings

    
answered by 06.08.2017 в 02:27