Home > Backend Development > PHP Problem > php delete database in form

php delete database in form

王林
Release: 2023-05-06 22:04:06
Original
577 people have browsed it

With the development of the Internet, the construction of websites has become more and more common, and there are more and more scenarios that require database operations. As a very popular programming language, PHP is also widely used in website development, especially in database operations. Due to its high development efficiency, low learning threshold, and easy mastery, it has been widely used. application. In actual development, how to operate the database quickly and efficiently has become a key issue that many PHP developers pay attention to.

This article will introduce how to delete the database in the form in PHP, mainly including the following:

  1. Delete operation of the database
  2. The association between the form and the database
  3. Steps to delete the database in the form
  4. Example analysis

1. Database deletion operation

In PHP, for the database deletion operation, we generally Use SQL statements to operate. The following is the deletion statement format of the SQL statement:

DELETE FROM table_name WHERE condition;
Copy after login

Among them, table_name is the name of the table to be deleted, and condition is the condition for deletion. If there are no conditions, the entire table will be deleted, which is a very dangerous operation. Therefore, be careful when using the DELETE statement.

2. Association between forms and databases

In website development, forms are generally used to collect data submitted by users and then save the data to the database. The connection between the form and the database is achieved through PHP scripts. The specific process is: first, we write the form code in HTML, and then use PHP script to process it. The PHP script stores the collected form data in the database or retrieves the data from the database and provides it to the user. The following is a simple form code example:

<form method="post" action="submit.php">
  <label for="name">姓名:</label>
  <input type="text" id="name" name="name"><br><br>
  <label for="email">邮箱:</label>
  <input type="text" id="email" name="email"><br><br>
  <label for="message">信息:</label>
  <textarea id="message" name="message"></textarea><br><br>
  <input type="submit" value="提交">
</form>
Copy after login

In the above code, we use the

3. Steps to delete the database in the form

Based on the above, we will further explore how to delete the database in the form. If we want to delete a record in the database, we need to go through the following steps:

  1. Add a delete button to the form and add a name attribute to it. For example, in the above form code, you can add a button named delete with the following code:
<input type="submit" value="删除" name="delete">
Copy after login
  1. Get the submitted form data in the PHP script.
$name = $_POST['name'];  // 获取姓名
$email = $_POST['email'];  // 获取邮箱
$message = $_POST['message'];  // 获取信息
Copy after login
  1. Construct an SQL statement to perform the deletion operation based on the submitted data.
if (isset($_POST['delete'])) {
  $name = $_POST['name'];
  $email = $_POST['email'];
  $message = $_POST['message'];
  $db = mysqli_connect('localhost', 'root', '', 'my_db');
  $sql = "DELETE FROM messages WHERE name='$name' AND email='$email' AND message='$message'";
  mysqli_query($db, $sql);
  mysqli_close($db);
  header("Location: index.php");  // 重定向到主页面
}
Copy after login

In this example, we use the mysqli_connect function to connect to the database, use the mysqli_query function to execute the SQL statement, and finally use the header function to redirect the page to the main page. It should be noted that when constructing SQL statements, you need to pay attention to security issues such as SQL injection.

4. Example analysis

We use an example to demonstrate how to delete the database in the form.

Suppose we now have a message board where users can leave messages. We need to save the messages submitted by users to the database. At the same time, we need to add a delete button. When the user clicks the delete button, the corresponding message can be deleted.

First, we create a data table named message in the MySQL database to store message information submitted by users. The data table structure is as follows:

CREATE TABLE `messages` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `email` varchar(255) NOT NULL,
  `message` text NOT NULL,
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
Copy after login

Next, let’s take a look at the code of the submit.php file, which is used to receive form information and store data in the database.

<?php
if (isset($_POST['submit'])) {
  $name = $_POST['name'];
  $email = $_POST['email'];
  $message = $_POST['message'];

  // 连接数据库
  $db = mysqli_connect('localhost', 'root', '', 'my_db');

  // 执行SQL语句,将数据存储到数据库中
  $sql = "INSERT INTO messages (name, email, message) VALUES ('$name', '$email', '$message')";
  mysqli_query($db, $sql);

  // 关闭数据库连接
  mysqli_close($db);

  // 重定向到主页面
  header("Location: index.php");
}
?>
Copy after login

In this example, we use the mysqli_connect function to connect to the database, use the mysqli_query function to execute the SQL statement, and finally use the header function to redirect the page to the main page. It should be noted that when constructing SQL statements, you need to pay attention to security issues such as SQL injection.

Let’s take a look at the code of the index.php file, which is used to display the message information submitted by the user and add a delete button.

';
  echo '

' . $name . '

'; echo '

' . $message . '

'; echo '

' . $email . ' • ' . $created_at . '

'; echo '
'; echo ''; echo ''; echo ''; echo '<input type="submit" value="删除" name="delete">'; echo '
'; echo '
'; } // 关闭数据库连接 mysqli_close($db); ?>
Copy after login

In this example, we first retrieve data from the database using the mysqli_fetch_assoc function, and then use HTML and PHP code to render the data onto the page. For each piece of data, we have added a delete button. When the user clicks the delete button, the delete code in the submit.php file will be called to delete the corresponding message from the database.

So far, we have introduced how to use PHP to delete the database in the form. As a powerful language, PHP can not only conveniently operate databases, but also quickly perform operations such as form processing and data rendering, so it is widely used in website development.

The above is the detailed content of php delete database in form. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template