使用PHP从MySQL数据库删除数据的表格 [英] Form to delete data from MySQL database using PHP

查看:54
本文介绍了使用PHP从MySQL数据库删除数据的表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在创建一个包含2种表单的小程序,一种将数据添加到数据库中,另一种从数据库中删除.我已经设法创建了第一个输入表单,但是对于如何使第二个表单起作用我有些困惑.在数据库任务"中,我有一个名为"ID"的表,该表具有"ID","Name"和"Hours"列

So I'm creating a small program with 2 forms, one to add data to a database, and one to delete from it. I've managed to create the first input form, but I'm slightly confused as to how I would get the second form to work. In the database "tasks" I have a table called "ID" which has the columns "ID", "Name" and "Hours"

这是这两种HTML表单的样子

Here's what the two HTML forms look like

<h2>Add Tasks</h2> 
<form action="test.php" method="get">
Name of Task: <input type="text" name="name"><br />
Hours: <input type="number" name="hours"><br />
<input type="submit" value="Add" name="submit">
</form>

<h2>Delete Tasks</h2> 
<form action="delete.php" method="get">
ID: <input type="number" name="ID"><br />
<input type="submit" value="Delete">
</form>

PHP的第一个表单添加任务"将插入数据

And the PHP for the first form "Add tasks" which inserts data

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


$conn = new mysqli($servername, $username, $password, "Tasks");


if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
};


if (isset($_GET['submit'])) {

mysqli_select_db ($conn,"Tasks");
$name = $_GET['name'];
$hours = $_GET['hours'];

$sql = "INSERT INTO ID (Name, Hours) VALUES ('".$name."','". $hours."')";
$results = mysqli_query($conn,$sql);


$query = "SELECT `Name` FROM `ID`";
$result = mysqli_query($conn, $query);
$x=0;

第二种形式的PHP用于删除任务.这是无效的部分

And the PHP for the second form which deletes tasks. This is the part that is not working

if (isset($_GET['submit'])) {
mysqli_select_db ($conn, "Tasks");
$id = $_GET['id'];
$sql = "DELETE FROM ID (ID) VALUES ('".$id."')";

$query = "SELECT `Name` FROM `ID`";
$result = mysqli_query($conn, $query);
$x=0;

我应该如何格式化第二个按钮的PHP.我基本上已经重用了第一种形式的代码.我是否需要与第一个按钮区分开?当前页面显示为完全空白.我是一个完整的新手,所以我们将不胜感激.

How should I format the PHP for the second button. I've basically reused the code for the first form. Do I need to differentiate it somehow from the first button? Currently the page is showing up completely blank. I'm a complete novice so any help would be appreciated.

推荐答案

您的SQL语句

"DELETE FROM ID (ID) VALUES ('".$id."')"

错了.

应该是

DELETE FROM table_name
WHERE some_column=some_value;

.因此,将您的声明更改为

. So, change your statement to

DELETE FROM ID WHERE ID='$id'

建议

  • 您应使用 POST 方法执行操作,这将导致数据编辑.
  • 您应该检查输入,确保其中不包含SQL语句.一个好方法是使用 $ stuff = mysql_real_escape_string($ _ GET ["stuff"]).
  • Suggestions

    • You should use POST method for action which will result in data edit.
    • You should check the input, make sure it did not contain SQL statement. A good way is to use $stuff = mysql_real_escape_string($_GET["stuff"]).
    • 这篇关于使用PHP从MySQL数据库删除数据的表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