如果我输入隐藏的输入,为什么我的while循环不起作用

时间:2013-07-15 09:36:26

标签: php html mysql

我目前正在尝试创建一个带有下拉列表的表单,该表单使用while循环从数据库填充自己。然后表单将用户的输入提交到我的数据库中的表中。为此,我使用了一段时间带有隐藏输入的循环,用于标记下拉列表中填充的选项,然后我将使用这些选项插入到我的数据库中。

问题: 如果我为隐藏输入编写代码,我的while循环不会循环,但它会在注释输入代码后执行。

我的代码摘录:

<html>
<head><link rel="stylesheet" type="text/css" href="style.css" /></head>
<body>
<?PHP //include("AdminNav.php");?>
<form action="<?PHP echo $_SERVER['PHP_SELF']?>" name="AddSub" method="POST">
<table border="1">
<tr>
<td>Category: </td>
<td>
<select name="category">
<?php
include("cxn.inc");
if($_SESSION['Auth']=="Yes" && $_SESSION['Type']=="Admin")
{
$userid=$_SESSION['UserId'];
$branch="0";
$getcat="SELECT id,Category FROM Categories WHERE Business=$userid && Branch=$branch";
$cat=mysqli_query($cxn,$getcat) or die (mysqli_error($cxn));
while($row=mysqli_fetch_assoc($cat))
{
    $catid=$row['id'];
    echo"<option value=$row[Category]>$row[Category]</option>";
    //echo"<input type='hidden' name='catid' value='$catid' />";<--This is the line
}

}
else if ($_SESSION['Auth']=="Yes" && $_SESSION['Type']=="Sub")
{
$userid=$_SESSION['UserId'];
$branchquery="SELECT id FROM Branch WHERE Bizid=$userid ";
$getbranch=mysqli_query($cxn,$branchquery) or die(mysqli_error($cxn));
$num=mysqli_num_rows($getbranch);
if($num>0)//Get Branch Details if Branches exist
{
    $resultbranch = mysqli_fetch_assoc($getbranch);
    $branch=$resultbranch['id'];
    $getcat="SELECT id,Category FROM Categories WHERE Business=$userid && Branch=$branch";
    $cat=mysqli_query($cxn,$getcat) or die (mysqli_error($cxn));

    while($row=mysqli_fetch_assoc($cat))
    {
        $catid=$row['id'];
        echo"<option value=$row[Category]>$row[Category]</option>";
        //echo"<input type='hidden' name='catid' value='$catid' />";
    }
}
else if($num==0)//Get just the User(Admin) Details if No branches exist
{
    $resultbranch = mysqli_fetch_assoc($getbranch);
    $branch=$resultbranch['id'];
    $getcat="SELECT id,Category FROM Categories WHERE Business=$userid";
    $cat=mysqli_query($cxn,$getcat) or die (mysqli_error($cxn));

    while($row=mysqli_fetch_assoc($cat))
    {
        $catid=$row['id'];
        echo"<option value=$row[Category]>$row[Category]</option>";
        //echo"<input type='hidden' name='catid' value='$catid' />";
    }
}

}

?>
</select>
</td>
</tr>
<tr>
<td>SubCategory:</td>
<td><input type="text" name="subcategory" maxlength="50" size="30" required="required" /></td>
</tr>
<tr>
<td>

<input type="submit" name="addsub" value="Submit" />
</td>
<td>
<?PHP 
if(isset($success))
{
echo"$success";
}
else if(isset($error))
{
echo"$error";
}   
?> 
</td>
</tr>
</table>
</form>
</body>
</html>

我的桌子摘录:

 ---------------------------------------------
 |  Id  |  Business  |  Branch  |  Category  |
 ---------------------------------------------
 | 155  |     5      |    0     |    Test1   |
 ---------------------------------------------
 | 156  |     5      |    0     |    Test2   |
 ---------------------------------------------
 | 157  |     5      |    0     |    Test3   |
 ---------------------------------------------
 | 158  |     5      |    0     |    Test4   |
 ---------------------------------------------

问题详情

当该行被注释掉时,下拉列表用Test 1,2,3,4填充自己,但是当我添加它时,它只显示Test1.Further,当我测试表单(通过提交它)时插入表格分类“Test1”但是它插入了id 158(属于'Test4'类别。

任何善良的灵魂都能指出我的错误以及我能做什么/应该做些什么来纠正它?我们也欢迎提出改进编码的建议。

谢谢!

修改(溶液) 正如其他任何人遇到同样的问题,我所做的是在我的处理代码中编辑一行代码来自

$catid=$row['id']; 

 $catid=$_POST['category'];

解决方案的功劳归于Corbin

4 个答案:

答案 0 :(得分:5)

<input>在选择框中间无效。

答案 1 :(得分:3)

您无法在<input>内嵌入<select>

只需选择$row['id']值即可。您可能应该使用处理该表单的代码中的ID。

答案 2 :(得分:2)

更多信息: 你的循环正在循环。但是你无法在浏览器中看到结果,导致你的代码无效,如其他答案所述。

另请检查“为什么使用PHP_SELF不安全:http://phpsecurity.wordpress.com/2007/11/03/the-danger-of-php_self/

你应该用引号包装你的选项值。没有它们也是无效的

我叮叮当你想要提交类别的ID。这应该这样做:

echo'<option value="'.$row['id'].'">'.$row['Category'].'</option>';

答案 3 :(得分:1)

尝试更改回声:

echo '<option value='.$row['Category'].'>'.$row['Category'].'</option>';

对另一个循环执行相同操作,是的,select标记内应该没有输入。您可以放置​​在外部并进行一些dom操作,以便在有选择时更改隐藏的输入值

相关问题