【发布时间】:2016-07-05 06:30:12
【问题描述】:
所以我正在 PHP 5.6 中开发 admin 面板功能,每当我尝试将用户切换到管理员时,它会将他们设置为 Builder 我不知道我在哪里或做错了什么
这是代码
这是updateusera.php
<?php include 'core/init.php';
$id = $_GET['id'];
$type = $_GET['type'];
if($type == 'admin'){
mysql_query("UPDATE `users` SET `type` = 'user' WHERE `user_id` = '$id'");
header('location: changeusers.php');
} else if($type =='user'){
mysql_query("UPDATE `users` SET `type` = 'admin' WHERE `user_id` = '$id'");
header('location: changeusers.php');
}
//=============================================== ==============================
if($type == 'moderator'){
mysql_query("UPDATE `users` SET `type` = 'user' WHERE `user_id` = '$id'");
header('location: changeusers.php');
} else if($type =='user'){
mysql_query("UPDATE `users` SET `type` = 'moderator' WHERE `user_id` = '$id'");
header('location: changeusers.php');
}
//=============================================== ==============================
if($type == 'builder'){
mysql_query("UPDATE `users` SET `type` = 'user' WHERE `user_id` = '$id'");
header('location: changeusers.php');
} else if($type =='user'){
mysql_query("UPDATE `users` SET `type` = 'builder' WHERE `user_id` = '$id'");
header('location: changeusers.php');
}
?>
this is the changeusers.php file
由于某种原因,它不允许我将我的两个代码都放在这里 P.S 我是 Stackoverflow 的新手,所以我仍然习惯所有这些
如果有人有想法,我很想听听
谢谢!
【问题讨论】:
-
首先,您遇到了两个主要问题。 1. 您正在使用已弃用的
mysql_*-函数(自 PHP 5.5 起已弃用并在 PHP 7 中删除)。 2. 你对SQL Injections 敞开心扉。我建议更改为 Mysqli 或首选 PDO,然后使用 Prepared Statements。 -
我的一个简单问题。当你
echo $_GET['type']时你得到了什么 -
我还是先习惯了 PHP,所以我正在构建它以便更好地使用它,而且我还有很多其他文件,所以当我有时间时,我会开始转换为mysqli
-
顺便说一句..您需要在
header('location: ....);语句之后添加exit;。 -
您实际上应该首先更改为 MySQLi 或 PDO。
mysql_-functions 真的很不安全,你用这些写的代码越多,你以后需要更新的越多(……那很可能会发生)。 不要从不安全和不推荐使用的工具开始学习。它对你没有帮助。