phpstudy数据库增删改查登录注册
时间: 2023-09-28 14:07:52 浏览: 90
基于php+mysql+html超市商品管理系统(源码+数据库+论文)
好的,以下是一个简单的 PHP + MySQL 实现增删改查登录注册的示例:
## 连接数据库
```php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// 创建连接
$conn = mysqli_connect($servername, $username, $password, $dbname);
// 检查连接
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
```
## 插入数据
```php
$sql = "INSERT INTO users (username, password) VALUES ('$username', '$password')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
```
## 更新数据
```php
$sql = "UPDATE users SET password='$new_password' WHERE username='$username'";
if (mysqli_query($conn, $sql)) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($conn);
}
```
## 删除数据
```php
$sql = "DELETE FROM users WHERE username='$username'";
if (mysqli_query($conn, $sql)) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . mysqli_error($conn);
}
```
## 查询数据
```php
$sql = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// 登录成功
} else {
// 登录失败
}
```
## 注册
```php
$sql = "SELECT * FROM users WHERE username='$username'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// 用户名已存在
} else {
$sql = "INSERT INTO users (username, password) VALUES ('$username', '$password')";
if (mysqli_query($conn, $sql)) {
echo "注册成功";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
```
以上示例仅供参考,实际使用中还需要进行数据过滤和安全性处理。
阅读全文