I am writing a script to register users, but I want to add an incrementing suffix to each duplicated name. The script works and posts to the database. On entering a name for the first time, everything is OK, but when I enter a duplicate name from then on, the value entered does not clear out of the text box. I've been workingon this for a while now and the kids are starting to complain.

There is a column "num" in my database which is integer and has a default value of 1. There is a fair bit of other code that I've stripped out, leaving what is necessary for someone to have a look.

Can anyone suggest how to amend the code so that the value entered is cleared after the form is processed.

<?php
session_start();
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED); 
if (isset($_POST['family']))
{
	
	$group1   = $_SESSION['group1'];
	$group2   = $_SESSION['group2'];
	$group3   = $_SESSION['group3'];
	$group4   = $_SESSION['group4'];
	$group5   = $_SESSION['group5'];
	$group6   = $_SESSION['group6'];
	//Connect to the database 
	include_once "demo_conn.php";
	// Filter the posted variables
	$family = preg_replace("/[^A-Za-z0-9 -]/", ".", $_POST['family']);//family name
	$str = $family;//convert family name to upper case
	$family = strtoupper($str);
	$given = preg_replace("/[^A-Za-z0-9]/", ".", $_POST['given']);//given name/s
	$given = ucwords($given);
	$givfam = $given .' '. $family;	//joining of given and family names
	$userGroup = $_POST['userGroup'];

	
		$result=mysql_query("SELECT MAX(num) AS maxnum FROM users WHERE userId LIKE '$givfam%'");
		$row = mysql_fetch_array($result);
		$num=$row[maxnum];
			if ($num >= 1)
			{ 
			$num = $num + 1;
			$givfam = $givfam.$num; 
			$sql = mysql_query("INSERT INTO users (userId, num, family, given, userPwd, transferCode, userCollege, userGroup, egroup, managerId) VALUES('$givfam', '$num', '$family', '$given', '$activatecode', '$transferCode','$school', '$userGroup', '$egroup', '$managerid')");
			$newvalues = mysql_query("SELECT group1, group2, group3, group4, group5, group6 FROM users");
			}
			else
			{
			$sql = mysql_query("INSERT INTO users (userId, family, given, userPwd, transferCode, userCollege, userGroup, egroup, managerId) VALUES('$givfam', '$family', '$given', '$activatecode', '$transferCode','$school', '$userGroup', '$egroup', '$managerid')");		
			$newvalues = mysql_query("SELECT group1, group2, group3, group4, group5, group6 FROM users");

			$URL='post_adduser.php';
			header ('Location: '.$URL);
}
}
?>

<head>
</head>
<body>
                <table>
                  <tr>
                    <form action="post_adduser.php" method="post" enctype="multipart/form-data" >
                        <td><input name="given" type="text" value="<?php echo "$given"; ?>" /></td>
                        <td><input name="userPwd" type="hidden" value="<?php echo "$activatecode"; ?>" /></td>
                        <td><input name="family" type="text" value="<?php echo "$family"; ?>" /></td>
                        <td>
                          <input type='radio' name='userGroup' value='<?php echo $group1; ?>'/><?php echo $group1; ?><br />
                          <input type='radio' name='userGroup' value='<?php echo $group2; ?>'/><?php echo $group2; ?><br />
                          <input type='radio' name='userGroup' value='<?php echo $group3; ?>'/><?php echo $group3; ?><br />
                          <input type='radio' name='userGroup' value='<?php echo $group4; ?>'/><?php echo $group4; ?><br />
                          <input type='radio' name='userGroup' value='<?php echo $group5; ?>'/><?php echo $group5; ?><br />
                          <input type='radio' name='userGroup' value='<?php echo $group6; ?>'/><?php echo $group6; ?><br /></td>              
                        <td><input type="submit" name="Submit" value="Submit Form" /></td>
                     </form>
                   </tr>
                 </table>

</body>
</html>
Member Avatar for diafol

> but when I enter a duplicate name from then on, the value entered does not clear out of the text box

Am I to understand that your field automatically stores a name. This may be a browser thing.

BTW: enctype Why? You're not uploading a file are you?

Also strtoupper() - be careful of this as it does not allow accented characters.

$str = mb_strtoupper($str, 'UTF-8');

... may be safer.

You also enclose variables in quotes:

<?php echo "$given"; ?>

No need for this. Just do:

<?php echo $given; ?>

You can give all variables a default value, e.g.

$given = "Enter given name here";

before your conditional.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.