【问题标题】:Delete row in table - MySQL, PHP, HTML删除表中的行 - MySQL、PHP、HTML
【发布时间】:2015-11-27 22:01:05
【问题描述】:

我有一个显示来自 MySQL 数据库的数据的 HTML 表。我想添加一个“删除行”按钮,以便通过单击我可以从数据库表中删除一行(链接到成员 ID 的数据)。

我可以看到SQL应该做什么,但我不知道如何用PHP实现它。

我还有一个搜索页面,您可以在其中通过输入他们的名字来搜索成员,我也想在该搜索页面中添加相同的删除功能。

但现在我想让它在一个首先显示所有数据的 HTML 表格上工作!

这是我的代码(减去与数据库的连接和断开连接等等):

// Retrieve Member Details
$sql = "SELECT MemberID, FirstName, Surname, DOB, Address, County, 
               PostCode, MobileNo, Email FROM Members";
$result = mysql_query($sql, $connection);

//create table
echo "<table>";

// Loop through the data and then display chosen data
echo "<h2>Member Details</h2>";
echo "<tr><th>Member ID</th><th>First Name</th><th>Surname</th>
      <th>DOB</th><th>Address</th><th>County</th><th>Post Code</th>
      <th>Mobile Number</th><th>Email</th><tr>";
while($a_row = mysql_fetch_assoc($result))
    echo "<tr><td>" . $a_row['MemberID']
        . "</td><td>" . $a_row['FirstName']
        . "</td><td>" . $a_row['Surname']
        . "</td><td>" . $a_row['DOB']
        . "</td><td>" . $a_row['Address']
        . "</td><td>" . $a_row['County']
        . "</td><td>" . $a_row['PostCode']
        . "</td><td>" . $a_row['MobileNo']
        . "</td><td>" . $a_row['Email']
        . "</td><tr>";                                       
//close table
echo "</table>";

搜索页面代码:

