Php Mysql更改用户名查询
我遇到了一些问题,我需要更改用户名,因为 这是我网站管理员的特权,但当它进入mysql端时,我不知道它是如何工作的: 管理员填写表单(用户名和新用户名) 我需要它找到用户名id,然后在输入的表单中用所选的新用户名更改用户名 如果这有帮助,这是我目前的代码Php Mysql更改用户名查询,php,mysql,Php,Mysql,我遇到了一些问题,我需要更改用户名,因为 这是我网站管理员的特权,但当它进入mysql端时,我不知道它是如何工作的: 管理员填写表单(用户名和新用户名) 我需要它找到用户名id,然后在输入的表单中用所选的新用户名更改用户名 如果这有帮助,这是我目前的代码 <?php include 'core/init.php'; protect_page(); admin_protect(); include 'includes/overall/header.php'; if (empty($_POS
<?php
include 'core/init.php';
protect_page();
admin_protect();
include 'includes/overall/header.php';
if (empty($_POST) === false) {
$required_fields = array('username', 'add');
foreach($_POST as $key=>$value) {
if (empty($value) && in_array($key, $required_fields) === true) {
$errors[] = 'Fields marked with an asterisk are required';
break 1;
}
}
}
if (empty($errors) === false) {
if (user_exists($_POST['cusername']) === true) {
$errors[] = 'Sorry, the username \'' . $_POST['username'] . '\' is taken!';
}
}
?>
<?php
if (isset($_GET['success']) === true && empty($_GET['success']) === true) {
echo 'The username has been succesfully changed!';
} else {
if (empty($_POST) === false && empty($errors) === true) {
$cusername = array(
'username' => $_POST['username'],
'cusername' => $_POST['cusername']
);
function cusername($cusername) {
mysql_query("UPDATE users WHERE username = '".$_POST['username']."' SET username = ".$_POST['cusername']."");
}
cusername($cusername);
header('Location: changeusername.php?success');
exit();
} else if (empty($errors) === false) {
echo output_errors($errors);
}
?>
<h1>Admin Access Only</h1>
<p>Change a username</p>
<form action="" method="post">
<ul>
<li>
Username*:<br>
<input type="text" name="username">
</li>
<li>
New Username*:<br>
<input type="text" name="cusername">
</li>
<li>
<input type="submit" value="Update">
</li>
</ul>
</form>
<?php
}
include 'includes/overall/footer.php';
?>
仅限管理员访问
更改用户名
-
用户名*:
-
新用户名*:
-
在UPDATE语句中,首先需要集合,然后是WHERE。但正如Marc B所指出的,您始终需要小心清理和转义用户输入的数据。不要相信用户给你的任何东西!在这种情况下,您可以使用,虽然函数现在被弃用,您应该考虑将数据库处理代码更改为MySQL或PdoyMySQL。
mysql_query("UPDATE users SET username = '".mysql_real_escape_string($_POST['cusername'])."' WHERE username = '".mysql_real_escape_string($_POST['username'])."'");
你很容易受到伤害。这尤其糟糕,因为您的查询正在修改用户信息。考虑<代码> CuxNeord <代码>用户名,权限=“超级用户”< /代码>…