Solved

PHP MYSQL Unique Entry Validation

Posted on 2010-08-30
9
303 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
When the s#!t hits the fan, you don’t have time to look up who’s on call, draft emails, call collaborators, or send text messages. An instant chat window is definitely the way to go, especially one like HipChat. HipChat is a true business app. An…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

623 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