【问题标题】:Can't figure out why this won't work (PHP)无法弄清楚为什么这不起作用(PHP)
【发布时间】: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

http://pastebin.com/qR2VybTk

由于某种原因,它不允许我将我的两个代码都放在这里 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 真的很不安全,你用这些写的代码越多,你以后需要更新的越多(……那很可能会发生)。 不要从不安全和不推荐使用的工具开始学习。它对你没有帮助。

标签: php html mysql


【解决方案1】:

$type =='user' 时,您有三个else if 语句,即true。这些else if 中的所有内容都在执行并发送了三个查询,其中最后一个是设置类型生成器。

你需要传入你需要设置的参数类型。那样:

if($type == 'admin') {
    echo "<a href='updateusera.php?id=&type=user'>Revoke Admin </a>";
} else {
    echo "<a href='updateusera.php?id=$id&type=admin'>Grant Admin </a>";
}
if($type == 'moderator') {
    echo "<a href='updateusera.php?id=$id&type=user'>Revoke Moderator </a>";
} else {
    echo "<a href='updateusera.php?id=$id&type=moderator'>Grant Moderator </a>";
}
if($type == 'builder') {
    echo "<a href='updateusera.php?id=$id&type=user'>Revoke Builder</a>";
} else {
    echo "<a href='updateusera.php?id=$id&type=builder'>Grant Builder</a>";
}

那么你的代码需要是这样的:

if($type == 'user') {
    mysql_query("UPDATE `users` SET `type` = 'user' WHERE `user_id` = '$id'");
    header('location: changeusers.php');
    exit;
}
if($type == 'admin') {
    mysql_query("UPDATE `users` SET `type` = 'admin' WHERE `user_id` = '$id'");
    header('location: changeusers.php');
    exit;
}
if($type == 'builder') {
    mysql_query("UPDATE `users` SET `type` = 'builder' WHERE `user_id` = '$id'");
    header('location: changeusers.php');
    exit;
}

【讨论】:

  • 那么我应该将我的else if's 更改为什么?
  • 我添加了出口的
  • 现在我得到了`注意:未定义的索引:第 4 行 C:\xampp\htdocs\updateusera.php 中的 id 注意:未定义的索引:输入 C:\xampp\htdocs\updateusera。 php 在第 5 行`
  • 你的代码看起来像mysql_query("UPDATE users` SET type = 'builder' WHERE user_id = '$id'"); header('location: changeusers.php'); exit ;`
  • 好的,所以现在不是一直将用户设置为 builder,而是将它们设置为 admin x.x
【解决方案2】:

您想将 else if 语句更改为 if 语句;基本上所有else if 语句都在执行,最后一个else if 语句设置为builder。

【讨论】:

  • 我应该将它们设置为什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-29
  • 1970-01-01
  • 2014-12-08
  • 2012-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多