PHP Code to Prevent Duplicate Data Input (i.e., User Names or Members)
Let's start simple: Note that, in the following, we check through the Firstname data in the table to make sure the group member added is not a duplicate. If it is, we don't add the name, but give a warning message instead.
$dupe=0;
$myArray=array();
$res = mysql_query("SELECT Firstname FROM $B") or die(mysql_error());
while ($row = mysql_fetch_row($res)) {
array_push ($myArray, $row[0]);
}
$num=mysql_num_rows($res);
for($i=0;$i<$num;$i++){
if($A==$myArray[$i]){unset($A);$dupe=1;}
}
if($dupe==1){$dupe=0;echo '<script language="javascript">alert("This name already exists.");</script>';}else{----actually insert the new data into the table----}
Another simple example: After POSTing the inputted user name to PHP, we check out the table to see if the name given was already used. If so, we warn the user and do not molest the table. Note that one can look into a table to see if it exists, or get an array of the relevant data and look through it. Either way works.
$username = $_POST['username'];
$checkformembers = mysql_query("SELECT * FROM members WHERE username = '$username'");
if(mysql_num_rows($checkformembers) != 0){echo '<script language="javascript">alert("Username already in use. Please try again.")</script>;';
}else{----actually insert the new data into the table----}
Sometimes there's a bug in the soup. PHP 5.2.11 has a dandy in its array_unique function which is supposed to dump any duplicate names, leaving only unique data. In our tests, it killed a good name now and then as well, leaving a data gap. Notice that we defined a couple of arrays and pushed the names POSTed to PHP into the first one. Then we ran the array_keys(array_flip()) function as a workaround for the broken array_unique function. It's not the function's purpose, but it worked, which is all that matters, though we're not all that sure why it works! When we put in duplicate names, the array_keys(array_flip()) function dumped them, and the
count() function detected fewer elements in the array and this caused us to give a warning message. The MySQL INSERT loaded the table with the cleaned array data—all standard stuff.
$myArray=array();$myArray1=array();
for($i=0;$i<$num;$i++){
array_push ($myArray, $_POST['people'][$i]);
}
$myArray1=array_keys(array_flip($myArray));//array_unique has BUG!
$c=count($myArray1);
if($num>$c){echo '<script language="javascript">alert("One or more duplicate names were deleted.");</script>';}
for($i=0;$i<$c;$i++){
$b=$myArray1[$i];
$j= $i + 1;
mysql_query("INSERT INTO $a (Firstname, ID, Status, Comment)
VALUES('$b','$j','99','')");
}
The following script deals with putting input into 3 different MySQL columns/fields, and the data must stay parallel from array to array. We start by putting all the POSTed data into arrays: $myArray, $myArray1 and $myArray2. Then we go through each element of the array, comparing it to all the elements with numerically higher key numbers. If two names are found to be the same, the element value is set to an empty string and a flag is set. If the flag is set, a message lets the user know that one or more duplicate names were deleted. Then the arrays are inserted into the MySQL table, except that the elements with the empty values are bypassed—not inserted anywhere. Note that this method of avoiding duplicates utilizes no functions like the array_keys(array_flip()) function or the broken array_unique function.
$dupe=0;
for($i=0;$i<$num;$i++){
$myArray[$i]=$_POST['people'][$i];
$myArray1[$i]=$_POST['uname'][$i];
$myArray2[$i]=$_POST['upassword'][$i];
}
for($i=0;$i<$num;$i++){
if($num==1){break;}
$m=$myArray[$i];
$k=$i+1;if($k==$num){break;}
for($j=$k;$j<$num;$j++){
if($m==$myArray[$j]){$myArray[$j]="";$dupe=1;}
}
}
if($dupe==1){$dupe=0;echo '<script language="javascript">alert("One or more duplicate names were deleted.");</script>';}
$j=0;
for($i=0;$i<$num;$i++){
$b=$myArray[$i];$u=$myArray1[$i];$p=$myArray2[$i];
if($b<>""){$j = $j + 1;mysql_query("INSERT INTO $a (Firstname, ID, Status, Comment, Username, Password)
VALUES('$b','$j','99','','$u','$p')");}
}