register site - how to compare

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gwolff2005
Forum Commoner
Posts: 68
Joined: Sun Apr 05, 2009 10:58 am

register site - how to compare

Post by gwolff2005 »

Hi guys,

I try to build a register page. the code is given and when code is right, name and username are saved in the database. But how can I check if the name is already in the database and if yes the user should see a message saying. The name already exists please try another one...

Thanks in advance!

Code: Select all

<?php require_once('Connections/Login.php'); ?>
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;
 
  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
 
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
 
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO users (username, password) VALUES (%s, %s)",
                       GetSQLValueString($_POST['username'], "text"),
                       GetSQLValueString($_POST['password'], "text"));
 
  mysql_select_db($database_Login, $Login);
  $Result1 = mysql_query($insertSQL, $Login) or die(mysql_error());
}
 
mysql_select_db($database_Login, $Login);
$query_Recordset1 = "SELECT code.code, users.username, users.password FROM code, users";
$Recordset1 = mysql_query($query_Recordset1, $Login) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
<?php
// *** Validate request to login to this site.
if (!isset($_SESSION)) {
  session_start();
}
 
$loginFormAction = $_SERVER['PHP_SELF'];
if (isset($_GET['accesscheck'])) {
  $_SESSION['PrevUrl'] = $_GET['accesscheck'];
}
 
if (isset($_POST['code'])) {
  $loginUsername=$_POST['code'];
  $password=$_POST['code'];
  $MM_fldUserAuthorization = "";
  $MM_redirectLoginSuccess = "intro.php";
  $MM_redirectLoginFailed = "http://www.guntmarwolff.com";
  $MM_redirecttoReferrer = false;
  mysql_select_db($database_Login, $Login);
  
  $LoginRS__query=sprintf("SELECT code, code FROM code WHERE code='%s' AND code='%s'",
    get_magic_quotes_gpc() ? $loginUsername : addslashes($loginUsername), get_magic_quotes_gpc() ? $password : addslashes($password)); 
   
  $LoginRS = mysql_query($LoginRS__query, $Login) or die(mysql_error());
  $loginFoundUser = mysql_num_rows($LoginRS);
  if ($loginFoundUser) {
     $loginStrGroup = "";
    
    //declare two session variables and assign them
    $_SESSION['MM_Username'] = $loginUsername;
    $_SESSION['MM_UserGroup'] = $loginStrGroup;       
 
    if (isset($_SESSION['PrevUrl']) && false) {
      $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];  
    }
    header("Location: " . $MM_redirectLoginSuccess );
  }
  else {
    header("Location: ". $MM_redirectLoginFailed );
  }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Registration</title>
</head>
<form action="" method="post"></form>
<body>
<p>&nbsp;</p>
 
<form method="POST" name="form1" action="<?php echo $loginFormAction; ?>">
  <table align="center">
    <tr valign="baseline">
      <td nowrap align="right"><div align="left">Code</div></td>
      <td><input name="code" type="text" id="code" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Username:</td>
      <td><input type="text" name="username" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Password:</td>
      <td><input type="text" name="password" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">&nbsp;</td>
      <td><input type="submit" value="Insert record"></td>
    </tr>
  </table>
  <input type="hidden" name="MM_insert" value="form1">
</form>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>
Last edited by gwolff2005 on Wed Apr 08, 2009 1:51 am, edited 3 times in total.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: register site - how to compare

Post by califdon »

Please use [syntax=php]and[/syntax] tags around code that you post here. It's terribly difficult to read unformatted code like that.
watson516
Forum Contributor
Posts: 198
Joined: Mon Mar 20, 2006 9:19 pm
Location: Hamilton, Ontario

Re: register site - how to compare

Post by watson516 »

Before you insert the data into the db, query the db to see if the name already exists, if it does, redirect back to the registration form. If it doesn't then insert it.
gwolff2005
Forum Commoner
Posts: 68
Joined: Sun Apr 05, 2009 10:58 am

Re: register site - how to compare

Post by gwolff2005 »

Hi watson, yeah, that is exactly what I want to do.. But I don't know the code for that...
watson516
Forum Contributor
Posts: 198
Joined: Mon Mar 20, 2006 9:19 pm
Location: Hamilton, Ontario

Re: register site - how to compare

Post by watson516 »

Something like this might work

Code: Select all

$q="SELECT * FROM table WHERE username = $username";
$result=mysql_query($q);
if(mysql_num_rows($result)) {
    //This username is already taken
}else{
    //This username is not taken, proceed with inserting
}
You would need to replace the table name and username and $username but you should get the idea
gwolff2005
Forum Commoner
Posts: 68
Joined: Sun Apr 05, 2009 10:58 am

Re: register site - how to compare

Post by gwolff2005 »

Hi thanks for your help. I set mysql now on unique but when i get the error message
Duplicate entry 'admin' for key 1
How can I make it more user friendly and that it comes up under the registration fields?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: register site - how to compare

Post by califdon »

gwolff2005 wrote:Hi thanks for your help. I set mysql now on unique but when i get the error message
Duplicate entry 'admin' for key 1
How can I make it more user friendly and that it comes up under the registration fields?
As watson516 explained, don't let such an error occur, by testing whether a name already exists. If it doesn't exist, go ahead with your process to write a new record; if one does exist, send whatever friendly message you want back to the user.
Post Reply