Jump to content

Checking if username exists in database


mikebyrne

Recommended Posts

I've changed it to

 

<?PHP

define('_USER', 'root');
define('_PASS', '');
define('_ADMIN','admin'); 

//Connect to server and select database.
mysql_connect("127.0.0.1",_USER,_PASS)or die("cannot connect to server");
mysql_select_db(_ADMIN)or die("cannot select DB");

?>

 

But im still getting the same connection errors

 

 

Link to comment
Share on other sites

Here's what I use:

 

<?php
$_SESSION['user'] = htmlspecialchars($_POST['user']);
$_SESSION['mail'] = htmlspecialchars($_POST['mail']);
$user = mysql_real_escape_string(stripslashes($_SESSION['user']));
$email = mysql_real_escape_string(stripcslashes($_SESSION['mail']));

$sql = "SELECT * FROM user WHERE username='$user'";
    $result = mysql_query($sql) or die("Error in part sql: " . mysql_error());
    

    $count = mysql_num_rows($result);

    if ($count == 1) {
        $errors[] .= _USER_EXISTS;

    }
    $sel = "SELECT * FROM user WHERE email='$email'";

    $results = mysql_query($sel) or die("Error in part sel: " . mysql_error());
    
    $counts = mysql_num_rows($results);

    if ($counts == 1) {
        $errors[] .= _EMAIL_EXISTS;

    }
  
  if (isset($_POST['submit']) && $errors[0] != null) {
        echo '    <div class="ddgb_entrybox">
	<table width="100%" border="0" cellspacing="8" cellpadding="0">
	<tr>
    <td width="42%" align="right" valign="top"></td>
	<td align="center" valign="top">';
        echo "<h2>" . _ERROR . "</h2><ul>";
        foreach ($errors as $f) {
            echo "<li>" . $f . "</li>";
        }
        echo "</ul>";
        echo '<br><br><br>
</td>
	</tr>
	</table>
	</div>';
  } else {
    	$rand = get_rand_id(9);
?>

 

Which means if the username is found in the db it will throw the error, same with the email.

 

Hope that helps?

 

Sam

 

 

 

Link to comment
Share on other sites

<?php

$user = mysql_real_escape_string(htmlspecialchars($_POST['name']));

$sql =  "SELECT name FROM adminusers WHERE name ='$user'";
$result = mysql_query($sql) or die("Error in SQL: ".mysql_error());
$row = mysql_fetch_array($result);
$count = mysql_num_rows($result);

$name = $row['name'];


if ($count > 0) { // username should only exist once.
echo "Error! The username " . $name . " already exists in the database.";
}
else {
echo "Success! The username " .$user . " does not already exist in the database.";
}
}
?>

 

I leave every field blank but the username where I type in xxx but i stiil get the echo Success! The username does not already exist in the database.

 

The SQL of the database is:

 

CREATE TABLE `adminusers` (

  `name` varchar(255) collate latin1_general_ci default NULL,

  `address` varchar(255) collate latin1_general_ci default NULL,

  `address1` varchar(255) collate latin1_general_ci default NULL,

  `address2` varchar(255) collate latin1_general_ci default NULL,

  `address3` varchar(255) collate latin1_general_ci default NULL,

  `address4` varchar(255) collate latin1_general_ci default NULL,

  `county` varchar(255) collate latin1_general_ci default NULL,

  `zip` varchar(255) collate latin1_general_ci default NULL,

  `telephone` decimal(10,0) default NULL,

  `email` varchar(255) collate latin1_general_ci default NULL,

  `username` varchar(255) collate latin1_general_ci default NULL,

  `password` varchar(255) collate latin1_general_ci default NULL,

  `usertype` decimal(10,0) default NULL

) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;

 

-- ----------------------------

-- Records

-- ----------------------------

INSERT INTO `adminusers` VALUES ('Test', 'yyy', 'yfgfg', 'tftrtrt', 'dtddtdtd', 'tdtdtdtd', 'tdtduhlllkk', 'oooioi', '787875675', 'hjhjhkhjkhjkh', 'xxx', '7676767678', null);

 

As you can see I only have 1 record and the username IS xxx

 

Cant figure it out!!

 

the form section that applies is:

 

<tr valign="baseline">
      <td nowrap align="right" width="95">Username:</td>
      <td nowrap align="right">
    <input name="username" size="32" style="font-size: 8pt; $style_username; float:left"></td>
      <td width="269"><font color="#FF0000" style="font-size: 8pt"><?php echo $error_username; ?></font></td>
    </tr>

 

 

Link to comment
Share on other sites

Oh!! I see your problem. :P

 

You have two fields in your table, name and username.

 

Username is xxx but name is Test. You are querying to see if name is xxx so change your SELECT statement to

 

 

"SELECT name FROM adminusers WHERE username ='$user'"; 

 

You should be sorted.

 

Sam

 

Link to comment
Share on other sites

No this still doesnt fix the problem. Im running out of ideas now :'(

 

$user = mysql_real_escape_string(htmlspecialchars($_POST['name']));


$sql = "SELECT name FROM adminusers WHERE username ='$user'";
echo ".$user.";
$result = mysql_query($sql) or die("Error in SQL: ".mysql_error());
$row = mysql_fetch_array($result);
$count = mysql_num_rows($result);

$name = $row['name'];


if ($count > 0) { // username should only exist once.
echo "Error! The username " . $name . " already exists in the database.";
}
else {
echo "Success! The username " .$user . " does not already exist in the database.";
}

 

$user has to be the problem??

Link to comment
Share on other sites

