I have a form in a while loop for each post. I'm trying to create a form for each post so that you can like each post. Unfortunately, although I give each form a unique id with a hidden tag, my code only changes the first entry. I tried a lot to get the code to work. Hopefully someone can help me here.
<?php
$ipadresse = $_SERVER["REMOTE_ADDR"];
if(isset($_POST)){
$vote = $_POST['vote'];
$eintrag_id = $_POST['eintrag_id'];
$statement = $pdo->prepare("SELECT reaction FROM reactionen WHERE eintrag = ? AND ipadresse = ?");
$statement->execute(array($eintrag_id, $ipadresse));
$reaction = $statement->fetch();
if ($reaction !== false)
{
$stmt = $pdo->prepare("UPDATE reactionen
SET reaction = :reaction WHERE eintrag = '{$eintrag_id}' AND ipadresse = '{$ipadresse}'");
$stmt->execute(array('reaction' => $vote));
}
else
{
$insert = $pdo->prepare("INSERT INTO reactionen (eintrag, ipadresse, reaction)
VALUES (:eintrag_id, :ipadresse, :reaction)");
$insert->execute(array(
'eintrag_id' => $eintrag_id,
'ipadresse' => $ipadresse,
'reaction' => $vote
));
}
}
$statement = $pdo->prepare("SELECT id, imagedata, thema, beschreibung FROM weitere");
$statement->execute(array());
while($thema = $statement->fetch()) {?>
<div class="thumbnail">
<?php
$ergebnis = "SELECT MIN(id),imagedata FROM weitere WHERE id = '{$thema['id']}'";
$resultat = $pdo->query($ergebnis);
foreach ($resultat as $reihe):
if(isset($reihe['imagedata']) && $reihe['imagedata'] != '0'){?>
<img id="image" src="Bilder/<?php echo $reihe['imagedata'];?>">
<?php } endforeach; ?>
<span class="span">
<h3><?php echo $thema['thema']; ?></h3>
</span>
<p><?php echo nl2br($thema['beschreibung']); ?></p><br><br>
<form method="post" name="eintrag<?php echo $thema['id']; ?>">
<input type="hidden" class="eintrag_id" name="eintrag_id" value="<?php echo $thema['id']; ?>">
<table style="position:absolute; bottom:0px; left:0px; vertical-align:middle">
<tr>
<td><input type="radio" name="vote" id="hoch" value="1" onchange="document.eintrag<?php echo $thema['id']; ?>.submit(); return false;">
<label for="hoch"><img src="Bilder/hoch.png"></label></td>
<td>
<p <?php
$statement1 = $pdo->prepare("SELECT COUNT(*) AS anzahl FROM reactionen WHERE reaction = '1' AND eintrag = ?");
$statement1->execute(array($thema['id']));
$like = $statement1->fetch(); ?>><?php echo $like['anzahl'];?></p>
</td>
<td>
<input type="radio" name="vote" id="runter" value="-1" onchange="document.eintrag<?php echo $thema['id']; ?>.submit(); return false;">
<label for="runter"><img src="Bilder/runter.png"></label></td>
<td>
<p <?php
$statement2 = $pdo->prepare("SELECT COUNT(*) AS anzahl FROM reactionen WHERE reaction = '-1' AND eintrag = ?");
$statement2->execute(array($thema['id']));
$dislike = $statement2->fetch(); ?>><?php echo $dislike['anzahl'];?></p>
</tr>
</table>
</form>
<div id="break"></div>
<a href="kasse.php?titel=<?php echo rawurlencode($thema['thema']); ?>&id=<?php echo $thema['id']; ?>">
<img src="Bilder/pay.png" class="img" />
</a>
</div>
<?php } ?>