Solved

PHP MYSQL Unique Entry Validation

Posted on 2010-08-30
9
298 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

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

Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
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?
An overview on how to enroll an hourly employee into the employee database and how to give them access into the clock in terminal.
XMind Plus helps organize all details/aspects of any project from large to small in an orderly and concise manner. If you are working on a complex project, use this micro tutorial to show you how to make a basic flow chart. The software is free when…

856 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