Problem!

A

Anonymous

Guest
Hi all

I have a problem with the following code:

Code:
<?
 //error checking
if (empty($username) || empty($name) || empty($email) || empty($email1)) {
$message = "You did not fill in all the correct fields please try again!";
include "index.php";
}
elseif
(
$tos != "yes"
)
	{ 
	$message = "You must agree to the Terms and Conditions!";
	include "index.php";
	}
elseif
(
$email != $email1
)
{
$message = "You email addresses didnt match! Please try again!";
include "index.php";
}
include "../../connections/connect.php";
include "../../connections/select_db_members.php";
$query = "SELECT * FROM members WHERE username = '$username'";
	$result = mysql_query($query)
		or die (mysql_error());
	$num = mysql_num_rows($result);
	if ($num > 0) 
	{
	$message = "The username you have chosen is already in use, please try again!";
	include "index.php";
	}
else
{
	include "../../connections/connect.php";
	include "../../connections/select_db_members.php";

$pass=date("m-i").time();  
$password = md5($pass);
$ip = $_SERVER['REMOTE_ADDR'];
$dob = "dob_day - $dob_month - $dob_year";
$query = "INSERT INTO members(username,password,name,email,dob,ip,status,last_login) VALUES('$username','$password','$name','$email','$dob','$ip','user',NOW())";
	$result = mysql_query($query);
$to = $email;
$from = "From: Dance-Nation";
$subject = "Application To Dance Nation!";
$message = "
Dear $name
	Thankyou for your application to dance-nation.co.uk
	Your details are as follows:
	Username = $username
	Password = $pass
	
	Your password has been automatically generated and we recommend you change this password on your first login!
	Welcome to Dance-Nation!
	If you have nany problems please email support@dance-nation.co.uk
	
	Cheers!
	Dan (Dance-nation owner!)
	dan@dance-nation.co.uk";
mail($to,$subject,$message,$from);
$message = "Your registration was successful! You will recieve your password shorty and will then be able to login!";
include "../login.php";
}
?>

I have a problem with it where everytime i run the script even if it finds an error it will run the query and insert the info into the database.
I cant seem to find a way to stop it doing this and it is a real pain!

Hope someone can help me!
Cheers!
 
Do you mean the statements at the top where you include the index.php? If that is the areay, try putting:

exit();

after each include so it stops the current script for executing.
 
No its the final query qhich inserts the information into the database at the end,

for some reason even if it reaches one of the errors on the way through it still runs that query.
 
This is not tested, but I think it should work when bugged out, also I wonder where you are getting the values for $username, $name, $email, $email1, and $tos. If you are passing those with a form may I suggest replaceing $x with $_POST[x] or $_GET[x] depending on which method you use with forms. That is of course if you were using forms to pass the information, and you have not translated these elsewhere that I an unaware of.
Code:
<? 
//make sure all required fields are not empty
	if (empty($username) || empty($name) || empty($email) || empty($email1)) { 
		$message = "You did not fill in all of the correct fields please try again!"; 
		include "index.php";
	} else {
		
       		if ($tos != "yes") { 
  			$message = "You must agree to the Terms and Conditions!"; 
   			include "index.php"; 
   		} elseif ($email != $email1) { 
			$message = "Your email addresses did not match! Please try again."; 
			include "index.php"; 
		} else {
			include "../../connections/connect.php"; 
			include "../../connections/select_db_members.php"; 
			$query = "SELECT * FROM members WHERE username = '$username'"; 
   			$result = mysql_query($query) 
    				or die (mysql_error()); 
   			$num = mysql_num_rows($result); 
   				if ($num > 0) {
   				$message = "The username you have chosen is already in use! Please try again."; 
   				include "index.php"; 
   			} else { 
   				include "../../connections/connect.php"; 
   				include "../../connections/select_db_members.php"; 

				$pass=date("m-i").time();  
				$password = md5($pass); 
				$ip = $_SERVER['REMOTE_ADDR']; 
				$dob = "dob_day - $dob_month - $dob_year"; 
				$query = "INSERT INTO members(username,password,name,email,dob,ip,status,last_login) VALUES('$username','$password','$name','$email','$dob','$ip','user',NOW())"; 
	   			$result = mysql_query($query); 
				$to = $email; 
				$from = "From: Dance-Nation"; 
				$subject = "Application To Dance Nation!"; 
				$message = "Dear" . $name . "Thank you for your application to dance-nation.co.uk. Your details are as follows: Username = " . $username . "Password = " . $pass . "Your password has been automatically generated and we recommend you change this password on your first login! Welcome to Dance-Nation! If you have nany problems please email support@dance-nation.co.uk<p><p>Cheers!<br>Dan (Dance-nation owner!)<br>dan@dance-nation.co.uk"; 
				mail($to,$subject,$message,$from); 
				$message = "Your registration was successful! You will recieve your password shorty and will then be able to login!"; 
				include "../login.php"; 
			}
		}
	}
?>

I always have problems when I use alot of if, elseif, and else. Making my code structured with tabs helps me keep track. I hope you are using a good text editor. If you would like to look around I recommend Vim. It is available for many os's including windows.[/code]
 
Back
Top