While and Rand() loop voting system

Asked

Viewed 108 times

0

I’m making a website with a voting system, but the images change places with the function Rand().

So far so good, but when it comes to voting, when I click on a button, the only one that works is the button in the first image, with the ID = 0.

Here is the code:

 <?php

        $bdd = new PDO('mysql:host=127.0.0.1;dbname=studio_photo_cergy_compte','root','rootroot');

        $i=0;

        $rep = $bdd->query('SELECT * FROM participant ORDER BY rand()');

        $name_value="concurrent";

        if(isset($_POST[$name_value.$i])) {

            $id = htmlspecialchars($_POST['id']);

            $reqmail = $bdd->prepare("UPDATE participant SET vote=vote+1 WHERE id =".$i);
            $reqmail->execute(array($vote));

            header("Location: merci_vote_sans_connexion.php");
        }

        while($photo = $rep->fetch()){
    ?>

        <img src="images/<?php echo $photo['image'] ?>">

        <form action="" method="post" align="center">
            <?php echo "<input type=\"submit\" name=\"".$name_value.$i."\" value=\"Voter\">"; ?>
        </form>
        <br><br>

        <?php 
            $i++;}
            $rep->closeCursor();
        ?>

I understand why the $i is always equal to 0, since the if(isset) is out of the while, and the increase of $i is inside the loop, however, I have tested several ways and I can’t make each button match a different image in my database using this while.

How to solve?

1 answer

0


Yeah, it’s just the line if(isset($_POST[$name_value.$i]))

As you yourself noticed, the variable $i will always be zero and the code snippet of this if will only execute when the first button (the zero) is clicked.

One way you can do this is to add the variable $i in the button value. Below is an example of how the form could be:

  while($photo = $rep->fetch()){
    ?>

        <img src="images/<?php echo $photo['image'] ?>">

        <form action="" method="post" align="center">
          <button name="votar" value="<?php echo $i; ?>"><?php echo 'Votar '.$i; ?></button>
        </form>
        <br><br>

        <?php 
            $i++;}
            $rep->closeCursor();
        ?>

Then the check with isset() will look like:

    if(isset($_POST['votar'])) {
        $id = $_POST['votar']; // armazena o value do button que no loop é definido com o id (variável $i)
     // segue o restante do se código

        $reqmail = $bdd->prepare("UPDATE participant SET vote=vote+1 WHERE id =".$i);
        $reqmail->execute(array($vote));
        header("Location: merci_vote_sans_connexion.php");
    }

Now the variable $id has the value of the button that was clicked and then you can recover clicks on any button.

NOTE: I meant the variable $i as id because it is with her that you are making the query in $reqmail = $bdd->prepare("UPDATE participant SET vote=vote+1 WHERE id =".$i);

I hope I’ve helped.

  • Thank you very much, it helped yes, I did not know how to get the $i amount into the While, but I understood, thank you :)

  • Nice @Mathismaudet. If you can mark the answer as valid, you can help those who have similar questions.

  • 1

    Okay, it’s just I’m new to the site, I didn’t know I had to do this, but it’s done now ^^

Browser other questions tagged

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