删除php和mysqli

时间:2018-04-17 09:23:34

标签: php html mysql

我已经使用php,html和mysql创建了一个应用程序。应用程序可以将用户的信息(如id,name,bio和日期)存储到数据库中并显示在html表中。 id是一个自动增量值,随着用户输入的每个数据而增加。应用程序的插入部分工作正常但是当我尝试删除记录时没有任何反应。 html表单是我有意决定不包含的代码的一部分。这是我的代码的快照:

   $records = array();

 if(!empty($_POST)) {
    if(isset($_POST['firstName'],$_POST['lastName'], $_POST['bio'])){
    $firstName = trim($_POST['firstName']);
    $lastName  = trim($_POST['lastName']);
    $bio       = trim($_POST['bio']);

if(!empty($firstName) && !empty($lastName) && !empty($bio)) {
    $insert = $db->prepare("INSERT INTO people (firstName, lastName, 
    bio, created) VALUES (?, ?,?, NOW())");
    $insert->bind_param('sss', $firstName, $lastName, $bio); 

    if($insert->execute()){

        header('Location: addressbook.php');
        die();
       }

      }
      }
     }

  if($results = $db->query("SELECT * FROM people")){
  if($results->num_rows){
   while($row = $results->fetch_object()){
    $records[] = $row;
  }
 $results->free();  
}
 } 


 ?>

 <!DOCTYPE html>
 <html>
 <head></head>
 <body>
 <div class = "container">



<?php
    if(!count($records)){

    echo 'No records found';
    }

    else{


?>
<table class="table">
<thead>
    <tr>
        <th>ID</th>
        <th>First Name</th>
        <th>Last Name</th>
        <th>Bio</th>
        <th>Created</th>
        <th>Delete</th>
    </tr>
</thead>
<tbody>

    <?php
        foreach ($records as $r) {


    ?>
        <tr>
            <td><?php echo escape($r->id);?></td>
            <td><?php echo escape($r->firstName); ?></td>
            <td><?php echo escape($r->lastName); ?></td>
            <td><?php echo escape($r->bio); ?></td>
            <td><?php echo escape($r->created); ?></td>
            <td>
            <a  onclick="return confirm('Do you want to delete the 
          record')" href="addressbook.php?idd=<?php echo $row['id'] ?>" 
          class="btn btn- 
          danger">Delete</a></td>



        <?php

        }
        ?>
     </tr>

 //My guess is the problem is with this code down here for deleting

    <?php

    if(isset($_POST['idd'])){
        $idd = $_POST['idd'];
        $results = $db->query("DELETE FROM people WHERE id=$idd");
        if($results){
            header('Location: addressbook.php');
        }
    }
   ?>



  </tbody>
 </table>

   <?php

   }
   ?>

4 个答案:

答案 0 :(得分:0)

您需要使用$_GET,因为默认情况下href标记会使用GET方法发送数据。  你的代码应该是

 if(isset($_GET['idd'])){
        $idd = $_GET['idd'];
        $results = $db->query("DELETE FROM people WHERE id='$idd'");
        if($results){
            header('Location: addressbook.php');
        }
    }
  

注意 - 使用预准备语句来避免sql注入攻击

答案 1 :(得分:0)

`

<?php
    //database connectivity


    $con=mysqli_connect("localhost","root","");

 mysqli_select_db($con,"<db_name>");
 $idd = $_REQUEST['idd'];
 $sql= "DELETE FROM people WHERE id='$idd' ";

 $result = mysqli_query($con,$sql) or die(mysql_error());
header("refresh:0.1; addressbook.php");
?>`

答案 2 :(得分:-1)

 if(isset($_GET['idd'])){
      $idd = $_GET['idd'];
      $results = $db->query("DELETE FROM people WHERE id='{$idd}'");

尝试添加单引号。 如果它仍然无效,请查看$ _POST是否实际上正确发布。

答案 3 :(得分:-1)

在删除用户功能中尝试$results = $db->query("DELETE * FROM people WHERE id=$idd");而不是$results = $db->query("DELETE FROM people WHERE id=$idd");:)