Solved

PHP MYSQL Unique Entry Validation

Posted on 2010-08-30
9
299 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 31

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 31

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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 

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
 
LVL 31

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 31

Expert Comment

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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This guide will walk you through the essential considerations and tech stack for building scalable websites. Know how to grow your business the smart way!
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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 …
An overview on how to enroll an hourly employee into the employee database and how to give them access into the clock in terminal.

761 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