Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

check if username is already in database at login

Posted on 2009-04-03
8
1,149 Views
Last Modified: 2013-12-12
How do i check if a username is already in the database at the login stage using php statements?
0
Comment
Question by:AnnMarieB
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 28

Expert Comment

by:gamebits
ID: 24058719
<?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
 

Author Comment

by:AnnMarieB
ID: 24058784
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
 
LVL 14

Expert Comment

by:shobinsun
ID: 24058846
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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 28

Expert Comment

by:gamebits
ID: 24058884
     $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
 

Author Comment

by:AnnMarieB
ID: 24058909
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
 
LVL 14

Accepted Solution

by:
shobinsun earned 500 total points
ID: 24058948
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
 
LVL 28

Expert Comment

by:gamebits
ID: 24058958
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
 
LVL 5

Expert Comment

by:striker46
ID: 24059770
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

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question