【问题标题】:Script for deleting table rows not working in MySQL & PHP用于删除在 MySQL 和 PHP 中不起作用的表行的脚本
【发布时间】:2015-09-16 17:39:34
【问题描述】:

我想用 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');
?>

【问题讨论】:

  • var_dump($_GET);exit; on delete.php 看看你得到了什么。
  • 您在 index.php 中使用 MySQLi,然后在 delete.php 中使用 mysql 是否有原因?
  • 我认为您的删除按钮实际上是在提交表单,即刷新表单并且永远不会关注链接。如果你手动去 delete.php?id=xx 会发生什么
  • delete.php 对 SQL 注入开放。只是说。
  • 另外,最好不要让机器人抓取页面,否则所有内容都将被删除(一旦你开始工作)。只是说。

标签: php html mysql sql


【解决方案1】:

不推荐使用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);
?>    

【讨论】:

    猜你喜欢
    • 2013-12-24
    • 1970-01-01
    • 2015-08-13
    • 2020-03-23
    • 1970-01-01
    • 2021-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多