简体   繁体   English

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

[英]PHP MySQL DELETE row from html table with link button

I've been looking at this for some time now, all I'm doing is learning PHP with mysql, with no previous programming experience before these so please be kind. 我一直在看这个已经有一段时间了,我正在做的就是用mysql学习PHP,之前没有以前的编程经验所以请善待。 I need to delete an entire row from a table. 我需要从表中删除整行。 The table is inside list.php and there's a delete button that redirects to delete.php . 该表位于list.php并且有一个删除按钮,重定向到delete.php But all it does it redirects me to a blank page with an url something like "delete.php?id=4" depending on row ID, which seems to be correct. 但所有这一切都将它重定向到一个空白页面,其中包含类似"delete.php?id=4"的URL,具体取决于行ID,这似乎是正确的。

This is connect.php : 这是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);

?>

And this is delete.php : 这是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);
    }

}
?>

Notice " print ($sql_delete); ", which doesn't print anything either. 注意“ print ($sql_delete); ”,它也不打印任何东西。 All I can think of is there is something wrong with the way I am asking for the ID by using $_GET , but can't get my head around it. 我能想到的是,我通过使用$_GET请求ID的方式有问题,但无法$_GET它。 When I run 我跑的时候

 "DELETE FROM Courses WHERE ID = 4;" 

inside xampp's mysql module, it works. 在xampp的mysql模块中,它可以工作。 Uhm... oh well. 嗯......太好了。

OK, let me add: This is actually my delete button inside list.php, which gives me an URL based on table row number, based on Course id. 好的,让我补充一下:这实际上是我在list.php中的删除按钮,它根据课程ID为我提供了基于表行号的URL。

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

connect.php 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 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 (Where is delete button) 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM