亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

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
//數(shù)據(jù)庫(kù)服務(wù)器
define('DB_HOST', 'localhost');

//數(shù)據(jù)庫(kù)用戶名
define('DB_USER', 'root');

//數(shù)據(jù)庫(kù)密碼
define('DB_PWD', 'secret');

//庫(kù)名
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 '數(shù)據(jù)不合法';
    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 '數(shù)據(jù)不合法';
    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 '數(shù)據(jù)不合法'; exit; } $sql = "delete from user where id in($id)"; $result = mysqli_query($conn, $sql); if ($result) { echo '刪除成功'; } else { echo '刪除失敗'; }
submitReset Code