stackovr stackovr -4 years ago 182
HTML Question

Login using Radio button in php

I create a login page in which I use the the radio button and I want that when a user select one radio button then the query will work according to it..

My login form code is....

<form role="form" action="login.php" method="post" role="form">
<fieldset>
<div class="form-group">
<input class="form-control" placeholder="Username" name="user" type="text" autofocus="">
</div>
<div class="form-group">
<input class="form-control" placeholder="Password" name="pass" type="password" value="">
</div>
<div>
<label for="" class="btn btn-info disabled col-lg-12">Login As</label> <br/>
<br/>
<label for=""><input name="admin" type="radio" value="Admin" class="" checked>Admin </label>
<br/>
<label for=""><input name="admin" type="radio" value="Subdmin" class="">Sub-Admin</label>
</div>
<div class="checkbox">
<label>
<input name="remember" type="checkbox" value="Remember Me">Remember Me
</label>
</div>
<!-- Change this to a button or input when using this as a form -->
<input type="submit" class="btn btn-lg btn-success btn-block" value="Login"/>
</div>
</fieldset>
</form>


My php Code is....

<?php session_start();
include 'conn.php';
if(isset($_POST) && count($_POST)>0) {
$user = $_POST['user'];
$pass = $_POST['pass'];
$loginas=$_POST['admin'];

if($loginas=="Admin")
{
$sSQL = "SELECT * FROM admin WHERE Username ='".$user."' AND Password = '".$pass."'";
$result = mysql_query($sSQL) or die(mysql_error());
$row=mysql_num_rows($result);
if($row==1)
{
// Set username session variable
$_SESSION['admin'] = $user;
// Jump to secured page
header('location:selected_user.php');
}
}
if($loginas=='Subadmin')
{
$SQL = "SELECT * FROM subadmin WHERE User_Name ='".$user."' AND Password = '".$pass."'";
$res = mysql_query($SQL) or die(mysql_error());
$roww=mysql_num_rows($res);
if($roww==1)
{
// Set username session variable
$_SESSION['admin'] = $user;

// Jump to secured page
header('location:subadmin/index.php');
}

}

else
{
header('location:signin.php');
}
}

?>


The problem is that when I check the sub admin or admin radio button then it will not login. Can some tell me what is wrong with my code?

NOTE: Please ignore session, or any code weakness, just try to help me to solve the problem....

Answer Source
  1. There is a typo "Subdmin" v.s. "Subadmin"
  2. Are you sure the database contain the login account? According to your implementation, if the db return result size!=1, it will jump to the last ELSE case
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download