删除SQL的按钮

时间:2016-04-12 17:55:50

标签: php mysql

我有一个会员/帐户网站发布" notes"并将其保存在您的帐户中。当有人发布笔记时,每个笔记都会有一个编辑和一个删除按钮。我希望删除按钮删除相应的帖子,但是删除其中的任何一个音符都在顶部。我认为问题在于按下删除按钮时的else或者在创建按钮的时间内。我不需要一种方法来为按钮分配ID吗?

提前致谢。

<!-- 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>

1 个答案:

答案 0 :(得分:0)

你有一个逻辑错误......你没有在任何地方发回你要删除的笔记的ID,你只是说要删除某些东西,然后从中取出笔记列表数据库,并删除弹出的第一个...

我要做的是将删除按钮更改为链接,并设置查询参数($_GET['noteID'])以选择要删除的ID ...

然后我会从数据库中选择NoteID,但也要确保用户试图删除自己的帖子,而不是其他人...

祝你好运:)