Hi guys,

I have a simple registration form and have had trouble getting it to check if the username is already taken in the database. This is what I have at the moment:
(the code i tried to use to make the check is about half way down)

<?php
mysql_connect("", "", "") or die(mysql_error());
mysql_select_db("") or die(mysql_error());;

$fname = $_POST["fname"];
$lname = $_POST["lname"];
$uname = $_POST["uname"];
$pword = $_POST["pword"];
$email = $_POST["email"];
//above values are taken from form on previous page...
//first  name, last name, username, password and email

$subject = "Thankyou for registering with Grafax!";
$from = "**myemail**";
$message = 'You have just registered the following details at Grafax.co.uk:<hr/>
<table bgcolor="#E0E0E0">
<strong>First Name: </strong>'. $fname .'<br/>
<strong>Last Name: </strong>'. $lname .'<br/>
<strong>Username: </strong>'. $uname .'<br/>
<strong>Password: </strong>'. $pword .'</table>
<hr/>
<h3><a href="#">Click here to login</a></h3>';


//this is where i tried to check the username against whats in the database. 
//If $uname entered by user is different to the result from the mysql query 
//then insert information. it didnt work.... :P
$dbunames = mysql_query("SELECT * FROM logins WHERE uname='$uname'");
if ($uname != $dbunames)


{
mysql_query("INSERT INTO ``.`` (`fname`, `lname`, `uname`, `pword`, `email`) VALUES ('$fname', '$lname', '$uname', '$pword', '$email')");
mail($email,$subject,$message,"From:$from\r\nReply-to: $from\r\nContent-type: text/html; charset=us-ascii");
echo "<h1>Successfully added: </h1><br><h3>fname:</h3>";
echo $fname;
echo "<br /><h3>lname:</h3>";
echo $lname;
echo "<br /><h3>uname:</h3>";
echo $uname;
echo "<br /><h3>pword:</h3>";
echo $pword;
echo "<hr>";
echo "<h1>Email sent to ". $email ."</h1>";
}
else 
{
echo "Username taken.";
}
?>
<head>
<title>Insert Outcome</title>
</head>

Thanks all.

Max

Instead of

if ($uname != $dbunames)

You can do,

if(mysql_num_rows($dbunames) > 0 ) { //check if there is already an entry for that username
 echo "Already taken";
} else {
//insert to table
}

worked perfectly, thanks :)

You are welcome :)

Please Help! I am having the same problem, but my form is a little more complicated, and instead of username, i do not want duplicate gallery names. i tried the msql num rows and maybe i just don't know where to put it in the code. Also my form does not go to a different page. Also, is there a better way to validate if the fields are empty? Also, i cannot get my form to go away after the user submits a gallery- the form is still there. Thanks in advance.

<?php require_once('includes/dbc.php');?>
<?php
// define a constant for the maximum upload size
define ('MAX_FILE_SIZE', 51200);

