check if username is already in database at login

How do i check if a username is already in the database at the login stage using php statements?
AnnMarieBAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gamebitsCommented:
<?php

//include your file to connect to the db
include 'dbconnection.php';

$username = $_POST['username'];

$sql = mysql_query("SELECT * FROM tablename WHERE username = '$username'");

$num = mysql_num_rows($sql);

if($num > 0){do what you need when the user exist, set cookie, session etc}else{Do what you want when the user doesn't exist}

?>

This is very basic many improvment could and should be done.
                           
0
AnnMarieBAuthor Commented:
if this is my code whereabouts should i insert it into??thanks
<html>
<head>
  <title>Input Validation</title>
<style type="text/css">
* {margin:1;padding:0}
p{margin-bottom:1em}
/* mac hide \*/
html, body {height:100%}
/* end hide */
body {
	background:#ffffcc;
	color: #000000;
}
#outer{
	min-height:100%;
	margin-left:160px;
	background:#F8E7EC;
	border-left:1px solid #000;
	margin-bottom:-52px;
	color: #000000;
}
 
* html #outer{	height:100%} /*IE treats height as min-height anyway*/
#header{
	position:absolute;
	top:0;
	left:0;
	width:100%;
	height:70px;
	background:#FF0000;
	border-top:1px solid #000;
	border-bottom:1px solid #000;
	overflow:hidden;/* fixed height so hide overflow*/
	color: #000000;
}
#left {
	float: left;
	position: relative; /* Needed for IE/win */
	width: 160px;  /*same as margin on outer */  
	margin-left: -159px; /*must be 1px less than width otherwise won't push footer down */   
	left:-1px;/* line it up exactly*/
	padding-top:72px;/* space for header*/
}
#left {margin-right: -3px;}/* 3 px jog for ie (also fixes mozilla issue with float drop)*/
#left p {padding:3px;}
#footer {
	width:100%;
	clear:both;
	height:50px;
	border-top:1px solid #000;
	border-bottom:1px solid #000;
	background-color: #FF8080;
	color: #000000;
	text-align:center;
	position:relative;
}
* html #footer {/*only ie gets this style*/
	\height:52px;/* for ie5 */
	he\ight:50px;/* for ie6 */
}
#clearfooter{clear:both;height:52px;}/*needed to make room for footer*/
#centrecontent {
	float:right;
	width:100%;
	position:relative;
	padding-top:72px;/* space for header*/
}
.outerwrap {
float: left;
width: 100%;
position:relative;
}
 
 
</style>
</head>
<div id="header">
<h1>DCU Student Timetable Organiser</h1>
<br>
<br>
</div>
<div id="outer">
	<div class="outerwrap">
		<div id="centrecontent">
			<body>
			<h2> Please enter all your details below:</h2><br>
 <form method = "post" action ="<?php echo $_SERVER['PHP_SELF']; ?>">
 
  First name: <input name="firstname" type="text" value="">
  <br><br><br>
 
 
  Last name: <input name="lastname" type="text" value="">
  <br><br><br>
 
 
  username: <input name="username" type="text" value="">
  <br><br><br>
  
  password: <input name="password" type="text" value="">
  <br><br><br>
 
  email: <input name="email" type="text" value="">
  <br><br><br>
 
  
<p><b>Please Choose your programme:</b>
<SELECT NAME="coursecode">
<OPTION value="CA">CA</OPTION>
<OPTION value="CAIS">CAIS</OPTION>
<OPTION value="CASE">CASE</OPTION>
<br></select>
<br><br>
 
 
   <b>Please choose your year:</b>
