我一直在尝试为项目创建一个CRUD,一切都很好,除了更新部分。当我单击指定行的编辑的href时,它确实出现在输入字段中。但是,当我点击editbtn时,特定行的变量没有更新,我被重定向到我所在的位置,而url给了我?user = edited意味着它已经完成了决定,但由于某种原因它们没有更新。 在我的数据库中有一个表(用户),其中包含以下行user_id,user_uid,user_email,user_pwd,user_status和user_level。我对php很新,所以我希望你能找出我的错误。
与数据库的连接
dbh.inc.php
<?php
$dbServername = "localhost";
$dbUsername = "username";
$dbPassword = "*******";
$dbName = "username_Project";
$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName );
?>
索引
admin_panel_users.php
<?php
session_start();
include 'includes/dbh.inc.php';
if (isset($_GET['edit'])) {
$id = $_GET['edit'];
$update = true;
$record= mysqli_query($conn, "SELECT * FROM users WHERE user_id=$id");
if ($record == 1 ) {
$n = mysqli_fetch_array($record);
$uid = $n['user_uid'];
$email = $n['user_email'];
$pwd = $n['user_pwd'];
$status = $n['user_status'];
$level = $n['user_level'];
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
.....
</head>
<body>
<?php
$result = mysqli_query($conn,"SELECT * FROM users");?>
<table border='2'>
<tr>
<th>Username</th>
<th>Email</th>
<th>Password</th>
<th>Status</th>
<th>Level</th>
<th>Actions</th>
</tr>
<?php while($row = mysqli_fetch_array($result)){?>
<tr>
<td><?php echo $row['user_uid'];?> </td>
<td><?php echo $row['user_email'];?></td>
<td><?php echo $row['user_pwd'];?></td>
<td><?php echo $row['user_status'];?></td>
<td><?php echo $row['user_level'];?></td>
<td>
<a href="admin_panel_users.php?edit=<?php echo $row['user_id']; >"
class="edit_btn">
<span class="glyphicon glyphicon-pencil"></span></a>
<a href="includes/deleteusers.inc.php?user_id=<?php echo $row['user_id'];>">
<span class="glyphicon glyphicon-trash"></span></a></td>
</tr>
<?php } ?>
<tr>
<form class="something" action="includes/addusers.inc.php" method="POST">
<td><input type="text" name="uid" class="uid" placeholder="Username"
value="<?php echo $uid; ?>"></td>
<td><input type="text" name="email" class="email" placeholder="Email"
value="<?php echo $email; ?>"></td>
<td><input type="text" name="pwd" class="pwd" placeholder="Password"
value="<?php echo $pwd; ?>"></td>
<td><input type="text" name="status" class="status" placeholder="Status"
value="<?php echo $status; ?>"></td>
<td><input type="text" name="level" class="level" placeholder="Level"
value="<?php echo $level; ?>"></td>
<td>
<?php if ($update == true): ?>
<button type="submit" name="update" class="updatebtn">
<span class="glyphicon-pencil"></span> </button>
<?php else: ?>
<button type="submit" name="submit8" class="addbtnuser">
<span class ="glyphicon-plus"></span> </button>
<?php endif ?>
</td>
</form>
</tr>";
</table>";
<?php mysqli_close($conn); ?>
</body>
</html>
功能
addusers.inc.php
<?php
include 'dbh.inc.php';
$uid = "";
$email = "";
$pwd = "";
$status = "";
$level = "";
$id = 0;
$update = false;
if (isset($_POST['submit8'])){
//INSERTS INTO
......
}
if (isset($_POST['update'])) {
$uid = mysqli_real_escape_string( $conn , $_POST['uid']);
$email = mysqli_real_escape_string( $conn , $_POST['email']);
$pwd = mysqli_real_escape_string( $conn , $_POST['pwd']);
$status = mysqli_real_escape_string( $conn , $_POST['status']);
$level = mysqli_real_escape_string( $conn , $_POST['level']);
$sql = "UPDATE users SET user_uid='$uid', user_email='$email',
user_pwd='$pwd', user_status='$status', user_level='$level' WHERE
user_id=$id";
mysqli_query($conn, $sql);
header ("Location: ../admin_panel_users.php?user=edited");
exit();
}
else{
header("Location: ../admin_panel_users.php");
exit();
}
答案 0 :(得分:0)
来自你的addusers.inc.php,上面写着:
$sql = "UPDATE users SET user_uid='$uid', user_email='$email',
user_pwd='$pwd', user_status='$status', user_level='$level' WHERE
user_id=$id";
看来你没有获得$ id变量,以便更新表中的特定行。你在addusers.inc.php的第8行默认为0。因此,它根本不会更新任何行,因为表行从1开始并递增。
在第8行,将其更改为
$id = $_GET['edit']
,因为您已将其作为GET参数传递到此处:
<a href="admin_panel_users.php?edit=<?php echo $row['user_id']; >"
class="edit_btn">
答案 1 :(得分:0)
您的SQL文本包括:
WHERE user_id = $id
$id
设置为0,因此相当于
WHERE user_id = 0