Script führt Update aller Spalten aus anstatt nur der gewählten

Einklappen
X
 
  • Filter
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge

  • Script führt Update aller Spalten aus anstatt nur der gewählten

    Hallo, ich habe folgendes Problem. In einem Script für einen Onlineshop soll die Tabelle Lieferadresse bearbeitbar sein. Das Hinzufügen neuer Lieferadressen und das Löschen funktionieren einwandfrei. Nur das Bearbeiten stellt ein Problem dar, denn immer wenn im Modal auf Speichern geklickt wird, ändert das Script alle Datensätze in der Tabelle, die dem jeweiligen User zugeordnet sind. Das heißt, wenn ich Lieferadresse 1 editieren möchte, werden auch die Lieferadressen 2, 3 etc mit den gleichen Daten gefüllt.

    Hier der Code der profile.blade.php in welcher die Adresse angezeigt werden.

    PHP-Code:
    <div class="">
                            <div class="bg-light rounded h-100 p-4">
                                <h6 class="mb-4"></h6>
                                    <div class="testimonial-item text-center">
                                        <img class="img-fluid rounded-circle mx-auto mb-4" src="img/user/<?=htmlentities($user['prof_img']); ?>" style="width: 100px; height: 100px;">
                                        <h5 class="mb-1"><?=htmlentities($user['vorname']); ?> <?=htmlentities($user['nachname']); ?></h5>
                                        <p><?=htmlentities($user['position']); ?></p>
                                        <p class="mb-0"><td><?=htmlentities($user['besch']); ?></td></p>
                                    </div>
                            </div>
                        </div>
                        <div class="col-12">
                            <div class="bg-light rounded h-100 p-4">
                                <h6 class="mb-4">Übersicht persönliche Daten</h6>
                                <div class="table-responsive">
                                    <table class="table">
                                        <thead>
                                            <tr>
                                                <th scope="col">Email</th>
                                                <th scope="col">Adresse</th>
                                                <th scope="col">PLZ</th>
                                                <th scope="col">Ort</th>
                                                <th scope="col">Bundesland</th>
                                                <th scope="col">Land</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <tr>
                                                <td><?=htmlentities($user['email']); ?></td>
                                                <td><?=htmlentities($user['adresse']); ?></td>
                                                <td><?=htmlentities($user['plz']); ?></td>
                                                <td><?=htmlentities($user['ort']); ?></td>
                                                <td><?=htmlentities($user['bundesland']); ?></td>
                                                <td><?=htmlentities($user['land']); ?></td>
                                                <td><a style="margin-left: 30%;" href="?view=settings" class="btn btn-primary">Bearbeiten</a></td>
                                            </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                        <div class="col-12">
                            <div class="bg-light rounded h-100 p-4">
                                <h6 class="mb-4">Lieferadressen</h6>
                                <div class="table-responsive">
                                    <table class="table">
                                        <thead>
                                            <tr>
                                            <th scope="col">Nr</th>
                                                <th scope="col">Vorname</th>
                                                <th scope="col">Nachname</th>
                                                <th scope="col">Adresse</th>
                                                <th scope="col">PLZ</th>
                                                <th scope="col">Ort</th>
                                                <th scope="col">Bundesland</th>
                                                <th scope="col">Land</th>
                                            </tr>
                                        </thead>
                                        <?php
                                    $statement 
    $pdo->prepare("SELECT * FROM users INNER JOIN lieferadresse ON users.email = lieferadresse.email WHERE users.id = $_SESSION[userid]");
                                    
    $statement->execute();
                                    
    $lieferadresse $statement->fetchAll(PDO::FETCH_ASSOC);
                                        if (
    is_array($lieferadresse)) {
                                                foreach (
    $lieferadresse as $la) { ?>
                                        <tbody>
                                            <tr>
                                            <td><?=htmlentities($la['id']); ?></td>
                                                <td><?=htmlentities($la['vorname']); ?></td>
                                                <td><?=htmlentities($la['nachname']); ?></td>
                                                <td><?=htmlentities($la['adresse']); ?></td>
                                                <td><?=htmlentities($la['plz']); ?></td>
                                                <td><?=htmlentities($la['ort']); ?></td>
                                                <td><?=htmlentities($la['bundesland']); ?></td>
                                                <td><?=htmlentities($la['land']); ?></td>
                                                <td><button style="margin-left: 30%;" name="<?=$la['id']?>" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#staticBackdrop-<?=$la['id']?>" value="<?=$la['id']?>"> Bearbeiten</button></td>
                                                <td><form action="functions/modal.inc.php" method="POST"><button style="margin-left: 20%;" name="del_la" type="submit" class="btn btn-primary" value="<?=$la['id']?>"> Löschen</button></form></td>
                                            </tr>
                                        </tbody>


                                        <?php
                            
    }
                        }
                    
    ?>
                                    </table>
                                </div>
                                <br>
                                <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#staticBackdrop-reg"><i class="fa fa-plus"></i> Neue Lieferadresse</button>
                            </div>
                        </div>
                        <?php require_once 'includes/modal.la.php' ?>
                        <?php require_once 'includes/modal.reg.php' ?>
                        <?php require_once 'functions/modal.inc.php' ?>
    Hier der Code der modal.la.php welcher das Modal und den entsprechenden PHP-Code enthält.

    PHP-Code:
    <!-- Modal -->
    <?php
        
    if (is_array($lieferadresse)) {
        foreach (
    $lieferadresse as $la) {
          if(isset(
    $_GET['save']) && isset($_GET['id'])) {
            
    $save $_GET['save'];
            
    $id $_GET['save'];

            if(
    $save == 'la_add') {
                
    $id trim($_POST['id']);
                
    $vorname trim($_POST['vorname']);
                
    $nachname trim($_POST['nachname']);
                
    $adresse trim($_POST['adresse']);
                
    $plz trim($_POST['plz']);
                
    $ort trim($_POST['ort']);
                
    $bundesland trim($_POST['bundesland']);
                
    $land trim($_POST['land']);

                if(
    $vorname == "" || $nachname == "" || $adresse == "" || $plz == "" || $ort == "" || $bundesland == "" || $land == "") {
                    
    $error_msg "Bitte Vor- und Nachname ausfüllen.";
                } else {
                    
    $statement $pdo->prepare("UPDATE lieferadresse SET vorname = :vorname, nachname = :nachname, adresse = :adresse, plz = :plz, ort = :ort, bundesland = :bundesland, land = :land, updated_at=NOW() WHERE id = :id");
                    
    $result $statement->execute(array('vorname' => $vorname'nachname'=> $nachname'adresse'=> $adresse'plz'=> $plz'ort'=> $ort'bundesland'=> $bundesland'land'=> $land'id' => $la['id'] ));

                    
    $success_msg "Daten erfolgreich gespeichert.";
                    echo 
    "<script>window.open('account.php?view=profile','_self') </script>";
                }
            }
        }
    ?>
    <div class="modal fade" id="staticBackdrop-<?=$la['id']?>" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="staticBackdropLabel">Lieferadresse bearbeiten</h5>
            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
          </div>
          <div class="modal-body">
          <div class="">
                            <div class="bg-light rounded h-100 p-4">
                                <form action="account.php?view=profile&save=la_add&id=<?=$la['id']?>" method="post">
                                <input type="hidden" class="form-control" id="inputNachname" name="id" value="<?=$la['id']?>">
                                    <div class="mb-3">
                                        <label for="inputVorname" class="form-label">Vorname</label>
                                        <input type="text" class="form-control" id="inputVorname" name="vorname"
                                             value="<?=htmlentities($la['vorname']); ?>">
                                    </div>
                                    <div class="mb-3">
                                        <label for="inputNachname" class="form-label">Nachname</label>
                                        <input type="text" class="form-control" id="inputNachname" name="nachname" value="<?=htmlentities($la['nachname']); ?>">
                                    </div>
                                    <div class="mb-3">
                                        <label for="inputAdresse" class="form-label">Adresse</label>
                                        <input type="text" class="form-control" id="inputAdresse" name="adresse" value="<?=htmlentities($la['adresse']); ?>">
                                    </div>
                                    <div class="mb-3">
                                        <label for="inputPLZ" class="form-label">PLZ</label>
                                        <input type="text" class="form-control" id="inputPLZ" name="plz" value="<?=htmlentities($la['plz']); ?>">
                                    </div>
                                    <div class="mb-3">
                                        <label for="inputOrt" class="form-label">Ort</label>
                                        <input type="text" class="form-control" id="inputOrt" name="ort" value="<?=htmlentities($la['ort']); ?>">
                                    </div>
                                    <div class="mb-3">
                                        <label for="inputBundesland" class="form-label">Bundesland</label>
                                        <input type="text" class="form-control" id="inputOrt" name="bundesland" value="<?=htmlentities($la['bundesland']); ?>">
                                    </div>
                                    <div class="mb-3">
                                        <label for="inputLand" class="form-label">Land</label>
                                        <input type="text" class="form-control" id="inputLand" name="land" value="<?=htmlentities($la['land']); ?>">
                                    </div>
                                    <button type="submit" name="<?=$la['id']?>" value="<?=$la['id']?>" class="btn btn-primary">Speichern</button>
                                  </form>
                            </div>
                        </div>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Abbrechen</button>
          </div>
        </div>
      </div>
    </div>
    <?php
      
    }
    ?>
    Vielleicht hat jemand eine Idee und ich wäre für jeden Vorschlagbar dankbar. Viele Grüße.

  • #2
    Ja, das ist genau das was du da machst. Du gehst beim Speichern in einen foreach und updatest alle ids in diesem Array mit dem Daten.

    Du brauchst keinen foreach beim Speichern und musst dann die per GET übergeben id verwenden.
    Du solltest aber zum Speichern die id nicht per GET übergeben sondern als hidden field ebenfalls per POST. Sonst ist es etwas gar einfach das zu manipulieren.

    Kommentar


    • #3
      Vielen Dank für Antwort. Ich bin jetzt etwas ratlos, wie ich das abändern kann, kannst Du mir eventuell ein Beispiel geben, denn while klappt es auch nicht. Viele Grüße

      Kommentar


      • #4
        Du brauchst gar keine Schleife beim Speichern. Wieso auch?

        Kommentar


        • #5
          Vielen lieben Dank. Das Script arbeitet jetzt perfekt. Wollte mich auch noch für all die Tipps bedanken. Habe auf POST umgestellt und übergebe die ID nun mit einem Hidden-Input-Field. Letztlich war der Fehler lediglich eine falsche Schreibweise der ID im Execute-Befehl von SQL. Habe dank Dir viel gelernt. Schönes WE.
          Zuletzt geändert von MattOhl; 28.01.2023, 23:41.

          Kommentar

          Lädt...
          X