Home Backend Development PHP Problem php delete database in form

php delete database in form

May 06, 2023 pm 10:04 PM

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8 JIT (Just-In-Time) Compilation: How it improves performance. PHP 8 JIT (Just-In-Time) Compilation: How it improves performance. Mar 25, 2025 am 10:37 AM

PHP 8's JIT compilation enhances performance by compiling frequently executed code into machine code, benefiting applications with heavy computations and reducing execution times.

OWASP Top 10 PHP: Describe and mitigate common vulnerabilities. OWASP Top 10 PHP: Describe and mitigate common vulnerabilities. Mar 26, 2025 pm 04:13 PM

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

PHP Secure File Uploads: Preventing file-related vulnerabilities. PHP Secure File Uploads: Preventing file-related vulnerabilities. Mar 26, 2025 pm 04:18 PM

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

PHP Encryption: Symmetric vs. asymmetric encryption. PHP Encryption: Symmetric vs. asymmetric encryption. Mar 25, 2025 pm 03:12 PM

The article discusses symmetric and asymmetric encryption in PHP, comparing their suitability, performance, and security differences. Symmetric encryption is faster and suited for bulk data, while asymmetric is used for secure key exchange.

PHP Authentication & Authorization: Secure implementation. PHP Authentication & Authorization: Secure implementation. Mar 25, 2025 pm 03:06 PM

The article discusses implementing robust authentication and authorization in PHP to prevent unauthorized access, detailing best practices and recommending security-enhancing tools.

How do you retrieve data from a database using PHP? How do you retrieve data from a database using PHP? Mar 20, 2025 pm 04:57 PM

Article discusses retrieving data from databases using PHP, covering steps, security measures, optimization techniques, and common errors with solutions.Character count: 159

What is the purpose of mysqli_query() and mysqli_fetch_assoc()? What is the purpose of mysqli_query() and mysqli_fetch_assoc()? Mar 20, 2025 pm 04:55 PM

The article discusses the mysqli_query() and mysqli_fetch_assoc() functions in PHP for MySQL database interactions. It explains their roles, differences, and provides a practical example of their use. The main argument focuses on the benefits of usin

PHP CSRF Protection: How to prevent CSRF attacks. PHP CSRF Protection: How to prevent CSRF attacks. Mar 25, 2025 pm 03:05 PM

The article discusses strategies to prevent CSRF attacks in PHP, including using CSRF tokens, Same-Site cookies, and proper session management.

See all articles