php - 用于删除表行的脚本在 MySQL 和 PHP 中不起作用

标签 php html mysql sql

我想使用 MySQL 和 PHP 从数据库中删除表行。我搜索了我的代码,但不知道我做错了什么。我想我已经接近实现它了,可能是一些我没有意识到的简单的事情。

如果我将鼠标悬停在删除链接上,则会出现一个链接,其中显示要删除的行的正确 ID 号。但如果我点击它,它就不起作用。它只是刷新屏幕。

这是我的index.php代码:

<!-- Table -->

<form action="index.php" method="get" id="dispatch">
<div class="col-md-8 column">



     <fieldset>
        <legend>Incident Board (Incidents in red are active)</legend>
         <div class="scroll-bar">
         <table>
             <thead>
             <tr>
                 <th>Incident #</th>
                 <th>Town</th>
                 <th>Location</th>
                 <th>Incident Type</th>
                 <th>Time/Date</th>
                 <th>Admin</th>
                 <th>Delete Entry</th>
            </tr>
             </thead>
             <tbody>
             <?php


  if( isset($_POST['town']) )
  {
    $town = $_POST['town'];
  }

  if( isset($_POST['location']) )
  {
  $location = $_POST['location'];
  }

  if( isset($_POST['incident_type']) )
  {
  $incident_type= $_POST['incident_type'];
  }

  if( isset($_POST['time_date']) )
  {
  $time_date= $_POST['time_date'];
  }

  if( isset($_POST['admin']) )
  {
  $admin = $_POST['admin'];
  }

  if( isset($_POST['id']) )
  {
  $id = $_POST['id'];
  }



    $db = mysqli_connect('localhost','root','') or die("Database error"); 
    mysqli_select_db($db, 'cad');  
    $result= mysqli_query($db, "SELECT * FROM `cad` ORDER BY `time_date` DESC LIMIT 20"); 


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

  $town     = $row['town'];
  $location    = $row['location'];
  $incident_type     = $row['incident_type'];
  $time_date = $row['time_date'];
  $admin    = $row['admin']; 
  $id    = $row['id']; 

echo "


                    <tr>
                        <td class=\"id-center\">
                            ".$id."
                        </td>
                        <td >
                            ".$town."
                        </td>
                        <td>
                            ".$location."
                        </td>
                        <td >
                            ".$incident_type."
                        </td>
                        <td >
                            ".$time_date."
                        </td>
                        <td >
                            ".$admin."
                        </td>


                        <td>
                        <a href=\"delete.php?id=$id\" name=\"delete\" value=\"$id\" class=\"btn btn-primary btn-default center-1\"><span class=\"glyphicon glyphicon-trash\"></span></a>
                        </td> 


                        </tr>";
    }

  mysqli_close($db);


  ?>

             </tbody>
             </table> 
                </div>
             </fieldset>
             </div>
             </form>

<!-- End -->

这是我的delete.php代码:

<?php

    $username = "root";
    $password = "";
    $hostname = "localhost";

    $dbhandle = mysql_connect($hostname, $username, $password) or die("Could not connect to database");

    $selected = mysql_select_db("cad", $dbhandle);

    mysql_query("DELETE FROM cad WHERE id = ".$_GET['id']."");
    header('location: index.php');
?>

最佳答案

不推荐使用 mysql,而是使用 mysqli

<?php
$username = "root";
$password = "";
$hostname = "localhost";
$con=mysqli_connect($hostname, $username, $password,"cad");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

// Perform queries 
mysqli_query($con,"DELETE FROM cad WHERE id = ".$_GET['id']."");
mysqli_close($con);
?>    

关于php - 用于删除表行的脚本在 MySQL 和 PHP 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31129562/

相关文章:

php - 调用未定义函数 mysql_connect()

javascript - 动态调整iFrame高度动态改变内容高度(里面有解释)

php - 将日期格式转换为mysql时间戳

php - 如何防止重新执行php+mysql查询

php strtotime 上个月最后一天函数生成 2 个月前

javascript - 为使用 Blob 的 <embed> 或 &lt;iframe&gt; 设置默认另存为名称

css - 具有 html/css 背景的 Div

MySQL IN() 奇怪的行为

即使使用 sudo,MySQL 也无法以 root 身份登录本地主机

javascript - 如何将控制台错误显示为验证错误?