删除表上的记录

时间:2014-12-05 11:00:07

标签: php html

我在删除桌面上的某条记录时遇到问题。我目前所做的是为删除和编辑创建两个表单。出于某种原因,我无法获得已删除的所需ID。

我尝试回显从该表单传递的id的值。并且传递的ID是13.

所以基本上我为行做了一个循环。我在从某一行获取ID时遇到问题。

这是我桌子的截图。

http://prntscr.com/5df5xf

这是我如何使用表单

customer.php

<div class="box-body table-responsive">
                                    <form id='edit' action="functions/edit.php" method="post"></form>
                                    <form id='delete' action="functions/delete.php" method="post"></form>
                                    <table id="example1" class="table table-bordered table-striped">
                                        <thead>
                                            <tr>
                                                <th>Actions</th>
                                                <th>ID</th>
                                                <th>User</th>
                                                <th>Contact Number</th>
                                                <th>Date Registered</th>
                                                <th>Email</th>
                                                <th>Organization</th>
                                                <th>Address</th>


                                            </tr>
                                        </thead>
                                        <tbody>


                                         <?php

                                        $Query = mysqli_query($conn, "SELECT * FROM customer");

                                            while($row = mysqli_fetch_array($Query)){


                                            echo "<tr>";
                                            echo "<td><div class='btn-group'>

                                                    <button type='button' class='btn btn-info dropdown-toggle' data-toggle='dropdown'>Action 
                                                        <span class='caret'></span>
                                                        <span class='sr-only'>Toggle Dropdown</span>
                                                    </button>

                                                    <ul class='dropdown-menu' role='menu'>
                                                        <li><button form='edit' class='btn' type='submit' name='edit' style='width: 100%;background-color: white;'>Edit</button></li>
                                                        <li><button form='delete' class='btn' type='submit' name='delete' style='width: 100%;background-color: white;'>Delete</button></li>
                                                    </ul>
                                                     </div>
                                                    </td>";


                                            echo "<td>". "<input form='delete' type='hidden' name='id' value='" . $row['id'] . "'> " .  $row['id'] . "</td>";
                                            echo "<td>". $row['firstname'] . " " . $row['lastname'] . "</td>";
                                            echo "<td>". $row['contact'] . "</td>";
                                            echo "<td>". $row['date_reg'] . "</td>";
                                            echo "<td><span class='label label-success'>" . $row['email'] . "</span></td>";
                                            echo "<td>". $row['organization'] . "</td>";
                                            echo "<td>". $row['address'] . "</td></tr>";

                                             }
                                            ?>

                                        </tbody>
                                        <tfoot>
                                            <tr>
                                                <th>Actions</th>
                                                <th>ID</th>
                                                <th>User</th>
                                                <th>Contact Number</th>
                                                <th>Date Registered</th>
                                                <th>Email</th>
                                                <th>Organization</th>
                                                <th>Address</th>


                                            </tr>
                                        </tfoot>
                                    </table>


                            </div>

这里是我的delete.php的样子

<?php

  include('config.php');

  $deleteID = $_POST['id'];


  //----- Check if user to be deleted is the user in session -----//

  echo $deleteID;
  //   $deleteSQL = mysqli_query($conn, "delete from customer where id = '$deleteID'");


  echo "<script type='text/javascript'>alert('Deleted succesfuly');</script>";
  //header('Refresh: 0; url=../customers.php');

  //
?>

1 个答案:

答案 0 :(得分:0)

你的delete.php应该是这样的:

<?php
  include('config.php');
  $deleteID = $_POST['id'];
  // Create connection
  $conn = new mysqli($servername, $username, $password, $dbname);
  // Check connection
  if ($conn->connect_error) {
      die("Connection failed: " . $conn->connect_error);
  }
  $deleteSQL = mysqli_query($conn, "delete from customer where id = '$deleteID'");
  if ($conn->query($sql) === TRUE) {
      echo "<script type='text/javascript'>alert('Deleted succesfuly');</script>";
  //header('Refresh: 0; url=../customers.php');
  } else {
      echo "Error: " . $sql . "<br>" . $conn->error;
  }

  $conn->close();
  ?>

现在,如果在 config.php 中正确指定$servername, $username, $password, $dbname,这将有效。

你身边的编码很差。