我不知道这有什么不对,没有错误,但它没有解决更新查询。我不知道它是最新的代码还是旧代码。请告诉我如何解决这个问题。谢谢,这是代码。第一个代码是server.php
这里的结构:
<?php
session_start();
$username = "";
$password = "";
$lastname = "";
$firstname = "";
$id = 0;
$edit_state = false;
//connect to the database
$db = mysqli_connect('localhost', 'root', '', 'login');
// button is clicked
if (isset($_POST['save'])) {
$username = $_POST['text_username'];
$password = $_POST['text_password'];
$lastname = $_POST['text_lastname'];
$firstname = $_POST['text_firstname'];
//adding data in to database
$query = "INSERT INTO users (username, password, lastname, firstname) values ('$username', '$password', '$lastname', '$firstname')";
mysqli_query($db, $query);
$_SESSION['msg'] = "Account Saved!";
header('location: acc-settings.php');
}
//update records in the database
if (isset($_POST['update'])) {
$username = mysqli_real_escape_string($_POST['text_username']);
$password = mysqli_real_escape_string($_POST['password']);
$lastname = mysqli_real_escape_string($_POST['lastname']);
$firstname = mysqli_real_escape_string($_POST['firstname']);
$id = mysqli_real_escape_string($_POST['id']);
mysqli_query($db, "UPDATE users SET username = '$username', password = '$password', lastname = '$lastname', firstname = '$firstname' where id='$id'");
$_SESSION['msg'] = "Account Updated!";
header('location: acc-settings.php');
}
//retrieve records
$results = mysqli_query($db, "SELECT * FROM users"); ?>
这是acc-settings.php
<?php include 'server.php';
//fetching the record
if (isset($_GET['edit'])) {
$id = $_GET['edit'];
$rec = mysqli_query($db, "SELECT * FROM users where id=$id");
$record = mysqli_fetch_array($rec);
$username = $record['username'];
$password = $record['password'];
$lastname = $record['lastname'];
$firstname = $record['firstname'];
$id = $record['id'];
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Account Settings</title>
<link rel="stylesheet" type="text/css" href="css/acc-style.css">
</head>
<body>
<?php if (isset($_SESSION['msg'])): ?>
<div class="msg">
<?php
echo $_SESSION['msg'];
unset($_SESSION['msg']);
?>
</div>
<?php endif ?>
<table>
<thead>
<tr>
<th>Username</th>
<th>Password</th>
<th>Lastname</th>
<th>Firstname</th>
<th colspan="2">Action</th>
</tr>
</thead>
<tbody>
<?php while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<td><?php echo $row['username']; ?></td>
<td><?php echo $row['password']; ?></td>
<td><?php echo $row['lastname']; ?></td>
<td><?php echo $row['firstname']; ?></td>
<td>
<a href="acc-settings.php?edit=<?php echo $row['id']; ?>">Edit</a>
</td>
<td>
<a href="#">Delete</a>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<form method="post" action="#">
<input type="hidden" name="text_id" value="<?php echo $id; ?>">
<div class="input-group">
<label>Username</label>
<input type="text" name="text_username" value="<?php echo
$username; ?>">
</div>
<div class="input-group">
<label>Password</label>
<input type="text" name="text_password" value="<?php echo
$password; ?>">
</div>
<div class="input-group">
<label>Lastname</label>
<input type="text" name="text_lastname" value="<?php echo
$lastname; ?>">`enter code here`
</div>
<div class="input-group">
<label>Firstname</label>
<input type="text" name="text_firstname" value="<?php echo
$firstname; ?>">
</div>
<div class="input-group">
<?php if ($edit_state == true): ?>
<button type="submit" name="save" class="btn">Save</button>
<?php else: ?>
<button type="submit" name="update" class="btn">Update</button>
<?php endif ?>
</div>
</form>
</body>
</html>
请告诉我这段代码有什么问题:(如果问题不明白请评论下来。非常感谢你!
答案 0 :(得分:1)
我看到表单输入和$ _POST键的名称不匹配。例如
<input type="hidden" name="text_id" value="<?php echo $id; ?>">
name =&#34; text_id&#34;,但$ _POST [&#39; id&#39;]使用,而不是$ _POST [&#39; text_id&#39;]
许多其他领域也有同样的问题。