PHP MySQL使用链接按钮从html表中删除行

时间:2015-12-13 11:08:04

标签: php html mysql html-table

我已经看了一段时间了,我所做的只是用mysql学习PHP,之前没有以前的编程经验,所以请善待。我需要从表中删除整行。该表位于list.php内,并且有一个重定向到delete.php的删除按钮。但所有这一切都将它重定向到一个空白页面,其中的网址类似于"delete.php?id=4",具体取决于行ID,这似乎是正确的。

这是connect.php

<?php 
$server = "localhost";
$db_user = "root";
$db_pass = "";
$db_name = "school";

$db_conn = mysqli_connect($server, $db_user, $db_pass, $db_name);

$connect_error = 'Sorry, we are experiencing connection problems';

mysql_connect('localhost', 'root', '') or die($connect_error);
mysql_select_db('school') or die($connect_error);

?>

这是delete.php

<?php 
if(isset($_GET['ID'])){
    $courseID = $_GET['ID'];
    $sql_delete = "DELETE FROM Courses WHERE ID = $courseID";       
    print ($sql_delete);        
        $result = mysqli_query($db_conn,$sql_delete);
    if($result) {
        echo "Congratulations. You have deleted this course succesfully.";
        header('location:list.php');
    } else {
        echo "Error";
    error_reporting(E_ALL ^ E_DEPRECATED);
    }

}
?>

注意&#34; print ($sql_delete);&#34;,也没有打印任何内容。我能想到的是,我使用$_GET询问ID的方式有问题,但无法理解它。我跑的时候

"DELETE FROM Courses WHERE ID = 4;"

在xampp的mysql模块中,它可以工作。嗯......太好了。

好的,我来补充一下: 这实际上是我在list.php中的删除按钮,它根据课程ID为我提供了基于表行号的URL。

<?php echo "<td><a href=\"delete.php?id=$row[ID]\">Delete</a>";?>

1 个答案:

答案 0 :(得分:0)

connect.php

    $server = "localhost";
    $db_user = "root";
    $db_pass = "";
    $db_name = "school";
    $db_conn = mysqli_connect($server, $db_user, $db_pass, $db_name);

delete.php

if(isset($_GET['ID'])){
  $courseID = $_GET['ID'];
  $sql_delete = "DELETE FROM Courses WHERE ID = $courseID";        
  $result = mysqli_query($db_conn,$sql_delete);
  if(mysqli_affected_rows($db_conn)>0) {
      header('location:list.php?result=success');
  } else {
     header('location:list.php?result=fail');
  }
 }

list.php(删除按钮在哪里)

<?php
if(isset($_GET['result'))
{
if($_GET['result']=='success')
{

    echo "Congratulations. You have deleted this course succesfully.";
}
else{
     echo "error";
  }
}
?>
(Delete button)
<a href="delete.php?ID=5" >Delete</a>