Solved

PHP MYSQL Unique Entry Validation

Posted on 2010-08-30
9
297 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
3 Use Cases for Connected Systems

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

 

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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Issue with using GPO to install Mimecast in Outlook. 6 35
Help with query 3 25
Map software access 4 31
Edit image captured by printScreen 4 21
Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
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.
The viewer will learn how to dynamically set the form action using jQuery.
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…

777 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