-1

我的代码以这种方式工作。我有一个登录,当管理员登录时,他可以输入名称并提交。应向用户显示与该名称对应的所有数据库值。现在我得到的是,管理员可以输入值(名称)并点击提交。循环不显示 values ,而是存在并转到另一个函数。我哪里错了?

if( $_SESSION["logging"]&& $_SESSION["logged"])
{
 print_secure_content();
}  
function chk()
 {    
if($res[ROLE]=='admin')
{
  echo "hey admin";

<form action="" method=post>
<input type = "text" name="submit" />Enter<br/>
<input type="submit" name="submit" value="submit"/>
<?php
//It never gets inside this loop and directly jumps to print() function
if(isset($_POST['submit']))
 {
   echo "helloooo";
   $sq="select `name` from users where NAME='$_POST[submit]'";
   $result1=mysql_query($sq,$con) or die(mysql_error());
  while($res1=mysql_fetch_assoc($result1));
  {
    echo "hiiiii";
    $user['name']=$res1['NAME'];
    echo $user['name'];
   } ?>
  <input type="text" name="fo" class="textfield" value="<?php echo $user['name']; ?>"/>
  </label><br />
 <?php
 return mysql_num_rows($user);
 }
  }
  // this gets executed
  function print_secure_content()
   {
    print("<b><h1>hi $_SESSION[user]</h1>");
print "<br><h2>only a logged in user can see this</h2><br><a href='logout.php'>Logout</a>
    }
4

2 回答 2

3

试试这个代码..

<?php
if($res[ROLE]=='admin')
{
    echo "hey admin";
    ?>
    <form action="" method=post>
    Name<input type = "text" name="name" /><br/>
    <input type="submit" name="submit" value="submit"/>
    <?php
    //It never gets inside this loop and directly jumps to print() function
    if(isset($_POST['submit']))
    {
        $sq="select `name` from `users` where `name`='".$_POST['name']."'";
        $result1=mysql_query($sq) or die(mysql_error());
        while($res=mysql_fetch_assoc($result1))
        {
            $user['name']=$res['name'];
            echo $user['name'];
        } 
    ?>
        <input type="text" name="fo" class="textfield" value="<?php echo $user['name']; ?>"/>
        </label><br />
    <?php
    }
}
// this gets executed
function print_secure_content()
{
    print("<b><h1>hi $_SESSION[user]</h1>");
    print "<br><h2>only a logged in user can see this</h2><br><a href='logout.php'>Logout</a>";
}
?>

您的问题的更改是从...中删除;并使用while另一个...name<input type='text' />

于 2013-02-16T07:04:54.357 回答
1

问题出在您的表单中,提交和文本框的输入名称相同:

<form action="" method=post>
<input type = "text" name="submit" />Enter<br/>
<input type="submit" name="submit" value="submit"/>

正确的可能是:

<form action="" method=post>
    <input type = "text" name="submit" />Enter<br/>
    <input type="submit" name="submit_btn" value="submit"/>
</form>

同时关闭 from 标签。

于 2013-02-16T06:28:35.943 回答