Home >Backend Development >PHP Tutorial >PHP and UniApp implement basic operations of adding, deleting, modifying and checking data

PHP and UniApp implement basic operations of adding, deleting, modifying and checking data

WBOY
WBOYOriginal
2023-07-04 18:51:071441browse

PHP and UniApp realize the basic operations of adding, deleting, modifying and checking data

  1. Introduction
    In Web development, adding, deleting, modifying and checking data is a very basic and common operation. As a commonly used server-side scripting language, PHP can interact with the database to implement data addition, deletion, modification and query operations. UniApp is a cross-platform application development framework that can develop iOS and Android applications at the same time. This article will introduce how to use PHP and UniApp to implement the basic operations of adding, deleting, modifying and checking data.
  2. Database configuration
    First, we need to configure the database connection in the PHP code. Assuming we use a MySQL database, the following is a simple database connection configuration example:
$servername = "localhost";
$username = "root";
$password = "your_password";
$dbname = "your_database";

$conn = new mysqli($servername, $username, $password, $dbname);

// 检测连接是否成功
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}
  1. Insertion operation of data
    To implement the insertion operation of data, we need to send a request in UniApp to the server side and receive and process the request in PHP code. The following is an example of a simple insert operation:
// 在UniApp中发送请求
uni.request({
    url: 'http://your_domain.com/insert.php',
    method: 'POST',
    data: {
        name: 'John',
        age: 25
    },
    success: function(res) {
        console.log('插入成功', res.data);
    },
    fail: function(err) {
        console.log('插入失败', err);
    }
});
// 在insert.php中处理请求
$name = $_POST['name'];
$age = $_POST['age'];

$sql = "INSERT INTO users (name, age) VALUES ('$name', '$age')";

if ($conn->query($sql) === TRUE) {
    echo "插入成功";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
  1. Data query operation
    To implement the data query operation, we can send a request to the server in UniApp and The request is received and processed in PHP code. The following is an example of a simple query operation:
// 在UniApp中发送请求
uni.request({
    url: 'http://your_domain.com/select.php',
    method: 'GET',
    success: function(res) {
        console.log('查询成功', res.data);
    },
    fail: function(err) {
        console.log('查询失败', err);
    }
});
// 在select.php中处理请求
$sql = "SELECT * FROM users";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    $rows = array();
    while($row = $result->fetch_assoc()) {
        $rows[] = $row;
    }
    echo json_encode($rows);
} else {
    echo "0 结果";
}

$conn->close();
  1. Data update operation
    To implement the data update operation, we can send a request to the server in UniApp and The request is received and processed in PHP code. The following is an example of a simple update operation:
// 在UniApp中发送请求
uni.request({
    url: 'http://your_domain.com/update.php',
    method: 'POST',
    data: {
        id: 1,
        name: 'John',
        age: 30
    },
    success: function(res) {
        console.log('更新成功', res.data);
    },
    fail: function(err) {
        console.log('更新失败', err);
    }
});
// 在update.php中处理请求
$id = $_POST['id'];
$name = $_POST['name'];
$age = $_POST['age'];

$sql = "UPDATE users SET name='$name', age='$age' WHERE id=$id";

if ($conn->query($sql) === TRUE) {
    echo "更新成功";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
  1. Data deletion operation
    To implement the data deletion operation, we can send a request to the server in UniApp and The request is received and processed in PHP code. The following is an example of a simple deletion operation:
// 在UniApp中发送请求
uni.request({
    url: 'http://your_domain.com/delete.php',
    method: 'POST',
    data: {
        id: 1
    },
    success: function(res) {
        console.log('删除成功', res.data);
    },
    fail: function(err) {
        console.log('删除失败', err);
    }
});
// 在delete.php中处理请求
$id = $_POST['id'];

$sql = "DELETE FROM users WHERE id=$id";

if ($conn->query($sql) === TRUE) {
    echo "删除成功";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
  1. Summary
    This article introduces how to use PHP and UniApp to implement the basic operations of adding, deleting, modifying and checking data. By sending a request to the server side, and receiving and processing the request in PHP code, we can easily implement operations on the database. I hope this article can help you in actual development.

The above is the detailed content of PHP and UniApp implement basic operations of adding, deleting, modifying and checking data. 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