473,323 Members | 1,589 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

How can I check if username exists in database table.

Hi guys,

I need help with validating the user to see if the username exists in database table or not.

Here is my code.

default.php

Expand|Select|Wrap|Line Numbers
  1.  
  2. <form name="form1" action="authenticate.php">
  3. <table class="tableborder" align=center bgcolor="#f0f0f2">
  4.     <tr>
  5.   <td style="padding-bottom: 10px;" colspan="2" class="heading1"><b>Login Required</b></td>
  6. </tr>
  7.     <tr>
  8.       <td height="30">Username:</td>
  9.       <td><input type="text" name="Username" style="width:15em;">
  10.       </td>
  11.     </tr>
  12.     <tr>
  13.       <td height="30">Password:</td>
  14.       <td><input type="password" name="Password" style="width:15em;">
  15.       </td>
  16.     </tr>
  17.     <tr><td height="30" align="right"><input class="submit" name="register" type="button" value="Register" onClick="window.location=;"></td>
  18.     <td><input name="submit" type="submit" value="Log-in" class="submit"></td></tr>
  19.   </table>
  20. </form>
  21.  
authenticate.php

Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3. $con = mysql_connect("localhost","username","password");
  4. if (!$con)
  5.   {
  6.   die('Could not connect: ' . mysql_error());
  7.  
  8.   }
  9. mysql_select_db("tbl_login", $con);
  10. $Username = $_POST['Username'];
  11. $Password = $_POST['Password'] ; 
  12.  
  13. $sql="SELECT Username FROM login_tbl WHERE Username=’".$Username.”’ and Password=’”.$Password.”’”;
  14. $r = mysql_query($sql);
  15. if(!$r) {
  16.    $err=mysql_error();
  17.    print $err;
  18.    exit();
  19. }
  20. if(mysql_affected_rows()==0){
  21.    print "no such login in the system. please try again.";
  22.    exit();
  23. }
  24. else{
  25.    print "successfully logged into system.";
  26.    //proceed to perform website’s functionality – e.g. present information to the user
  27. }
  28. ?>
I desperately need to fix this code. Any help will be really appreciated.

Thanks
Aug 28 '08 #1
7 61587
Markus
6,050 Expert 4TB
What's wrong with above code?
What does or doesnt happen?

Use mysql_num_rows() to see if the user name is already present.
Aug 28 '08 #2
What's wrong with above code?
What does or doesnt happen?

Use mysql_num_rows() to see if the user name is already present.
Hi markusn,

I am not sure what's wrong with the code. When I enter username and password which already exists in database and click on login I get a blank page. Please help me with this problem.

Thanks
Aug 28 '08 #3
coolsti
310 100+
It is hard for us to see what the problem is. You need to do some debugging here yourself.

Suggestions: Echo out the query statement so you can see what is really being submitted to your database. See if there are any unexpected errors here, such as a blank value due to a typo or some other strange reason. Check to see that any non-alphabetic symbols in the password do not make the query incorrect syntactically.

Then if possible, try to copy and paste the query statement that you echo out to your screen directly into a database console window. This will let you see what the database is really outputting instead of having it filtered through your PHP code. Here you can see what the return is.

By the way, two things I notice about your code.

1) you have $Password used directly in your query, but don't you need to have something like a database specific function call like PASSWORD('$password')? My syntax here not totally correct but the point is, the database may store the password in an encrypted format so just comparing the stored value with the user supplied value directly may not work.

2) You examine if the user is registered by checking the affected rows, and this is not robust in my opinion. You should just use a SELECT count(*) statement and then examine the number of rows returned. If 0, then the user does not exist yet, if 1 then the user already exists.
Aug 28 '08 #4
Atli
5,058 Expert 4TB
Hi.

