【问题标题】:php if else statement within a select box选择框中的php if else语句
【发布时间】:2012-08-17 12:23:37
【问题描述】:

我正在开发一个允许用户创建文章的文章,但是对于管理员有一些限制,我将其标识为 SgroupId 1。现在当我使用我的管理员代码登录时,我意识到我仍然无法发布所有内容,除了我在 loadTypeUsers 中确定的内容。我知道我得到了 Sgroup1 的值,因为管理面板加载在下面的栏中。此外,当我回显该值时,我会得到 1 的返回值,这应该没问题。

但是当我尝试在弹出窗口中加载下拉列表时,它不会给我完整的列表。相反,它只加载我在 LoadTypeUsers 中指定的列表。有人可以帮我吗?

提前致谢。

~多夫

function MakeArticleTypeDropdown(){
        echo "<select name='ArticleTypeId'>";

        if($SgroupId == 1 || $SgroupId == 1){ 
            $results = LoadType();
        }
        else
        {
            $results = LoadTypeUsers();
        }

        while($row = mysql_fetch_array($results)){
            echo "<option value='".$row['ArticleTypeId']."'>"
                            .$row['ArticleTypeName']."</option>";
        }
        echo "</select>";
    }

这包含在 ArticleFunction.php 文件中

function LoadTypeUsers(){
    $query = "SELECT * FROM Articletype limit 1,3;";
    $resultaat=SendQuery($query);
    return $resultaat;
}

    function LoadType(){
    $query = "SELECT * FROM Articletype;";
    $resultaat=SendQuery($query);
    return $resultaat;
}

这包含在 Sentry.php 文件中

session_start();
$UserName = $_SESSION['username'];

$result = mysql_query("select * from user where username='".$UserName."'");
while($row = mysql_fetch_array($result)){
                $UserId = $row['UserId'];
                $CharacterName = $row['CharacterName'];
                $UserName = $row['UserName'];
                $SgroupId = $row['SgroupId'];
            };

【问题讨论】:

  • $SgroupId 变量在MakeArticleTypeDropdown 函数中应该来自哪里?从这里看,它似乎是未定义的。
  • 要么将其传递给函数 MakeArticleTypeDropdown($SgroupId) 要么使用全局 $SgroupId;在函数内部,因此它可以访问外部值;
  • rabble rabble mysql_* rabble rabble open to sql injection rabble rabble strict type-checking rabble rabble row is open to xss rabble
  • 只是好奇:为什么是if($SgroupId == 1 || $SgroupId == 1)? (重复)或其错字!!!

标签: php mysql html


【解决方案1】:

$SgroupId 没有在 MakeArticleTypeDropdown() 函数中定义,所以它总是处于 else 状态。试试下面的方法

MakeArticleTypeDropdown($SgroupId)
{
//-----------your code
}

【讨论】:

  • 谢谢,解决了问题。我真的很愚蠢,我忽略了将 groupId 拍摄到函数中!非常感谢
  • 这种情况经常发生在我们身上,这就是为什么这样的社区和论坛会互相帮助。感谢您的评论 :)
【解决方案2】:

首先,我没有看到您将$SgroupId 的值传递给MakeArticleTypeDropdown()。也许您遇到了范围问题,并且您正在检查未在函数内设置的变量 $SgroupId?

第二个:($SgroupId == 1 || $SgroupId == 1)|| 是干什么用的?

【讨论】:

  • 打错字了,抱歉,应该是 ($SgroupId == 1 || $SgroupId == 2)
【解决方案3】:

我认为LIMIT 子句应该是WHERE 子句。

SELECT * FROM Articletype WHERE SgroupId = 1 OR SgroupId = 3

也许还有那条线

if($SgroupId == 1 || $SgroupId == 1){  

应该阅读

if($SgroupId == 1 || $SgroupId == 3){  

【讨论】:

  • 我尝试使用 where 子句,但它使查询变得很长,而且它只是我需要表中的语句 2,3 和 4。我认为这是正确的解决方案,它对我有用。表本身没有链接到它的 SgroupId。 articleType 表只定义了 article 表中的数字。
  • LIMIT 只是限制检索的行数。暗示的不是那样。也许是对问题的编辑(请不要编辑现有文本,而是添加更多信息)。
  • 我明白了,我会调查这个,但我可以告诉你,ArticleType 表只有 2 个字段,即 ArticleTypeId(运行到 Article 表)和每个 Id 的定义,1是公告,2是文章等等。所以我不明白在这种情况下你的 SgroupId 是从哪里进来的。你想让我在我的数据库中添加另一个字段集,我声明那个字段集可以作为每个组使用?抱歉,因为你把我弄丢了,Ed。
  • 只是预感 PHP 变量名与列名相同,因此两个函数,LIMIT 将使用它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-25
  • 1970-01-01
  • 2015-07-07
  • 2018-07-24
  • 1970-01-01
相关资源
最近更新 更多