PHP development basic tutorial: batch and specific deletion of users

Encapsulate database connection function

In a real project, we use the host and user name , passwords, and libraries are all written in the configuration file.
If it is written hard in the code, if the relevant information of the database server changes, it is obviously not in line with the programmer's thinking to modify all the code.

In addition, in every page that needs to connect to the database. We all need to write connections, judge errors, and set character sets, which is too troublesome. And it is not conducive to reusing these codes.

We can use the include series of functions mentioned before to achieve our goal. The example diagram is as follows:

118.png

Therefore, we can make a configuration file config.php. Set all the configurations that need to be used as constants. The code is as follows:

<?php
//数据库服务器
define('DB_HOST', 'localhost');

//数据库用户名
define('DB_USER', 'root');

//数据库密码
define('DB_PWD', 'secret');

//库名
define('DB_NAME', 'book');

//字符集
define('DB_CHARSET', 'utf8');

We will extract the connection.php page , when you need to connect to the database in the future, you only need to include the connection.php file. The code is as follows:

<?php

include 'config.php';

$conn = mysqli_connect(DB_HOST, DB_USER, DB_PWD, DB_NAME);

if (mysqli_errno($conn)) {
    mysqli_error($conn);
    exit;
}

mysqli_set_charset($conn, DB_CHARSET);

We can realize the database connection by directly including the connection.php file in each file in the future:

include 'connection.php';

Complete the above preparations, and then complete the deletion of data


Batch and designated deletion of users

Before deleting, you need to determine whether to delete a single row Data or delete multiple rows of data.

  • A single line writes the corresponding ID to the delete.php file by passing parameters through get.

  • And multiple deletions pass the corresponding ID to the delete.php page through POST.

  • If neither of these two is met, then we can regard the data as illegal.

if (is_array($_POST['id'])) {

    $id = join(',', $_POST['id']);

} elseif (is_numeric($_GET['id'])) {

    $id = (int) $_GET['id'];

} else {
    echo '数据不合法';
    exit;
}


##Combined SQL statements

We have previously explained to you in the MySQL chapter that you can use the in sub-statement when deleting.

Similarly here, we can use the in sub-statement to achieve the effect.

The join function changes the id passed by the multi-select deletion into the format of 3, 4, 5. The final effect of the SQL statement for multi-select deletion is:

delete from user where id in(3,4,5,6,8);

And single-select deletion The effect of the statement is:

delete from user where id in(3)

In this way, we achieve the single-selection and multi-selection adaptive effects.

$sql = "delete from user where id in($id)";

The final complete code demonstration is as follows:

<?php

include 'connection.php';

if (is_array($_POST['id'])) {

    $id = join(',', $_POST['id']);

} elseif (is_numeric($_GET['id'])) {

    $id = (int) $_GET['id'];

} else {
    echo '数据不合法';
    exit;
}

$sql = "delete from user where id in($id)";

$result = mysqli_query($conn, $sql);

if ($result) {
    echo '删除成功';
} else {
    echo '删除失败';
}


Continuing Learning
||
<?php include 'connection.php'; if (is_array($_POST['id'])) { $id = join(',', $_POST['id']); } elseif (is_numeric($_GET['id'])) { $id = (int) $_GET['id']; } else { echo '数据不合法'; exit; } $sql = "delete from user where id in($id)"; $result = mysqli_query($conn, $sql); if ($result) { echo '删除成功'; } else { echo '删除失败'; }
submitReset Code