<?php
  // Connecting to Database Server
  $connection = mysql_connect("localhost", "1520621",

  // If connection cannot be made to Database Server
  if (!$connection)
    die("Cannot connect to Database");

  // Select the database
  mysql_select_db("db1520621", $connection)
    // If Database cannot be found
    or die("Cannot find Database");

  // SQL
  // Select all data from Members table where FirstName matches that which is inserted into searchName using POST  
  $sql ="SELECT * FROM Members";
  $sql.=" WHERE FirstName=\"".$_POST["searchName"]."\"";

  // Execute the query
  $queryResult=mysql_query($sql);

  //Check for errors and display following messages if there is
  if (mysql_error())
  {
    echo "Problem with Query<br>";
    echo "The following error message was returned from MySQL:<br>";
    echo mysql_error();
    exit;
  }

  //Create table
  echo "<table>";
  echo "<h2>Member Details</h2>";
  echo "<tr><th>First Name</th><th>Surname</th><th>DOB</th><th>Address</th><th>County</th><th>Post Code</th><th>Mobile Number</th><th>Email</th><tr>";

  // If no results found show message. If results found, loop if there is more than 1 result
  if (mysql_num_rows($queryResult)==0)
  {
    echo "No members with that name";
  }
  else
  {
    while ($dbRecord=mysql_fetch_array($queryResult))
    {
      echo "<tr><td>".$dbRecord["FirstName"]."</td><td>".$dbRecord["Surname"]."</td><td>".$dbRecord["DOB"]."</td><td>".$dbRecord["Address"]."</td><td>".$dbRecord["County"]."</td><td>".$dbRecord["PostCode"]."</td><td>".$dbRecord["MobileNo"]."</td><td>".$dbRecord["Email"]."</td></tr>";
    }
  }

  // Close connection to Database
  mysql_close($connection);
  ?>

【问题讨论】:

  • 请不要使用 Mysql_* 函数,这些函数在最新版本的 PHP 中已被贬低
  • 我不清楚这个问题。当你运行它时会发生什么?

标签: php html mysql sql sql-delete


【解决方案1】:

您应该使用 PHP 数据对象 (http://php.net/manual/en/ref.pdo-mysql.php) 或 MySQLi (http://php.net/manual/en/book.mysqli.php)。

回显行上的 Ending &lt;tr&gt; 表未关闭 (&lt;/tr&gt;),这可能会影响 HTML 输出。

删除的SQL查询:

$query = sprintf("DELETE FROM Members WHERE MemberID = '%s'",
mysql_real_escape_string($member_id));

【讨论】:

    【解决方案2】:

    这是从数据库中删除行的完整代码。首先,您必须添加删除链接并传递要删除的成员 ID。

    // Check if delete button is clicked and delete respective row
    if(isset($_GET['DeleteID']) AND !empty($_GET['DeleteID']))
        mysql_query("DELETE FROM Members where MemberID = '".$_GET['DeleteID']."'", $connection);
    
    // Retrieve Member Details
    $sql = "SELECT MemberID, FirstName, Surname, DOB, Address, County, PostCode, MobileNo, Email FROM Members";
    $result = mysql_query($sql, $connection);
    
    //create table
    echo "<table>";
    
    // Loop through the data and then display chosen data
    echo "<h2>Member Details</h2>";
    echo "<tr><th>Member ID</th><th>First Name</th><th>Surname</th><th>DOB</th><th>Address</th><th>County</th><th>Post Code</th><th>Mobile Number</th><th>Email</th><th>Delete</th><tr>";
    while($a_row = mysql_fetch_assoc($result)){
        echo "<tr><td>" . $a_row['MemberID'] . "</td><td>" . $a_row['FirstName'] . "</td><td>" . $a_row['Surname'] . "</td><td>" . $a_row['DOB'] . "</td><td>" . $a_row['Address'] . "</td><td>" . $a_row['County'] . "</td><td>" . $a_row['PostCode'] . "</td><td>" . $a_row['MobileNo'] . "</td><td>" . $a_row['Email'] . "</td>";
        echo "<td><a href='?DeleteID=" . $a_row['MemberID'] . "' onclick=\"return confirm('Delete?')\">Delete</a></td></tr>";
    }
    //close table
    echo "</table>";
    

    搜索页面代码

    <?php
        // Connecting to Database Server
        $connection = mysql_connect("localhost", "1520621", "w9p1n5");
    
        // If connection cannot be made to Database Server
        if (!$connection)
        die("Cannot connect to Database");
    
        // Select the database
        mysql_select_db("db1520621", $connection)
            // If Database cannot be found
            or die("Cannot find Database");
    
        // SQL
        if(isset($_GET['DeleteID']) AND !empty($_GET['DeleteID']))
        mysql_query("DELETE FROM Members where MemberID = '".$_GET['DeleteID']."'", $connection);
    
        // Select all data from Members table where FirstName matches that which is inserted into searchName using POST  
        $sql ="SELECT * FROM Members";
        $sql.=" WHERE FirstName=\"".$_REQUEST["searchName"]."\"";
    
        // Execute the query
        $queryResult=mysql_query($sql);
    
        //Check for errors and display following messages if there is
        if (mysql_error())
        {
            echo "Problem with Query<br>";
            echo "The following error message was returned from MySQL:<br>";
            echo mysql_error();
            exit;
        }
    
        //Create table
        echo "<table>";
        echo "<h2>Member Details</h2>";
        echo "<tr><th>First Name</th><th>Surname</th><th>DOB</th><th>Address</th><th>County</th><th>Post Code</th><th>Mobile Number</th><th>Email</th><tr>";
    
        // If no results found show message. If results found, loop if there is more than 1 result
        if (mysql_num_rows($queryResult)==0)
        {
            echo "No members with that name";
        }
        else
        {
            while ($dbRecord=mysql_fetch_array($queryResult))
            {
              echo "<tr><td>".$dbRecord["FirstName"]."</td><td>".$dbRecord["Surname"]."</td><td>".$dbRecord["DOB"]."</td><td>".$dbRecord["Address"]."</td><td>".$dbRecord["County"]."</td><td>".$dbRecord["PostCode"]."</td><td>".$dbRecord["MobileNo"]."</td><td>".$dbRecord["Email"]."</td>";
              echo "<td><a href='?DeleteID=" . $dbRecord['MemberID'] .(isset($_REQUEST['searchName'])?'&searchName='.$_REQUEST['searchName']:''). "' onclick=\"return confirm('Delete?')\">Delete</a></td></tr>";
            }
        }
    
        // Close connection to Database
        mysql_close($connection);
    ?>
    

    【讨论】:

    • 我仍然无法正常工作。不过感谢您的帮助。
    • 你遇到了什么问题?
    • 出现删除选项,但仅针对表中的最后一条记录。当我按下删除时,实际上并没有删除任何内容。
    • 好的,我已经编辑了上面的脚本。您现在可以通过从我的答案中复制新脚本来尝试吗?
    • 是否可以让它在外部文件中运行?我想让成员在被搜索时被删除(我有一个搜索查询),但我不能同时执行搜索查询和删除查询?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-03
    • 1970-01-01
    • 2015-07-21
    • 1970-01-01
    • 2015-05-27
    • 2012-10-10
    • 1970-01-01
    相关资源
    最近更新 更多