Jump to content

check for duplicate entry


farkewie

Recommended Posts

 

Hello i have used a 3rd party script to make a simple form where a user selects there country and it adds to a table with the username.

 

i have made it so that  2 people cant have the same country but i also want it so 2 people cant have the same username aswell can anyone help. or is the code to messy?

 

i have made it so that the tabe uname field is set to unuiqe  but i would like to display an error like the ones below. any help would be great

 

 


<?php require_once('../Connections/mail.php'); ?>
<?php
// PHPLS10 - Check for Unique Username and Insert - Part 1
function sanitize($variable){
$variable = str_replace("",";",$variable);
if(!get_magic_quotes_gpc()){
	$variable = addslashes($variable);
}
return $variable;
}
function checkPassword($password1,$password2,$minPasswordLength){
$result =0;
if(strlen($password1) >= $minPasswordLength){
	if($password1 == $password2){
		$result = $password1;
	}
}
return $result;
}

// PHPLS10 - Check for Unique Username and Insert - Part 2
if($HTTP_POST_VARS['action'] == "check"){

$minPasswordLength = 1;
$pPassword1 = sanitize($HTTP_POST_VARS['uname']);
$pPassword2 = sanitize($HTTP_POST_VARS['uname2']);
$pUsername = sanitize($HTTP_POST_VARS['country']);
$pPassword = checkPassword($pPassword1, $pPassword2, $minPasswordLength);
if((strlen($pUsername)>0) && ($pPassword !== 0)){
	$rsCheckSQL = "SELECT country FROM wii WHERE country='" . $pUsername . "'";	
	mysql_select_db($database_mail, $mail);
	$rsCheck = mysql_query($rsCheckSQL, $mail) or die(mysql_error());
	$totalRows_rsCheck = mysql_num_rows($rsCheck);
	if($totalRows_rsCheck < 1){
		$rsCheckSQL = "INSERT INTO wii (country, uname) VALUES ('" . $pUsername . "','" . $pPassword . "')";
		$rsCheck = mysql_query($rsCheckSQL, $mail) or die(mysql_error());
		$phplsMessage = "<p><font size=\"3\" face=\"Verdana, Arial\" color=\"#FF0000\"><b>Good luck   " . $pUsername . " </b></font></p> ";

	} else {
		mysql_free_result($rsCheck);
		$phplsMessage = "<p><font size=\"3\" face=\"Verdana, Arial\" color=\"#FF0000\"><b>Sorry  " . ucfirst ($pPassword1) . " , " . $pUsername . "  is taken!!</b></font></p>";
	}
}else{
	$phplsMessage = "<p><font size=\"3\" face=\"Verdana, Arial\" color=\"#FF0000\"><b>Something Fucked Up please email me to let me know</b></font></p>";
}
}
?>
<?php echo $phplsMessage; ?><form action="<?php echo $PHP_SELF; ?>" method="POST" name="wii" dir="ltr" lang="en">
  <select name="country">
    <option value="Afghanistan">Afghanistan</option>
    <option value="Zambia">Zambia</option>
    <option value="Zimbabwe">Zimbabwe</option>
  </select>
  <input type="submit" name="Submit" value="Submit" />
  <input name="uname" type="hidden" value="<? echo $check["uname"] ?>" />
    <input name="uname2" type="hidden" value="<? echo $check["uname"] ?>" />
    <input name="action" type="hidden" id="action" value="check">

    <br />
    Once country you press submit you cannot change it... <br />
</form>

Link to comment
https://forums.phpfreaks.com/topic/43462-check-for-duplicate-entry/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.