<select Name = "courseyear">
<OPTION value="1">1</OPTION>
<OPTION value="2">2</OPTION>
<OPTION value="3">3</OPTION>
<OPTION value="4">4</OPTION>
<OPTION value="5">5</OPTION>
</select><br><br>
<br><br>
	
 
<input type="submit" name="submit" value="Submit">
</form>
<?php
include 'config.php';
include 'opendb.php';
 
	if (isset($_POST['submit'])) {
	$firstname = $_POST['firstname'];
    $lastname = $_POST['lastname'];
	$username = $_POST['username'];
	$password = ($_POST['password']);
	$email = $_POST['email'];
	$coursecode = $_POST['coursecode'];
	$courseyear = $_POST['courseyear'];
 
 
    if ( !empty($firstname) && !empty($lastname) && !empty($username) && !empty($password) && !empty($email) ) {
       
 
		$query = mysql_query("INSERT INTO userinfo(firstname, lastname, username, password, email, coursecode, courseyear) VALUES ('$firstname','$lastname','$username','$password','$email','$coursecode','$courseyear')");
		
		$query1 = mysql_query("INSERT INTO weeklytimetable(username, coursecode, courseyear, semester, id, day, time, subject) SELECT userinfo.username, userinfo.coursecode, userinfo.courseyear, courseinfo.semester, courseinfo.id, courseinfo.day, courseinfo.time, courseinfo.subject FROM courseinfo, userinfo WHERE courseinfo.semester = '1'");
		$query2 = mysql_query("INSERT INTO sem1week1(username, coursecode, courseyear, semester, id, day, time, subject) SELECT weeklytimetable.username, weeklytimetable.coursecode, weeklytimetable.courseyear, weeklytimetable.semester, weeklytimetable.id, weeklytimetable.day, weeklytimetable.time, weeklytimetable.subject FROM weeklytimetable");
		
 
 
      Header("Location: inputsuccess.php");
    }
    else { $error = true; }
 
 
 
   if($error = true && empty($firstname))	{
		echo ' <span style ="color:red">Error! Please enter a firstname.</span<br>';
	}
	if($error = true && empty($lastname))	{
		echo ' <span style ="color:red">Error! Please enter a lastname.</span<br>';
	}
	if($error = true && empty($username))	{
		echo ' <span style ="color:red">Error! Please enter a usernamename.</span<br>';
	}
	if($error = true && empty($password))	{
		echo ' <span style ="color:red">Error! Please enter a password.</span<br>';
	}
	if($error = true && empty($email))	{
		echo ' <span style ="color:red">Error! Please enter a email.</span<br>';
	}
	if($coursecode == 'CA' && $courseyear == '3')	{
		echo ' <span style ="color:red">Error! you cannot pick year three with this course.</span<br>';
	}
	if($coursecode == 'CA' && $courseyear == '4')	{
		echo ' <span style ="color:red">Error! you cannot pick year four with this course.</span<br>';
	}
	if($coursecode == 'CA' && $courseyear == '5')	{
		echo ' <span style ="color:red">Error! you cannot pick year five with this course.</span<br>';
	}
	if($coursecode == 'CAIS' && $courseyear == '1')	{
		echo ' <span style ="color:red">Error! you cannot pick year one with this course.</span<br>';
	}
	if($coursecode == 'CAIS' && $courseyear == '2')	{
		echo ' <span style ="color:red">Error! you cannot pick year two with this course.</span<br>';
	}
	if($coursecode == 'CAIS' && $courseyear == '5')	{
		echo ' <span style ="color:red">Error! you cannot pick year three with this course.</span<br>';
	}
	if($coursecode == 'CASE' && $courseyear == '1')	{
		echo ' <span style ="color:red">Error! you cannot pick year one with this course.</span<br>';
	}
	if($coursecode == 'CASE' && $courseyear == '2')	{
		echo ' <span style ="color:red">Error! you cannot pick year two with this course.</span<br>';
	}
	if($coursecode == 'CASE' && $courseyear == '5')	{
		echo ' <span style ="color:red">Error! you cannot pick year three with this course.</span<br>';
	}
	//$query = mysql_query("SELECT * FROM userinfo WHERE user_name = '". $username ."' OR email = '". $email ."'");
 
	//if (mysql_num_rows($query) > 0)
	//{
     //echo 'Username or email already in use.';
	//}
 
	}
	?>
</div>
<div id="left">
		<h3><A HREF ="login2.php" TARGET="right">Login </A><br><br>
		<A HREF ="http://www.dcu.ie" TARGET="right">DCU </A></h3>
</div>
</div>
</body>
</html>

Open in new window

0
shobinsunCommented:
Hello,

Use this complete code . this will help you.

<?php    
mysql_connect("localhost", "dbusername","dbpasswd") or
    die ("Could not connect to database");
mysql_select_db("dbname") or
    die ("Could not select database");
 
     if(isset($_POST['submit'])&& $_POST['username'])
     {
      
      $user = '"'.$_POST['username'].'"';
      
      $sql = mysql_query("SELECT * FROM table WHERE name = ".$user);

      $count = mysql_num_rows($sql);
      if($count != 0)
      {
            echo "Test ok";
                //use your custom code here
      }
       else
       {
                //use your custom code
       }
     }
?>

<form name="form1" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" name="username">
<input type="submit" name="submit" value="Submit">
</form>

Regards.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

