根据用户输入显示数据库中的值

时间:2013-02-16 06:11:23

标签: php mysql

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

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>
    }

2 个答案:

答案 0 :(得分: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' /> ....

答案 1 :(得分: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标签。