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?
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 :)
– Mathis Maudet
Nice @Mathismaudet. If you can mark the answer as valid, you can help those who have similar questions.
– GeekSilva
Okay, it’s just I’m new to the site, I didn’t know I had to do this, but it’s done now ^^
– Mathis Maudet