在PHP中更新查询';It’没有错,但它不起作用
我不知道这有什么错,没有错误,但是更新查询不起作用。我不知道这是最新的代码还是旧代码。请告诉我如何解决这个问题。谢谢,这是密码。第一个代码是server.php 结构如下:在PHP中更新查询';It’没有错,但它不起作用,php,html,Php,Html,我不知道这有什么错,没有错误,但是更新查询不起作用。我不知道这是最新的代码还是旧代码。请告诉我如何解决这个问题。谢谢,这是密码。第一个代码是server.php 结构如下: <?php session_start(); $username = ""; $password = ""; $lastname = ""; $firstname = ""; $id = 0;
<?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>
帐户设置
用户名
暗语
姓氏
名字
行动
我发现表单输入和$POST键的名称不匹配。比如说
<input type="hidden" name="text_id" value="<?php echo $id; ?>">
您的脚本甚至有使用update语句添加检查的风险。应该有一个error@RiggsFolly如何在代码中应用该代码;(@Akintunde我已经在w3中检查过了,但它是正确的,我不知道有什么不对update查询中的where子句的id在引号中,但它是一个整数。将变量强制转换为整数(您确实应该改用参数化查询)并删除引号。不确定这是否是问题,但这是问题。如果更新格式正确,但where子句导致未更新记录,则数据库不会报告此错误。很抱歉,我忘了更改,但我知道该语法。但它也不起作用…:(等等,什么是正确的语法?textbot的名称或我数据库中的行?谢谢:)