In your query, you are escaping the values in back-ticks (`) rather than single-quotes ('). That won't work.
Back-ticks are meant for column or database names. Single-quotes for string and date values.

Also, the structure of that code is a bit odd.
You should be making sure that the user is valid, or else show him an error. Your code does the except opposite, checking if the user is NOT valid, validating by default.

Consider the following:
Expand|Select|Wrap|Line Numbers
  1. $result = mysql_query("...") or die(mysql_error());
  2. if(mysql_num_rows($result) == 1) {
  3.   echo "Success! You are logged in!";
  4. }
  5. else {
  6.   echo "Failure! Try again.";
  7. }
  8.  
Also note how I use the die() function there. Basically does the same thing the if statement after you query call does.
Aug 28 '08 #5
It is hard for us to see what the problem is. You need to do some debugging here yourself.

Suggestions: Echo out the query statement so you can see what is really being submitted to your database. See if there are any unexpected errors here, such as a blank value due to a typo or some other strange reason. Check to see that any non-alphabetic symbols in the password do not make the query incorrect syntactically.

Then if possible, try to copy and paste the query statement that you echo out to your screen directly into a database console window. This will let you see what the database is really outputting instead of having it filtered through your PHP code. Here you can see what the return is.

By the way, two things I notice about your code.

1) you have $Password used directly in your query, but don't you need to have something like a database specific function call like PASSWORD('$password')? My syntax here not totally correct but the point is, the database may store the password in an encrypted format so just comparing the stored value with the user supplied value directly may not work.

2) You examine if the user is registered by checking the affected rows, and this is not robust in my opinion. You should just use a SELECT count(*) statement and then examine the number of rows returned. If 0, then the user does not exist yet, if 1 then the user already exists.
Problem solved. Thanks guys.
Aug 29 '08 #6
Hi.

In your query, you are escaping the values in back-ticks (`) rather than single-quotes ('). That won't work.
Back-ticks are meant for column or database names. Single-quotes for string and date values.

Also, the structure of that code is a bit odd.
You should be making sure that the user is valid, or else show him an error. Your code does the except opposite, checking if the user is NOT valid, validating by default.

Consider the following:
Expand|Select|Wrap|Line Numbers
  1. $result = mysql_query("...") or die(mysql_error());
  2. if(mysql_num_rows($result) == 1) {
  3.   echo "Success! You are logged in!";
  4. }
  5. else {
  6.   echo "Failure! Try again.";
  7. }
  8.  
Also note how I use the die() function there. Basically does the same thing the if statement after you query call does.
Problem solved. Thanks Atli.
Aug 29 '08 #7
I think you are right and mysqli should be used instead
Feb 4 '19 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Colin Steadman | last post by:
I have built a page that lists all our databases, the users connected to each database, and what application they are using. Because these databases are moved, or removed from time to time I need...
2
by: Jonathan | last post by:
I am looking for a simple way to check if a database table exists. I keep getting advice to use "Try.. Catch" and other error handling methods, but I obviously don't want to have to display an...
2
by: adam | last post by:
hello What query shoul I send to SQL serwer ( in transact SQL language ) to check does some database exist on serwer ? It similar to problem "does some table exist in database" - resolve to it...
16
by: Brian Tkatch | last post by:
Is there a way to check the order in which SET INTEGRITY needs to be applied? This would be for a script with a dynamic list of TABLEs. B.
2
by: Boujii | last post by:
<html> <head> <title>Add New MySQL User</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <? if(isset($_POST)) {
82
by: happyse27 | last post by:
Hi All, I modified the user registration script, but not sure how to make it check for each variable in terms of preventing junk registration and invalid characters? Two codes below : a)...
4
by: qwedster | last post by:
Howdy folks! I am using stored procedure to see if a value exists in database table and return 0 if exists or else -1, in the following SQL queries. However how to check if a value (that is...
2
by: qwedster | last post by:
Folk! How to programattically check if null value exists in database table (using stored procedure)? I know it's possble in the Query Analyzer (see last SQL query batch statements)? But how...
3
by: PHPstarter | last post by:
Hiya. I have a script that is run by a <form> which basically allows u to write into inputs the fields username, new username, password, repeat password, comment etc, The problem is that I want...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.