使用php连接mysql完成以下功能,要求先用workbench的model创建mysql表:user,child user: id,name,sex,age,address,password child: id, parent_id, name, birthdate, sex, time,password 1,管理员登录,添加用户user信息(管理员不需要创建表,验证用户名和密码就可以)2,用户登录, (1) 输入姓,例如王,为child取一个名字,可以多次取名字 (2) 把上一步的名字连同自己的id,保存到孩子信息表中。3,child登陆(假设长大以后),查看个人信息用php实现一个人口管理系统,可以在原来表的基础上,添加医生表,疫苗表,疫苗接种表,child登录可以查自己疫苗接种信息
时间: 2023-06-26 20:06:43 浏览: 94
PHP 连接mysql数据库做简单的登录页面,实现用户的登录注册功能
以下是用PHP连接MySQL实现人口管理系统的代码:
1. 管理员登录,添加用户user信息
```php
<?php
$servername = "localhost";
$username = "admin";
$password = "admin123";
$dbname = "population";
// 创建连接
$conn = mysqli_connect($servername, $username, $password, $dbname);
// 检查连接是否成功
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// 获取管理员输入的用户名和密码
$admin_username = $_POST["admin_username"];
$admin_password = $_POST["admin_password"];
// 验证管理员用户名和密码是否正确
$sql = "SELECT * FROM admin WHERE username='$admin_username' AND password='$admin_password'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) == 1) {
// 如果验证成功,处理添加用户信息的请求
$username = $_POST["username"];
$sex = $_POST["sex"];
$age = $_POST["age"];
$address = $_POST["address"];
$password = $_POST["password"];
// 将用户信息插入到user表中
$sql = "INSERT INTO user (name, sex, age, address, password) VALUES ('$username', '$sex', '$age', '$address', '$password')";
if (mysqli_query($conn, $sql)) {
echo "User added successfully!";
} else {
echo "Error adding user: " . mysqli_error($conn);
}
} else {
// 如果验证失败,提示管理员用户名或密码错误
echo "Invalid username or password!";
}
mysqli_close($conn);
?>
```
2. 用户登录,输入姓并为child取名字,保存到孩子信息表中
```php
<?php
$servername = "localhost";
$username = "admin";
$password = "admin123";
$dbname = "population";
// 创建连接
$conn = mysqli_connect($servername, $username, $password, $dbname);
// 检查连接是否成功
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// 获取用户输入的用户名和密码
$username = $_POST["username"];
$password = $_POST["password"];
// 验证用户用户名和密码是否正确
$sql = "SELECT * FROM user WHERE name='$username' AND password='$password'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) == 1) {
// 如果验证成功,处理输入姓并为child取名字的请求
$surname = $_POST["surname"];
// 生成child的名字
$sql = "SELECT COUNT(*) AS count FROM child WHERE name LIKE '$surname%'";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$count = $row["count"] + 1;
$child_name = $surname . $count;
// 将child信息插入到child表中
$parent_id = mysqli_fetch_assoc($result)["id"];
$birthdate = $_POST["birthdate"];
$sex = $_POST["sex"];
$time = $_POST["time"];
$password = $_POST["password"];
$sql = "INSERT INTO child (parent_id, name, birthdate, sex, time, password) VALUES ('$parent_id', '$child_name', '$birthdate', '$sex', '$time', '$password')";
if (mysqli_query($conn, $sql)) {
echo "Child added successfully!";
} else {
echo "Error adding child: " . mysqli_error($conn);
}
} else {
// 如果验证失败,提示用户用户名或密码错误
echo "Invalid username or password!";
}
mysqli_close($conn);
?>
```
3. child登陆,查看个人信息和疫苗接种信息
```php
<?php
$servername = "localhost";
$username = "admin";
$password = "admin123";
$dbname = "population";
// 创建连接
$conn = mysqli_connect($servername, $username, $password, $dbname);
// 检查连接是否成功
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// 获取child输入的用户名和密码
$child_name = $_POST["child_name"];
$child_password = $_POST["child_password"];
// 验证child用户名和密码是否正确
$sql = "SELECT * FROM child WHERE name='$child_name' AND password='$child_password'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) == 1) {
// 如果验证成功,输出child的个人信息和疫苗接种信息
$row = mysqli_fetch_assoc($result);
$id = $row["id"];
$parent_id = $row["parent_id"];
$birthdate = $row["birthdate"];
$sex = $row["sex"];
$time = $row["time"];
echo "ID: " . $id . "<br>";
echo "Parent ID: " . $parent_id . "<br>";
echo "Birthdate: " . $birthdate . "<br>";
echo "Sex: " . $sex . "<br>";
echo "Time: " . $time . "<br>";
// 查询child的疫苗接种信息
$sql = "SELECT vaccine.name, vaccine.date FROM vaccine JOIN vaccination ON vaccine.id=vaccination.vaccine_id WHERE vaccination.child_id='$id'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
echo "<h2>Vaccination Records:</h2>";
while ($row = mysqli_fetch_assoc($result)) {
$name = $row["name"];
$date = $row["date"];
echo $name . " on " . $date . "<br>";
}
} else {
echo "No vaccination record found!";
}
} else {
// 如果验证失败,提示child用户名或密码错误
echo "Invalid username or password!";
}
mysqli_close($conn);
?>
```
注意:以上代码仅为示例,实际使用时需要根据具体需求进行修改和完善。同时,为了安全起见,应该对用户输入进行严格的验证和过滤,防止SQL注入等攻击。
阅读全文