2016-04-12 9 views
0

Ich habe eine Mitglied/Konto-Website, die "Notizen" veröffentlicht und speichert sie in Ihrem Konto. Wenn jemand eine Notiz schreibt, erhält jede Notiz eine Bearbeitungs- und eine Löschtaste. Ich möchte, dass der Löschen-Button den entsprechenden Post löscht, jedoch löscht er die Notiz, die oben steht. Ich nehme an, das Problem liegt entweder im else, wenn der Löschen-Button gedrückt wird oder während der Zeit, wenn der Button erstellt wird. Brauche ich nicht eine Möglichkeit, den Tasten eine ID zuzuordnen?Schaltfläche zum Löschen für SQL

Vielen Dank im Voraus.

<!-- connections.php connects to the database --> 
<?php require 'connections.php'; ?> 

<!-- check to make sure the user is logged in, 
    if not then redirect them to login.php --> 
<?php session_start(); 
if(isset($_SESSION["UserID"])){ 
} else { 
    header('Location: Login.php'); 
    die(); 
}?> 

<!-- $result is a query containing all the notes 
    of the current user --> 
<?php $UserID = $_SESSION["UserID"]; 
     $result = mysqli_query($con, "SELECT * FROM notes WHERE UserID = '$UserID'");  
?> 

<!-- when you click 'save' upload the new note 
    to the database and refresh the page. 
    when you click 'delete' remote the note 
    that goes with that button from the database --> 
<?php if(isset($_POST['save'])) { 

     session_start(); 
     $note = $_POST['note']; 
     $UserID = ($_SESSION["UserID"]); 

     $sql = $con->query("INSERT INTO notes (UserID, note)Values('{$UserID}','{$note}')"); 

     header('Location: Account.php'); 

    } else if (isset($_POST['delete'])){ 
     $result = mysqli_query($con, "SELECT * FROM notes WHERE UserID = '$UserID'"); 
     $row = mysqli_fetch_array($result); 
     $noteID = $row['noteID']; 
     $UserID = ($_SESSION["UserID"]); 

     $sql = $con->query("DELETE FROM notes WHERE noteID = '$noteID'"); 

     header('Location: Account.php'); 

    } else if (isset($_POST['edit'])){ 


}?> 

<!DOCTYPE HTML> 
<html lang="en"> 
    <head> 
     <title>My Account</title> 
     <link rel="stylesheet" type="text/css" href="stylesheet.css"> 
    </head> 

    <body> 

     <h1 class="titleAct">Welcome</h1> 

     <form action="" method="post" name="notesubmit" id="notesubmit"> 

       <div> 
        <textarea name="note" cols="50" rows="4" form="notesubmit" id="noteinput">New Note 
        </textarea> 
       </div> 

        <input name="save" type="submit" class="button" id="save" value="Save"> 

      <!-- Whenever a note is saved, print out the 
       note with timestamp followed by the edit 
       and delete buttons for each note --> 
      <?php while ($row = mysqli_fetch_array($result)): ?> 
       <?php $note = $row['note'];?> 
       <?php $date = $row['time'];?> 

     <div id="note"> 
      <p class="note"><?php echo $date; ?></br> ---------------- </br><?php echo $note; ?></p> 
     </div> 

       <input name="edit" type="submit" class="button" id="edit" value="Edit"> 
       <input name="delete" type="submit" class="button" id="delete" value="Delete"> 

      <?php endwhile; ?> 
     </form> 

     <div> 
      <a class="link" href="Logout.php">Logout</a> 
      <div> 
      <a class="link" href="Deactivate.php">Deactivate My Account</a> 
      </div> 
     </div> 

    </body> 
</html> 

Antwort

0

Sie haben einen logischen Fehler ... Ihr nicht die ID der Notiz, die Sie überall löschen möchten zurückschicken, die Sie gerade sagen Sie etwas löschen möchten, holt dann die Liste der Notizen aus der Datenbank und löscht den ersten, die in knallt ...

Was ich tun würde, ist die Löschtaste auf einen Link zu ändern, und einen Abfrageparameter ($_GET['noteID']) Einst auswählen, welche ID zu löschen ...

Dann würde ich die NoteID aus der Datenbank auswählen, aber auch sicherstellen, dass der Benutzer versucht, sein eigenes p zu löschen ost, und nicht jemand elses ...

Viel Glück :)

Verwandte Themen