0
我遇到了一些麻煩,我需要更改用戶名,因爲 這是管理員在我的網站上的特權,但是當它到達MySQL的一面時,我不知道如此,這是它是如何工作的: 管理員填寫表單(用戶名&新用戶名) 我需要它來查找用戶名id,然後用所選的新用戶名以輸入的形式更改用戶名 如果這有幫助,這裏是我的代碼到目前爲止Mysql更改用戶名查詢
<?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';
?>
您很容易受到[sql注入攻擊](http://bobby-tables.com)的影響。這尤其糟糕,因爲您的查詢正在修改用戶信息。考慮用戶名'cusername',權限='超級用戶'... – 2015-02-10 21:33:26