I have this for my register form that if it exists die which falgs up a new blank page that displays the message but i just want the error to stop the script and appear on the form.

if(usernameTaken($_POST['user'])){
      $use = $_POST['user'];
      die("Sorry, the username: <strong>$use</strong> is already taken, please pick another one.");
}

Recommended Answers

All 6 Replies

in between the strong tags you have $use dont you mean $user

why dont you try

<? if(usernameTaken($_POST['user'])){
      $use = $_POST['user'];
}else{
		 echo"Sorry, the username: <strong>$user</strong> is already taken, please pick another one.";
}
?>

yes sorry i did mean user but it still doesnt work becuase it still adds it to the database, i need it to stop the script and echo a msg

thanks for the reply

hmm the die you have should work. but try using an exit

<? if(usernameTaken($_POST['user'])){
      $use = $_POST['user'];
}else{
         echo"Sorry, the username: <strong>$user</strong> is already taken, please pick another one.";
exit();
}
?>

Nope still not working

here is my entire code

<?php
session_start(); 
include("database.php");
include("login.php");

/**
 * Returns true if the username has been taken
 * by another user, false otherwise.
 */
function usernameTaken($username){
   global $conn;
   if(!get_magic_quotes_gpc()){
      $username = addslashes($username);
   }
   $q = "select username from customer where username = '$username'";
   $result = mysql_query($q,$conn);
   return (mysql_numrows($result) > 0);
}

/**
 * Inserts the given (username, password) pair
 * into the database. Returns true on success,
 * false otherwise.
 */
function addNewUser($username, $password){
   global $conn;
   $q = "INSERT INTO customer (Firstname, Surname, Company, Email, Type, Tel, Mob, username, password) VALUES ('$_POST[first]', '$_POST[surn]', '$_POST[comp]', '$_POST[email]', '$_POST[type]', '$_POST[tel]', '$_POST[mob]', '$username', '$password')";
   return mysql_query($q,$conn);
}

/**
 * Displays the appropriate message to the user
 * after the registration attempt. It displays a 
 * success or failure status depending on a
 * session variable set during registration.
 */
function displayStatus(){
   $uname = $_SESSION['reguname'];
   if($_SESSION['regresult']){
	   echo ("Registered!");
	   }
	 else{ 
	 
?>
		<form action="<?php echo $HTTP_SERVER_VARS['PHP_SELF']; ?>" method="post">
			<h1>Register Form</h1>
			<p>Personal Details</p>

			<label>First Name:<span class="small">Add your name</span></label><input type="text" name="first" />
            <label>Surname:<span class="small">Add your name</span></label><input type="text" name="surn" />
            <label>Company:<span class="small">Add your name</span></label><input type="text" name="comp" />
            <label>Email:<span class="small">Add a valid address</span></label><input type="text" name="email" />
            <label>Customer<span class="small">Add your name</span></label><input type="radio" name="type" align="left" value="cus" />
            <label>Stockist<span class="small">Add your name</span></label><input type="radio" name="type" align="left" value="stk" />
            <label>Telephone:<span class="small">Add a valid address</span></label><input type="text" name="tel" />
            <label>Mobile:<span class="small">Add a valid address</span></label><input type="text" name="mob" />
			<label>Username:<span class="small">Min. size 8 chars</span></label><input type="text" name="user" />
			<label>Password:<span class="small">Min. size 6 chars</span></label><input type="password" name="pass" />
			<button type="submit" name="subjoin">Sign-up</button>
            <input type="hidden" name="MM_insert" value="fact_form_id" />
		</form>
<?php
}
?>
<!--************************************************--!>
<?php
   
   unset($_SESSION['reguname']);
   unset($_SESSION['registered']);
   unset($_SESSION['regresult']);
}

if(isset($_SESSION['registered'])){
/**
 * This is the page that will be displayed after the
 * registration has been attempted.
 */
?>

<?php
   return;
}

/**
 * Determines whether or not to show to sign-up form
 * based on whether the form has been submitted, if it
 * has, check the database for consistency and create
 * the new account.
 */
if(isset($_POST['subjoin'])){
   /* Make sure all fields were entered */
   if(!$_POST['user'] || !$_POST['pass']){
      die('You didn\'t fill in a required field.');
   }

   /* Spruce up username, check length */
   $_POST['user'] = trim($_POST['user']);
   if(strlen($_POST['user']) > 30){
      die("Sorry, the username is longer than 30 characters, please shorten it.");
   }
  

   /* Check if username is already in use */
   if(usernameTaken($_POST['user'])){
      $user = $_POST['user'];
      die("Sorry, the username: <strong>$user</strong> is already taken, please pick another one.");
   }


   /* Add the new account to the database */
   $md5pass = md5($_POST['pass']);
   $_SESSION['reguname'] = $_POST['user'];
   $_SESSION['regresult'] = addNewUser($_POST['user'], $md5pass);
   $_SESSION['registered'] = true;
   echo "<meta http-equiv=\"Refresh\" content=\"0;url=$HTTP_SERVER_VARS[PHP_SELF]\">";
   return;
}
else{
/**
 * This is the page with the sign-up form, the names
 * of the input fields are important and should not
 * be changed.
 */
?>

<?php
}
?>

sorry i had the unset sessions in the wrong place and was closing the session before id finished with it

thanks

nope i was wrong now its dismantling the page

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.