gamebitsCommented:
     $email = $_POST['email'];
      $coursecode = $_POST['coursecode'];
      $courseyear = $_POST['courseyear'];
 
$sql00 = mysql_query("SELECT username FROM userinfo WHERE username = '$username'");
  $num = mysql_num_rows($sql00);
 
    if ( !empty($firstname) && !empty($lastname) && !empty($username) && !empty($password) && !empty($email)  && $num < 1) {      
 
            $query = mysql_query("INSERT INTO userinfo(firstname, lastname, username, password, email, coursecode, courseyear) VALUES ('$firstname','$lastname','$username','$password','$email','$coursecode','$courseyear')");
            
0
AnnMarieBAuthor Commented:
this is the error i got when i ran that code:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\wamp\www\StudentOrganiser\check.php on line 12
error
0
shobinsunCommented:
Hello,

Here I am attaching the modified code of you.

Hope this will help you.

Regards.
<html>
<head>
  <title>Input Validation</title>
<style type="text/css">
* {margin:1;padding:0}
p{margin-bottom:1em}
/* mac hide \*/
html, body {height:100%}
/* end hide */
body {
        background:#ffffcc;
        color: #000000;
}
#outer{
        min-height:100%;
        margin-left:160px;
        background:#F8E7EC;
        border-left:1px solid #000;
        margin-bottom:-52px;
        color: #000000;
}
 
* html #outer{  height:100%} /*IE treats height as min-height anyway*/
#header{
        position:absolute;
        top:0;
        left:0;
        width:100%;
        height:70px;
        background:#FF0000;
        border-top:1px solid #000;
        border-bottom:1px solid #000;
        overflow:hidden;/* fixed height so hide overflow*/
        color: #000000;
}
#left {
        float: left;
        position: relative; /* Needed for IE/win */
        width: 160px;  /*same as margin on outer */  
        margin-left: -159px; /*must be 1px less than width otherwise won't push footer down */   
        left:-1px;/* line it up exactly*/
        padding-top:72px;/* space for header*/
}
#left {margin-right: -3px;}/* 3 px jog for ie (also fixes mozilla issue with float drop)*/
#left p {padding:3px;}
#footer {
        width:100%;
        clear:both;
        height:50px;
        border-top:1px solid #000;
        border-bottom:1px solid #000;
        background-color: #FF8080;
        color: #000000;
        text-align:center;
        position:relative;
}
* html #footer {/*only ie gets this style*/
        \height:52px;/* for ie5 */
        he\ight:50px;/* for ie6 */
}
#clearfooter{clear:both;height:52px;}/*needed to make room for footer*/
#centrecontent {
        float:right;
        width:100%;
        position:relative;
        padding-top:72px;/* space for header*/
}
.outerwrap {
float: left;
width: 100%;
position:relative;
}
 
 
</style>
</head>
<div id="header">
<h1>DCU Student Timetable Organiser</h1>
<br>
<br>
</div>
<div id="outer">
        <div class="outerwrap">
                <div id="centrecontent">
                        <body>
                        <h2> Please enter all your details below:</h2><br>
 <form method = "post" action ="<?php echo $_SERVER['PHP_SELF']; ?>">
 
  First name: <input name="firstname" type="text" value="">
  <br><br><br>
 
 
  Last name: <input name="lastname" type="text" value="">
  <br><br><br>
 
 
  username: <input name="username" type="text" value="">
  <br><br><br>
  
  password: <input name="password" type="text" value="">
  <br><br><br>
 
  email: <input name="email" type="text" value="">
  <br><br><br>
 
  
<p><b>Please Choose your programme:</b>
<SELECT NAME="coursecode">
<OPTION value="CA">CA</OPTION>
<OPTION value="CAIS">CAIS</OPTION>
<OPTION value="CASE">CASE</OPTION>
<br></select>
<br><br>
 
 
   <b>Please choose your year:</b>
