简体   繁体   中英

Display value from database based on user input

My code works this way. I have a login, where when the admin logs in , he can enter the name and submit. All database values corresponding to that name should be displayed to the user. For now all I get is , the admin can enter the value(name) and hit submit . Instead of displaying values , the loop exists and goes to another function. Where am I going wrong?

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

Try this code..

<?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>";
}
?>

changes from your question is remove ; from while and use another name for <input type='text' /> ....

The problem is in your form, the input name for submit and textbox are same:

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

the correct could be:

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

Also close the from tag.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM