0

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 } ?> 

 
ndc85430
  • 1,395
  • 3
  • 11
  • 17
  • 1
    _“my code only changes the first entry”_ — What exactly do you mean by “change”? Which part of the code? _When_? Inline event handlers like `onchange` are [bad practice](/q/11737873/4642212). They’re an [obsolete, cumbersome, and unintuitive](/a/43459991/4642212) way to listen for events. Always [use `addEventListener`](//developer.mozilla.org/en/docs/Learn/JavaScript/Building_blocks/Events#inline_event_handlers_%E2%80%94_dont_use_these) instead. – Sebastian Simon Jun 14 '23 at 16:56
  • 2
    You have duplicate ids - every form has a field with `id="hoch"`. Every form has a field with `id="runter"`. I don't know if it's causing your problem, but I can't imagine having invalid HTML like that is helping. – droopsnoot Jun 14 '23 at 16:57
  • What I mean by change only the first entry is that the code only changes the entry with id 1, no matter if I like the post with id 3 or 5 – Christian Radtke Jun 14 '23 at 17:01
  • @ChristianRadtke You still haven’t explained what you mean by “change”. I’m not asking what “first” means. – Sebastian Simon Jun 14 '23 at 17:02
  • 1
    What debugging have you done so far? What is `$thema['id']`? Use the [browser console (dev tools)](//webmasters.stackexchange.com/q/8525) (hit `F12`). The dev tools provide an **Inspector** / **Elements** tab. Inspect your elements. What is `onchange="document.eintrag.submit(); return false;"` rendered as? The dev tools provide a **Network** tab. Which _actual URL_ is requested? Look into the **Request** tab and into the **Response** tab inside the Network tab: do you see what you expect? – Sebastian Simon Jun 14 '23 at 17:04
  • @SebastianSimon change result in database – Christian Radtke Jun 14 '23 at 17:08
  • 1
    Your approach to access the form is overly complicated to begin with. Every input element within a form, has a [`form`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement#instance_properties_related_to_the_parent_form) property referring to that form. So `onchange="this.form.submit()"` should do perfectly fine here, without having to explicitly address the form via a generated name. – CBroe Jun 15 '23 at 06:43
  • And even if there was no `form` property, `this.closest("form").submit()` would do the trick. – Sebastian Simon Jun 15 '23 at 10:24

0 Answers0