THAT SEEMS TO HAVE FIXED IT!!

 

Now how can I tidy it up to include the validation for leaving the field blank? something along the lines of:

 

if ($user == "" ||($count > 0)  {

echo 'got no password';

                $valid=0;

                $style_password = "background-color:#FF5959";

                $error_password = "Theres a problems with your password?<br>";

Link to comment
Share on other sites

Glad I could help. :D

 

<?php
$user = mysql_real_escape_string(htmlspecialchars($_POST['name']));

if($user = "") {
$errors[] .= 'You must enter a username';
}
if($errors[0] != NULL) {
echo '    <div>
	<table width="100%" border="0" cellspacing="8" cellpadding="0">
	<tr>
    <td width="42%" align="right" valign="top"></td>
	<td align="center" valign="top">';
        echo "<h2> Error! </h2><ul>";
        foreach ($errors as $f) {
            echo "<li>" . $f . "</li>";
        }
        echo "</ul>";
        echo '<br><br><br>
</td>
	</tr>
	</table>
	</div>';
}
else {


$sql = "SELECT name FROM adminusers WHERE username ='$user'";
echo ".$user.";
$result = mysql_query($sql) or die("Error in SQL: ".mysql_error());

$row = mysql_fetch_array($result);

$count = mysql_num_rows($result);

$name = $row['name'];

if ($count > 0) { // username should only exist once.
echo "Error! The username " . $name . " already exists in the database.";
}
else {
echo "Success! The username " .$user . " does not already exist in the database.";
}
}
?>

 

if the username field, once posted, is empty it throws up an error. If not then it runs the query.

 

Sam

Link to comment
Share on other sites

At present my valdation page looks like this

 

<?php


require_once("adminconnect.php");

$name = $_POST['name'];
$address = $_POST['address'];
$address1 = $_POST['address1'];
$address2 = $_POST['address2'];
$address3 = $_POST['address3'];
$address4 = $_POST['address4'];
$county = $_POST['county'];
$zip = $_POST['zip'];
$telephone = $_POST['telephone'];
$email = $_POST['email'];
$password =$_POST['password'];
$username = $_POST['username'];
$num =$_POST ['num'];




if($_POST["action"] == "signup"){

        $valid=1;

                if ($_POST['name']=="") {
			echo 'got no name<br>';
                $valid=0;
                $style_name = "background-color:#FF5959";
                $error_name = "Your name seems to be mising?<br>";
                                }

			if ($address == "" || strlen($address) < 2) {
    			echo 'got no address1<br>';//added by me to denote failure in this statement 
  		 		$valid=0;
   				$style_address = "background-color:#FF5959";
  				$error_address = "There is a problem with the address field?<br>";
							}


			if ($address1 == "" || strlen($address1) < 2) {
    			echo 'got no address1<br>';//added by me to denote failure in this statement 
  		 		$valid=0;
   				$style_address = "background-color:#FF5959";
  				$error_address1 = "There is a problem with the address field?<br>";
							}

			if ($address2 == "" || strlen($address2) < 2) {
    			echo 'got no address2<br>';//added by me to denote failure in this statement 
  		 		$valid=0;
   				$style_address = "background-color:#FF5959";
  				$error_address2 = "There is a problem with the address field?<br>";
							}

			if ($address3 == "" || strlen($address3) < 2) {
    			echo 'got no address3<br>';//added by me to denote failure in this statement 
  		 		$valid=0;
   				$style_address = "background-color:#FF5959";
  				$error_address3 = "There is a problem with the address field?<br>";
							}

			if ($address4 == "" || strlen($address4) < 2) {
    			echo 'got no address4<br>';//added by me to denote failure in this statement 
  		 		$valid=0;
   				$style_address = "background-color:#FF5959";
  				$error_address4 = "There is a problem with the address field?<br>";
							}
			if ($county == "" || strlen($county)<2) {
			echo 'got no county<br>';
                $valid=0;
                $style_county = "background-color:#FF5959";
                $error_county = "The County field is blank?<br>";
        						} 
			if ($zip == "" || strlen($zip)<2) {
			echo 'got no zip<br>';
                $valid=0;
                $style_zip = "background-color:#FF5959";
                $error_zip = "Theres a problem with the zip code?<br>";
        						}   
			if (!eregi("^[0-9]+",$telephone)) {
			echo 'got no phone<br>';
                $valid=0;
                $style_telephone = "background-color:#FF5959";
                $error_telephone = "Theres a problem with the telephone number?<br>";
        						}   
			  if (!eregi("^[A-Za-z0-9.-]+",$email)) {
			echo 'got no mail';
                $valid=0;
                $style_email = "background-color:#FF5959";
                $error_email = "Theres a problem with the email address?<br>";
       						 	}
        
        		if ($password == "" || strlen($password)<7) {
			echo 'got no password';
                $valid=0;
                $style_password = "background-color:#FF5959";
                $error_password = "Theres a problems with your password?<br>";
       							 }      


$user = mysql_real_escape_string(htmlspecialchars($_POST['username']));


$sql = "SELECT name FROM adminusers WHERE username ='$user'";
echo ".$user.";
$result = mysql_query($sql) or die("Error in SQL: ".mysql_error());
$row = mysql_fetch_array($result);
$count = mysql_num_rows($result);

$name = $row['name'];


if ($count > 0) { // username should only exist once.
echo "Error! The username " . $name . " already exists in the database.";
}
else {
echo "Success! The username " .$user . " does not already exist in the database.";
}
}
?>

 

Im not sure how I can incorprate yours within the code. Im trying to keep this a neat as possible

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.