if (array_key_exists('upload', $_POST)) {

    $gallery_name = $_POST['gallery_name'];
    $address = $_POST['address'];
	$website = $_POST['website'];
	$email = $_POST['email'];
	$phone = $_POST['phone'];
	$hours = $_POST['hours'];
	$description = $_POST['description'];
    
	
	
if(empty($gallery_name)) {
echo 'You need to enter your gallery name'. '<br/>';
$output_form =true;
}

if(empty($address)) {
echo 'You need to enter your address'. '<br/>';
$output_form =true;
}

if(empty($website)) {
echo 'You need to enter your website'. '<br/>';
$output_form =true;
}

if(empty($email)) {
echo 'You need to enter your email address'. '<br/>';
$output_form =true;
}

if(empty($phone)) {
echo 'You need to enter your phone number'. '<br/>';
$output_form =true;
}

if(empty($hours)) {
echo 'You need to enter your hours of operation'. '<br/>';
$output_form =true;
}

if(empty($description)) {
echo 'You need to enter a description'. '<br/>';
$output_form =true;
}

   




if(!empty($gallery_name) && !empty($address) && !empty($email)){	
     
	  
  // define constant for upload folder
  define('UPLOAD_DIR', 'images/');
  // replace any spaces in original filename with underscores
  // at the same time, assign to a simpler variable
  $file = str_replace(' ', '_', $_FILES['image']['name']);
  // convert the maximum size to KB
  $max = number_format(MAX_FILE_SIZE/1024, 1).'KB';
  // create an array of permitted MIME types
  $permitted = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png');
  // begin by assuming the file is unacceptable
  $sizeOK = false;
  $typeOK = false;
  
  // check that file is within the permitted size
  if ($_FILES['image']['size'] > 0 && $_FILES['image']['size'] <= MAX_FILE_SIZE) {
    $sizeOK = true;
	}

  // check that file is of an permitted MIME type
  foreach ($permitted as $type) {
    if ($type == $_FILES['image']['type']) {
      $typeOK = true;
	  break;
	  }
	}
  
  if ($sizeOK && $typeOK) {
    switch($_FILES['image']['error']) {
	  case 0:
        // check if a file of the same name has been uploaded
		if (!file_exists(UPLOAD_DIR.$file)) {
		  // move the file to the upload folder and rename it
		  $success = move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR.$file);
		 
		 
		  }else {
		  // get the date and time
		  ini_set('date.timezone', 'America/New York');
		  $now = date('Y-m-d-His');
		  $success = move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR.$now.$file);
		  }
		  

		  
	  if ($success) {
		 

		
		 
      $result = '<img src="' . UPLOAD_DIR . $file . '" alt="Gallery Image" /><p>' . $gallery_name . ' ' . $address . '<p>Successfully Added</p>';
	  
	  
	  $dbc= mysqli_connect(DB_HOST, DB_USER, DB_PSW, DB_NAME);

   
      
      // Write the data to the database
      $query = "INSERT INTO gallery_info (gallery_name, address, website, email, phone, hours, description, join_date, image)".
	  "VALUES ('$gallery_name', '$address', '$website', '$email', '$phone', '$hours', '$description', NOW(), '$file')";
      mysqli_query($dbc, $query);
	
 
      
		  
	      }
		else {
		  $result = "Error uploading $file. Please try again.";
		  }
	    break;
	  case 3:
		$result = "Error uploading $file. Please try again.";
	  default:
        $result = "System error uploading $file. Contact webmaster.";
	  }
    }
  elseif ($_FILES['image']['error'] == 4) {
    $result = 'No file selected';
	}
  else {
    $result = "$file cannot be uploaded. Maximum size: $max. Acceptable file types: gif, jpg, png.";
	}
  } 
 } 


// if the form has been submitted, display result
if (isset($result)) {
  echo "<p>$result</p>";
  
  }

		

 ?>
<form method="POST" enctype="multipart/form-data" name="uploadImage" id="uploadImage">
  <table border="0" cellpadding="0" cellspacing="0" id="tblsubmit">
    <tr>
      <th scope="row">Gallery Name:</th>
      <td><input type="text" name="gallery_name" value="" /></td>
    </tr>
    <tr>
      <th scope="row">Address:</th>
      <td><input type="text" name="address" value="" /></td>
    </tr>
    
        <tr>
      <th scope="row">Website:</th>
      <td><input type="text" name="website" value="" /></td>
    </tr>
    
        <tr>
      <th scope="row">Email:</th>
      <td><input type="text" name="email" value="" /></td>
    </tr>
    
        <tr>
      <th scope="row">Phone:</th>
      <td><input type="text" name="phone" value="" /></td>
    </tr>
    
        <tr>
      <th scope="row">Hours:</th>
      <td><input type="text" name="hours" value="" /></td>
    </tr>
    
        <tr>
      <th scope="row">Description:</th>
      <td><input type="text" name="description" value="" /></td>
    </tr>
    
       
    
    
    <tr>
      <th scope="row">Image:</th>
      <td><input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MAX_FILE_SIZE; ?>" />
        <input type="file" name="image" id="image" /> </td>
    </tr>
    <tr>
      <th scope="row">&nbsp;</th>
      <td><input type="submit" name="upload" id="button" value="Register Gallery" /></td>
    </tr>
  </table>
  
</form>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.