删除查询php无法正常工作,没有任何错误,并留在同一页面上

时间:2018-03-25 04:25:01

标签: php html database

我无法通过PHP代码从数据库中删除我的数据,尽管我可以从phpmyadmin中删除。 我附上了PHP和HTML。我试图使用phpmyadmin使用的相同代码,但没有进展。即便如此,我也没有收到用于调试的警告信息,并且没有结果。

<?php
    $conn2=mysqli_connect("localhost","root","") or die(mysql_error());
    mysqli_Select_db($conn2,"editor") or die("connot connect to the database");

    if (isset ($_GET ['delid'] ) ) {
        $deluser=$_GET['delid'];

        $alertMessage = "<div class='alert alert-danger'>
        <p> Are you sure you want to delete this record?</p><br>
        <form action='".htmlspecialchars($_SERVER['PHP_SELF']). " ?id=$deluser' method='post'>
            <input type='submit' class='btn btn-danger' id='con_del' name='con_del' value='Yes' delete!>
            <a href='location.. /listNB.php' class='close' data-dismiss='alert' aria-label='close'>X</a>
        </form>
        </div>
        ";
    }

    if (isset($_GET['con_del']))
    {
        $entry_id= $row["entry_id"];
        $sql= "DELETE FROM `editornb` WHERE `editornb`.`entry_id` = '".$entry_id."'";
        $que= mysqli_query($conn2,$sql);
             
        if ($que) {
            print'<script> alert("Sucessfully deleted!!!");</script>';
            
        } else{
            print'<script> alert("error");</script>';
            
        }

    }

?>
!DOCTYPE html>
<html>
<head>
    <title> CK EDITOR </title>
    <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="fontawesome/fontawesome-free-5.0.6/web-fonts-with-css/css/fontawesome.min.css">
    <script src="ckeditor/ckeditor/ckeditor.js" type="text/javascript" ></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="bootstrap/js/bootstrap.min.js" type="text/javascript"></script> 
</head>
<body>
    <?php
    if (isset($alertMessage))
     echo $alertMessage;

    ?>
    <br> 
    <a class="btn btn-success" href="textareaNB.php"> Home </a>
    <a class="btn btn-success" href="ListNB.php">List</a>
    <br><br>
    <table class="table table-striped table-bordered">
        <tr>
            <th> ID</th>
            <th> DATE</th>
            <th> Content</th>
            <th> Update</th> 
            <th> Delete</th>

        </tr>
        <?php
        session_start();
        $user=($_SESSION['u_uid']);
        $query="SELECT * FROM `editornb` where user_uid='".$user."'";
        $result= mysqli_query($conn2, $query);
        $result_check= mysqli_num_rows ($result);
        $date=date("M/d/y");
        if ( $result_check > 0 )
        {
         while ($row= mysqli_fetch_assoc($result)){
                echo "<tr>";
                echo "<td>" .$row["entry_id"]."</td>";
                echo "<td>" .$row["date"]. "</td>";
                echo "<td>" .$row["content"]."</td>";
                echo '<td><a href="update.php?upid='.$row['entry_id'].'" type="button" class="btn btn-primary btn-sm"> 
                <span class="fa fa-edit"></span> </a></td>';

                echo '<td><a href="ListNB.php?delid='.$row['entry_id'].'" type="button" class="btn btn-danger btn-sm">
                <span class="fa fa-trash"></span> </a></td>';

                echo"</tr>";
            }
        }
        ?>

    </table>
</booy>
</html>

3 个答案:

答案 0 :(得分:1)

好的家伙我找到了自己问题的解决方案...... 做了什么是我试图获取我试图删除的emtry号码。 通过使用我为获取表的数据所做的相同查询。 whcih是select * from tablename。

比使用mysqli_fetch_assoc我使用$ row varibale重新获取数据。  我的计划工作得很好,希望能帮助别人,

虽然我确实使用过@ KarloKokkak和@prasannaputtaswamy的建议

&#13;
&#13;
 if (isset($_POST['del']))
   {   
        $user=($_SESSION['u_uid']);
        $query="SELECT * FROM `editornb` where user_uid='".$user."'";
        $result= mysqli_query($conn2, $query);
        $result_check= mysqli_num_rows ($result);
        $row= mysqli_fetch_assoc($result);
        $id =($row['entry_id']);
        $user=($_SESSION['u_uid']);
        $sql= "DELETE FROM editornb WHERE user_uid='$user' AND entry_id= '$id' ";
        $answer= mysqli_query($conn2,$sql);
        if ($answer == TRUE ) {
         print'<script> alert("Sucessfully deleted!!!");</script>';
        } else{
         print'<script> alert("error");</script>';
      }
    }
&#13;
&#13;
&#13;

答案 1 :(得分:0)

mysqli_Select_db错字?

应该是: mysqli_select_db

删除htmlspecialchars()

中的htmlspecialchars($_SERVER['PHP_SELF'])

只需

 $_SERVER['PHP_SELF']

您正在为$entry_id分配错误的值;

发件人:

$entry_id= $row["entry_id"];

$entry_id= $_GET["id"];

在您的第二个PHP文件中 删除现有的session_start();并将其移动到该文件的最顶部。在发送标头之前,您无法输出任何内容。这会导致错误。

<?php session_start(); ?> <-- Move it there like that. No whitespace before "<?php"
!DOCTYPE html>
<html>
<head>
    <title> CK EDITOR </title>

您的<form>操作属性中还有一个额外的空格字符。在<form>标记中,action=""属性更改

" ?id=$deluser'

要:

"?id=$deluser'

答案 2 :(得分:0)

根据@Karlo Kokkak改变建议进行所有更改后

$entry_id= $_POST["id"]; 

$entry_id= $_POST["con_del"];

这应该可以正常工作。如果您回显查询,请检查是否正确传递了id。