修改用户信息



我无法修改我的用户信息。当我单击"保存"时,它会重新加载页面,但不会将其保存为新用户信息。我不知道是SQL注入的问题还是其他什么。以下是我的修改页面的外观图片:https://i.stack.imgur.com/KMWMi.jpg

<?php
session_start();
?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Modify user</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
<style type="text/css">
</style>
<?php
$get=$_GET['id'];
$form = "edituser.php?id=$get";
?>
</head>
<body>
<h2>Modify user</h2>
<form action="modifyuser.php" method="post">
<table>
<tr>
<td>
Name / Vorname:
</td><br>
<td>
<div class="form-group">
<input type="text" name="input_name"
<?php
if(isset($_POST['input_name'])) {
echo 'value="'.$_POST['input_name'].'"';
}
?>
>
</div>
</td>
</tr>
</table><br>
<?php
include "config.php";
if((isset($_POST['input_add']))&&($_POST['input_name']!="")){
if (isset($_GET['modify'])) {
$addusername = $_POST['input_name'];
$id = $_GET['modify'];
$modify_query = 'update benutzer 
set username="'.$addusername.'" 
where id="'.$id.'"';
if($link->query($modify_query)=== TRUE){
echo "User was successfully updated";
}else {
echo "Error updating the user";
}
}
$result = mysqli_query($link, $modify_query);
echo $modify_query;
header("Location: pannel.php");
}
echo "<button class='btn btn-success' name='input_add' onclick='return confirm("Do you want to modify the user?")';>Save</button>";
echo "&nbsp;";
echo "<a href='pannel.php' class='btn btn-default'>Go Back</a>";
?>
</form>
</body>
</html>

您的代码容易受到SQL注入的攻击。使用PDO,如下所述。但是,目前,请根据您自己的驱动程序修改SQL查询。其余的都一样。

现在,试着理解下面的代码。此外,我会建议你保持你的代码漂亮,干净和简短。

<?php
if($_POST){
$inputName = $_POST['input_name'];
$userId = $_POST['userid'];
$stmt = $pdo->prepare("UPDATE benutzer SET username = :uname WHERE id = :id");
$stmt-> bindValue(':uname', $inputName);
$stmt-> bindValue(':id', $userId);
$stmt-> execute();
if($stmt){
echo "User was successfully updated";
header("Location: pannel.php");
exit();
}else{
echo "Error updating the user";
}
}
?>

在您的表单中,使用onSubmit来确认返回,而不是使用PHP。通过这种方式,您可以在重新加载页面之前进行确认。

<form method="post" action="" onSubmit="return confirm('Are you sure you want to upddate this user?')">
<input type="hidden" name="userid" value="<?php echo $hiddenUserIdHere; ?>">
<input type="text" name="input_name">
</form>

这是一个更好的方法。

最新更新