Home  >  Article  >  Backend Development  >  PHP job addition, deletion, modification and check

PHP job addition, deletion, modification and check

WBOY
WBOYOriginal
2023-05-07 09:50:071044browse

PHP (Hypertext Preprocessor) is an open source server-side scripting language, especially suitable for web development. It can be embedded into HTML and is easy to operate, making web development more efficient and convenient. Therefore, PHP becomes an integral part of web development. This article will focus on PHP to discuss the addition, deletion, modification and query operations of PHP.

1. Review of PHP basic knowledge

Before operating PHP, we need to recall some knowledge about PHP’s basic syntax and environment:

  1. PHP’s variables and Data type

In PHP, variables start with the $ symbol. The data types of variables include the following:

String (string), numerical value (integer), floating point number ( float), boolean, array, object, null.

  1. Control structure

It is very important to master the control structure, among which if statements and for loops are the most basic control structures. The if statement can execute the corresponding block of code based on given conditions, while the for loop can repeatedly execute a block of code.

  1. Function

A function is a way of organizing code that accepts parameters, performs calculations, and returns a result. In PHP, we can customize a function and call it by name and variables.

2. PHP CRUD

In actual Web development, CRUD is a necessary function. Below we will introduce the operations of PHP from the four aspects of adding, deleting, modifying and checking.

(1) Add data

There are two main ways to add data in PHP, one is to enter data manually, and the other is to submit data through a form. However, either way, you need to connect to the database. Next, let's take a look at an example of manual input.

1. Manually insert data

<?php
$servername = "localhost";//数据库地址,这里指本地
$username = "yourusername";//登录数据库的用户名
$password = "yourpassword";//登录数据库的密码
$dbname = "yourdatabasename";//连接的数据库名称

// 创建连接
$conn = mysqli_connect($servername, $username, $password, $dbname);
// 检测连接
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

// SQL插入语句
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')";

if (mysqli_query($conn, $sql)) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

mysqli_close($conn);
?>

The above code involves the knowledge of connecting to the database and SQL statements. We need to create a connection object $conn, and then call the mysqli_query function to execute the insert statement. The variable $conn needs to use the mysqli_close function to close the connection after use.

2. Form method to insert data

The form method is a more practical method of inserting data. We can define the input data type and which table to insert data into in the form. Then receive the data through php script, and finally store the data in the database.

The sample code is as follows:

<!DOCTYPE html>
<html>
<body>

<form action="insert.php" method="post">
  First name:<br>
  <input type="text" name="fname">
  <br>
  Last name:<br>
  <input type="text" name="lname">
  <br>
  Email:<br>
  <input type="text" name="email">
  <br><br>
  <input type="submit" value="Submit">
</form> 

</body>
</html>

Among them, the "action" attribute defines the file name of the php script (such as insert.php below), and the "method" attribute defines the data submission method as POST , the input box in the form is defined by the "name" attribute, and the final submit button is also a form through which we can submit data to the corresponding php script.

In the insert.php file we need to accept variables and insert them into the database. The sample code is as follows:

<?php
$servername = "localhost";
$username = "yourusername";
$password = "yourpassword";
$dbname = "yourdatabasename";

// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);

// 检测连接
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// 接收参数
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];

// 插入数据
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('$fname', '$lname', '$email')";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

(2) Deleting data

Deleting data and adding data Similarly, you also need to establish a database connection and then execute the SQL statement.

The following is an example:

<?php
$servername = "localhost";
$username = "yourusername";
$password = "yourpassword";
$dbname = "yourdatabasename";

// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);

// 检测连接
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// 删除数据
$sql = "DELETE FROM MyGuests WHERE id=3";

if ($conn->query($sql) === TRUE) {
    echo "Record deleted successfully";
} else {
    echo "Error deleting record: " . $conn->error;
}

$conn->close();
?>

(3) Modify data

Modifying data also requires establishing a database connection and executing SQL statements. However, it should be noted that we need to specify the data rows to be modified, otherwise the data of the entire table will be modified together, causing unnecessary consequences.

Such as the following sample code:

<?php
$servername = "localhost";
$username = "yourusername";
$password = "yourpassword";
$dbname = "yourdatabasename";

// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);

// 检测连接
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// 修改数据
$sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2";

if ($conn->query($sql) === TRUE) {
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . $conn->error;
}

$conn->close();
?>

(4) Querying data

Querying data is our most commonly used method, which requires establishing a connection and executing a SELECT statement. The query results will return an array, and we can perform various operations on this array.

The following code is a simple query example:

<?php
$servername = "localhost";
$username = "yourusername";
$password = "yourpassword";
$dbname = "yourdatabasename";

// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);

// 检测连接
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// 查询数据
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // 输出数据
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
    }
} else {
    echo "0 results";
}

$conn->close();
?>

In this sample code, we use the SELECT statement to query the id, firstname and lastname data from the MyGuests data table. When the query result is not empty, we traverse through a while loop and output each piece of data.

3. Summary

In the process of Web application development, adding, deleting, modifying and checking are the most basic and important operations. Through the introduction of this article, we can understand the application of PHP in operating these four basic methods. Of course, to be proficient in these basic methods, you also need to have an in-depth mastery of MySQL syntax, basic knowledge of PHP and certain Web front-end knowledge. I hope this article will be helpful to your subsequent study.

The above is the detailed content of PHP job addition, deletion, modification and check. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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
Previous article:php delete relative filesNext article:php delete relative files