Solved

PHP MYSQL Unique Entry Validation

Posted on 2010-08-30
9
295 Views
Last Modified: 2013-12-13
I have a simple form that will be used by a client to enter bowlers into a database.  The table in question will be linked to another table by the field bowlerID.  There is a firstName field and lastName field.

I need suggestions on the best way to ensure that there are no duplicate combinations of firstNames and lastNames.  I have attached the code for the entry page, and the three fields above are the only fields in the table.
<?php
if($_SESSION['admin'] == true){
	if(isset($_POST['submitBowler'])){
	
	$errors = array();
	
	if(empty($_POST['firstName'])){
		$errors[] = "Please enter the bowler's first name";
	} else {
		$bowlerFirstName = $_POST['firstName'];
	}
	
	if(isset($_POST['lastName']) && $_POST['lastName'] == ""){
		$errors[] = "Please enter the bowler's last name";
	} else {
		$bowlerLastName = $_POST['lastName'];
	}
	
		if(empty($errors)){
			$q = "INSERT INTO bowlers (`firstName`, `lastName`) VALUES (?,?)";
			$stmt = mysqli_prepare($dbc, $q);
			mysqli_stmt_bind_param($stmt, "ss", $bowlerFirstName, $bowlerLastName);
			if(mysqli_stmt_execute($stmt)){
				echo '<h3>You have added '.$bowlerFirstName. ' '.$bowlerLastName.' to the database</h3>';
			} else {
				echo '<div style="padding:10px; color:red;"><h2>Error!</h2>
			<p>The following error(s) occurred:<br /> The bowler could not be added. Please contact Dennis or Allen';
			}
			mysqli_stmt_close($stmt);
	} else {
		echo '<div style="padding:10px; color:red;"><h2>Error!</h2>
			<p>The following error(s) occurred:<br />';
			foreach($errors as $msg){
				echo " - $msg<br />\n";
			}
	}
}
?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>?content=manageBowlers" method="post" enctype="multipart/form-data" name="manageBowlersForm" id="manageBowlersForm">
<table class="center" width="70%" border="0" cellspacing="0">
  <tr>
  	<td><h2>ADD A BOWLER</h2></td>
  </tr>
  <tr>
    <td width="38%"><label for="select">Bowler First Name:</label></td>
    <td width="62%"><label for="firstName"></label>
      <input type="text" name="firstName" id="firstName" />
    </td>
  </tr>
  <tr>
    <td><label for="scoreDate">Bowler Last Name:</label></td>
    <td><label for="lastName"></label>
      <input type="text" name="lastName" id="lastName" /></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><p><input type="submit" name="submitBowler" id="submitBowler" value="Add Bowler" />
      &nbsp; <a href="subpage.php?content=manageScores">Back to Add Scores</a></p></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>
</form>

<title><?php echo($title);?>Enter New Bowler for West Park Lanes</title>
<?php
} else {
	include("login.inc.php");
}
?>

Open in new window

0
Comment
Question by:DennisHacker
  • 4
  • 4
9 Comments
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 33559876
Try this: hope this helps.


<?php
$sql = "SELECT firstName FROM browlers WHERE firstName = '$firstName' AND lastName='$lastName";
$result = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($result > 0){
  echo "This user is already registered!";
}
?>

Open in new window

0
 
LVL 30

Accepted Solution

by:
Marco Gasi earned 500 total points
ID: 33559910
Sorry, I've made some little errors.
<?php
$sql = "SELECT firstName FROM browlers WHERE firstName = '$bowlerFirstName' AND lastName='$bowlerLastName";
$result = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($result > 0){
  $errors[] = "This user is already registered!";
}
?>

Open in new window

0
 

Author Comment

by:DennisHacker
ID: 33560268
I am in the midst of trying this out.  I fixed a couple of typos, but now I'm having a problem getting the page to enter anything in the database.  I will be back shortly.
0
 

Author Closing Comment

by:DennisHacker
ID: 33561203
The idea was fine.  I had to fix a couple of coding errors but I'm able to use the solution.  Thanks!
0
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 
LVL 30

Expert Comment

by:Marco Gasi
ID: 33561240
Thanks for points, but I'm curious (and I hate my errors :-)): can you tell me wich errors you have fixed?
0
 
LVL 6

Expert Comment

by:birwin
ID: 33561268
If you expect any significant number of bowlers to use the site, you will get legitimate duplicates. You are best to assign a userid to each, or use their email address as your unique identifier.
The best option is to use both. Use their email address as the user id, but assign an automatically created user id number to each user. Use the email address for login, but use the number for all database entries. That way, if the user changes their email address, you only have to change their email in the database. All historical data will still point to the user id number.
0
 

Author Comment

by:DennisHacker
ID: 33561402
birwin:

Thanks for the input.  I had considered that, but this is a small bowling center site and I personally know all of the people who bowl there.  We don't have anybody with the same name.  These aren't actually users, they will be entered by bowling center staff as they qualify for score awards.  The only login is administrative and belongs to the bowling center staff.
0
 

Author Comment

by:DennisHacker
ID: 33561418
marqusG:

In the query, the table name "bowlers" was misspelled as "browlers."

There was a missing parenthesis on Line 4 after the ">0)"

Thanks.
0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 33561434
Thank you for your kindness. Good luck for your site.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now