dmin_personnel id,int,password,varchar,name,varchar,role,varchar,department,varchar,sex,varchar,email,varchar,telephon,varchar添加admin_personnel表数据,下面代码错误在哪 <?php // 连接数据库 include "conn.php"; // 获取请求参数 $id = $_POST['id']; $name = $_POST['name']; $password = $_POST['password']; $sex = $_POST['sex']; $role = $_POST['role']; $department = $_POST['department']; $email = $_POST['email']; $telephone = $_POST['telephone']; // 创建预处理语句 $stmt = mysqli_prepare($conn, "INSERT INTO admin_personnel (id, name, password, sex, role, department,email,telephone) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"); mysqli_stmt_bind_param($stmt, "isssssss", $id, $name, $password, $sex, $role, $department, $email, $telephone); // 执行预处理语句 if (mysqli_stmt_execute($stmt)) { // 获取插入数据的ID $insert_id = mysqli_insert_id($conn); // 查询插入的数据 $result = mysqli_query($conn, "SELECT * FROM admin_personnel WHERE id=\"$insert_id\""); $row = mysqli_fetch_assoc($result); // 返回响应数据 $response = array( "data" => $row, "meta" => array( "msg" => "用户创建成功", "status" => 201 ) ); echo json_encode($response); } else { // 返回错误信息 $response = array( "meta" => array( "msg" => "用户创建失败", "status" => 500 ) ); echo json_encode($response); } // 关闭连接 mysqli_stmt_close($stmt); mysqli_close($conn); ?>
时间: 2023-12-09 12:06:38 浏览: 128
代码中存在以下错误:
1. admin_personnel 表中的字段名和数据类型没有对应上。
2. $telephone 变量名拼写错误,应该为 $telephon。
修改后的代码如下:
```php
<?php
// 连接数据库
include "conn.php";
// 获取请求参数
$id = $_POST['id'];
$name = $_POST['name'];
$password = $_POST['password'];
$sex = $_POST['sex'];
$role = $_POST['role'];
$department = $_POST['department'];
$email = $_POST['email'];
$telephone = $_POST['telephone'];
// 创建预处理语句
$stmt = mysqli_prepare($conn, "INSERT INTO admin_personnel (id, password, name, role, department, sex, email, telephon) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
mysqli_stmt_bind_param($stmt, "isssssss", $id, $password, $name, $role, $department, $sex, $email, $telephone);
// 执行预处理语句
if (mysqli_stmt_execute($stmt)) {
// 获取插入数据的ID
$insert_id = mysqli_insert_id($conn);
// 查询插入的数据
$result = mysqli_query($conn, "SELECT * FROM admin_personnel WHERE id=\"$insert_id\"");
$row = mysqli_fetch_assoc($result);
// 返回响应数据
$response = array(
"data" => $row,
"meta" => array(
"msg" => "用户创建成功",
"status" => 201
)
);
echo json_encode($response);
} else {
// 返回错误信息
$response = array(
"meta" => array(
"msg" => "用户创建失败",
"status" => 500
)
);
echo json_encode($response);
}
// 关闭连接
mysqli_stmt_close($stmt);
mysqli_close($conn);
?>
```
阅读全文