<select Name = "courseyear">
<OPTION value="1">1</OPTION>
<OPTION value="2">2</OPTION>
<OPTION value="3">3</OPTION>
<OPTION value="4">4</OPTION>
<OPTION value="5">5</OPTION>
</select><br><br>
<br><br>
        
 
<input type="submit" name="submit" value="Submit">
</form>
<?php
include 'config.php';
include 'opendb.php';
 
        if (isset($_POST['submit'])) {
        $firstname = $_POST['firstname'];
        $lastname = $_POST['lastname'];
        $username = $_POST['username'];
        $password = ($_POST['password']);
        $email = $_POST['email'];
        $coursecode = $_POST['coursecode'];
        $courseyear = $_POST['courseyear'];
 
 
    if ( !empty($firstname) && !empty($lastname) && !empty($username) && !empty($password) && !empty($email) ) {
 
 
        $query = mysql_query("SELECT * FROM userinfo WHERE user_name = '". $username ."' OR email = '". $email ."'");
 
        if (mysql_num_rows($query) != 0)
        {
     		echo 'Username or email already in use.';
        }
        else
        {
       
 
                $query = mysql_query("INSERT INTO userinfo(firstname, lastname, username, password, email, coursecode, courseyear) VALUES ('$firstname','$lastname','$username','$password','$email','$coursecode','$courseyear')");
                
                $query1 = mysql_query("INSERT INTO weeklytimetable(username, coursecode, courseyear, semester, id, day, time, subject) SELECT userinfo.username, userinfo.coursecode, userinfo.courseyear, courseinfo.semester, courseinfo.id, courseinfo.day, courseinfo.time, courseinfo.subject FROM courseinfo, userinfo WHERE courseinfo.semester = '1'");
                $query2 = mysql_query("INSERT INTO sem1week1(username, coursecode, courseyear, semester, id, day, time, subject) SELECT weeklytimetable.username, weeklytimetable.coursecode, weeklytimetable.courseyear, weeklytimetable.semester, weeklytimetable.id, weeklytimetable.day, weeklytimetable.time, weeklytimetable.subject FROM weeklytimetable");
                
 
 
      Header("Location: inputsuccess.php");
      }
    }
    else { $error = true; }
 
 
 
   if($error = true && empty($firstname))       {
                echo ' <span style ="color:red">Error! Please enter a firstname.</span<br>';
        }
        if($error = true && empty($lastname))   {
                echo ' <span style ="color:red">Error! Please enter a lastname.</span<br>';
        }
        if($error = true && empty($username))   {
                echo ' <span style ="color:red">Error! Please enter a usernamename.</span<br>';
        }
        if($error = true && empty($password))   {
                echo ' <span style ="color:red">Error! Please enter a password.</span<br>';
        }
        if($error = true && empty($email))      {
                echo ' <span style ="color:red">Error! Please enter a email.</span<br>';
        }
        if($coursecode == 'CA' && $courseyear == '3')   {
                echo ' <span style ="color:red">Error! you cannot pick year three with this course.</span<br>';
        }
        if($coursecode == 'CA' && $courseyear == '4')   {
                echo ' <span style ="color:red">Error! you cannot pick year four with this course.</span<br>';
        }
        if($coursecode == 'CA' && $courseyear == '5')   {
                echo ' <span style ="color:red">Error! you cannot pick year five with this course.</span<br>';
        }
        if($coursecode == 'CAIS' && $courseyear == '1') {
                echo ' <span style ="color:red">Error! you cannot pick year one with this course.</span<br>';
        }
        if($coursecode == 'CAIS' && $courseyear == '2') {
                echo ' <span style ="color:red">Error! you cannot pick year two with this course.</span<br>';
        }
        if($coursecode == 'CAIS' && $courseyear == '5') {
                echo ' <span style ="color:red">Error! you cannot pick year three with this course.</span<br>';
        }
        if($coursecode == 'CASE' && $courseyear == '1') {
                echo ' <span style ="color:red">Error! you cannot pick year one with this course.</span<br>';
        }
        if($coursecode == 'CASE' && $courseyear == '2') {
                echo ' <span style ="color:red">Error! you cannot pick year two with this course.</span<br>';
        }
        if($coursecode == 'CASE' && $courseyear == '5') {
                echo ' <span style ="color:red">Error! you cannot pick year three with this course.</span<br>';
        }
 
 
        }
        ?>
</div>
<div id="left">
                <h3><A HREF ="login2.php" TARGET="right">Login </A><br><br>
                <A HREF ="http://www.dcu.ie" TARGET="right">DCU </A></h3>
</div>
</div>
</body>
</html>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gamebitsCommented:
This mean the query did not work, I used the table name you have in the INSERT query as well as the field name "username", first thing would be to check if we connect to the db properly.

If you have access to mysql with phpmyadmin for example you can try to run the query directly you just need to replace $username with an appropriate username
0
striker46Commented:
http://www.aleixcortadellas.com/main/2009/03/05/465/

This is a solution which combines PHP and AJAX for a username availability check.
Try the demo here to see it in action: http://www.aleixcortadellas.com/demos/ajaxusernamecheck/index.php

Regards
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.