Register Form Problems

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mmc01ms
Forum Commoner
Posts: 97
Joined: Wed Dec 01, 2004 3:33 am
Location: Nottingham, UK

Register Form Problems

Post by mmc01ms »

I've sorted out my inital problems with this script however now the problem i can't sort out is that it wont insert anything into my database it prints out its the error message if the insert has been unsucessful:

Code: Select all

if($succesful!=1){
			error_message("Error Signing Up");
Full code is:

Code: Select all

<?php 
	//some of this script created with reference from Beginning PHP4 from WROX Publishing on 01/12/2004
	//Written by Manpreet Sandhu
	$login_script = "./login3.php";
	
	//produce error message in alert box when user clicks ok return to previous page.
	
	function error_message($msg){
		echo "<script>alert("$msg");history.go(-1)</script>";
		exit;
	}
	
	function in_use($username){
		
		require ("dbConnect.php");
		
		$username = $_POST['username'];
		
		
		$connection = mysql_connect("$dbhost","$dbusername", "$dbpasswd")
			or die ("Couldn't connect to server.");
		
		$db = mysql_select_db("$database_name", $connection)
			or die("Couldn't select database.");
		
		$dbQuery = "SELECT username FROM customers WHERE username = '$username' ";
		$result = mysql_query($dbQuery);
		if(!mysql_num_rows($result)) return 0;
		else return 1;
	}	
	
	function register_form(){
		global $username, $firstName, $lastName, $inital, $addressLineOne, $addressLineTwo, $city, $postcode, $homePhoneNumber, $mobilePhoneNumber, $email, $password1, $password1, $password2, $dateofBirth;
		global $PHP_SELF;
?>
<html>
	<head>
		<title>Register Form</title>
	</head>
	<body>
		<center><p>Please complete this form and fill out all fields labeled with a (*)<br>
			No details will be passed to 3rd party companies<br></p>
			
			<form name="register_form" method="post" action="register.php?action=register">
				<table>
					<tr>
						<td>* Enter Desired Username: </td>
						<td><input name="username" type="text" maxlength="10" id="username"></td>
					</tr>
					<tr>
						<td>* Enter Desired Password: </td>
						<td><input name="password1" type="password" maxlength="10" id="password1"></td>
					</tr>
					<tr>
						<td>* Type Password Again: </td>
						<td><input name="password2" type="password" maxlength="10" id="password2"></td>
					</tr>
					<tr>
						<td>* Email: </td>
						<td><input name="email" type="text" maxlength="40" id="email"></td>
					</tr>
					<tr>
						<td>* First Name: </td>
						<td><input name="firstName" type="text" maxlength="20" id="firstName"></td>
					</tr>
					<tr>
						<td>* Last Name: </td>
						<td><input name="lastName" type="text" maxlength="20" id="lastName"></td>
					</tr>
					<tr>
						<td>* Inital: </td>
						<td><input name="inital" type="text" maxlength="4" id="inital"></td>
					</tr>
					<tr>
						<td>* Address Line One: </td>
						<td><input name="addressLineOne" type="text" maxlength="25" id="addressLineOne"></td>
					</tr>
					<tr>
						<td>* Address Line Two: </td>
						<td><input name="addressLineTwo" type="text" maxlength="25" id="addressLineTwo"></td>
					</tr>
					<tr>
						<td>* City:  </td>
						<td><input name="city" type="text" maxlength="15" id="city"></td>
					</tr>
					<tr>
						<td>County:  </td>
						<td><input name="county" type="text" maxlength="15" id="county"></td>
					</tr>
					<tr>
						<td>* Postcode:  </td>
						<td><input name="postcode" type="text" maxlength="8" id="postcode"></td>
					</tr>
					<tr>
						<td>* Home Phone Number:  </td>
						<td><input name="homePhoneNumber" type="text" maxlength="11" id="homePhoneNumber"></td>
					</tr>
					<tr>
						<td>* Mobile Number:  </td>
						<td><input name="mobilePhoneNumber" type="text" maxlength="11" id="mobilePhoneNumber"></td>
					</tr>
					<tr>
						<td>* Date of Birth:  </td>
						<td>Day: <select name ="day">
								<option>1</option>
								<option>2</option>
								<option>3</option>
								<option>4</option>
								<option>5</option>
								<option>6</option>
								<option>7</option>
								<option>8</option>
								<option>9</option>
								<option>10</option>
								<option>11</option>
								<option>12</option>
								<option>13</option>
								<option>14</option>
								<option>15</option>
								<option>16</option>
								<option>17</option>
								<option>18</option>
								<option>19</option>
								<option>20</option>
								<option>21</option>
								<option>22</option>
								<option>23</option>
								<option>24</option>
								<option>25</option>
								<option>26</option>
								<option>27</option>
								<option>28</option>
								<option>29</option>
								<option>30</option>
								<option>31</option>
							</select>
							Month: <select name ="month">
								<option>1</option>
								<option>2</option>
								<option>3</option>
								<option>4</option>
								<option>5</option>
								<option>6</option>
								<option>7</option>
								<option>8</option>
								<option>9</option>
								<option>10</option>
								<option>11</option>
								<option>12</option>
							</select>
							Year: <select name ="year">
								<option>1950</option>
								<option>1951</option>
								<option>1952</option>
								<option>1953</option>
								<option>1954</option>
								<option>1955</option>
								<option>1956</option>
								<option>1957</option>
								<option>1958</option>
								<option>1959</option>
								<option>1960</option>
								<option>1961</option>
								<option>1962</option>
								<option>1963</option>
								<option>1964</option>
								<option>1965</option>
								<option>1966</option>
								<option>1967</option>
								<option>1968</option>
								<option>1969</option>
								<option>1970</option>
								<option>1971</option>
								<option>1972</option>
								<option>1973</option>
								<option>1974</option>
								<option>1975</option>
								<option>1976</option>
								<option>1977</option>
								<option>1978</option>
								<option>1979</option>
								<option>1980</option>
								<option>1981</option>
								<option>1982</option>
								<option>1983</option>
								<option>1984</option>
								<option>1985</option>
								<option>1986</option>
								<option>1987</option>
								<option>1988</option>
							</select>
						</td>
					</tr>
					<tr>
						<td>
							<input type="submit" name="Submit" value="Submit">
							<input type="reset" name="Reset" value="Reset">
						</td>
					</tr>
				</table>
				
				
			</form>
		</center>
	</body>
</html>

<?php
	}
	
	function create_account() {
		
		$username = $_POST['username'];
		$firstName = $_POST['firstName'];
		$lastName = $_POST['lastName'];
		$inital = $_POST['inital'];
		$addressLineOne = $_POST['addressLineOne'];
		$addressLineTwo =$_POST['addressLineTwo'];
		$city = $_POST['city'] ;
		$county = $_POST['county'] ;
		$postcode = $_POST['postcode'];
		$homePhoneNumber = $_POST['homePhoneNumber'] ;
		$mobilePhoneNumber = $_POST['mobilePhoneNumber'] ;
		$email = $_POST['email'] ;
		$password1 = $_POST['password1'] ;
		$password2 = $_POST['password2'] ;
		$Date_of_Birth = $_POST['year']."-".$_POST['month']."-".$_POST['day'];
		
		if($password1 != $password2) error_message("Passwords do not match please re-enter them.");
		if(empty($username)) error_message("Enter your desired Username.");
		if(empty($password1)) error_message("Please enter a password.");
		if(empty($firstName)) error_message("Please enter a firstname.");
		if(empty($lastName)) error_message("Please enter a last name.");
		if(empty($email)) error_message("Please a enter an email.");
		if(strlen($password1 < 4 )) error_message("Password too short must be more then 4 characters.");
		if(empty($password2)) error_message("Please re-enter second password for verification.");
		if(empty($email)) error_message("Enter your email.");
		if(empty($addressLineOne)) error_message("Enter your address.");
		if(empty($addressLineTwo)) error_message("Enter the second line of your address.");
		if(empty($postcode)) error_message("Please enter your postcode.");
		if(empty($city)) error_message("Enter the city you live in.");
		if(empty($homePhoneNumber)) error_message("Please enter a home phone number.");
		if(empty($mobilePhoneNumber)) error_message("Please enter a mobile phone number.");
		if(empty($inital)) error_message("Please enter your title.");
		if(strlen($homePhoneNumber  < 11)) error_message("Invalid Home Phone Number.");
		if(strlen($mobilePhoneNumber < 11)) error_message("Invalid Mobile Phone Number.");
		if(empty($Date_of_Birth)) error_message("Invalid Birth Date");
		
		require ("dbConnect.php");
		
		
		$connection = mysql_connect("$dbhost","$dbusername", "$dbpasswd")
			or die ("Couldn't connect to server.");
		
		$db = mysql_select_db("$database_name", $connection)
			or die("Couldn't select database.");
		
		
		if(in_use($username))
			error_message("$username is in use. Please choose a diffrent ID.");
		
		
		if(!$county){
			
			$query = "insert into customers values(NULL, '$firstName', '$lastName', '$inital', '$addressLineOne', '$addressLineTwo', '$city','$postcode', '$homePhoneNumber', '$mobilePhoneNumber', '$email', '$Date_of_Birth', '$password1', NULL, NULL)";
			mysql_query($query, $db_conn);
			$results = mysql_affected_rows();
			
			if($results>0) {
				$succesful=1;
			}
			else{
				register_form();
			}
		}else
		{
			$query = "insert into customers values(NULL, '$firstName', '$lastName', '$inital', '$addressLineOne', '$addressLineTwo', '$city','$county', '$postcode', '$homePhoneNumber', '$mobilePhoneNumber', '$email', '$Date_of_Birth', '$password1', NULL, NULL)";
			$results = mysql_affected_rows();
			
			if($results>0) {
				$succesful=1;
			} else {
				register_form();
			}
		}
		
		if ($succesful==1){
			echo $firstName
				." "
				. $lastName 
				. ", You have succesfully become a customer of Funky Monkey Records. You will recieve a validation e-mail so you can activiate your account.<br><br>
						";
				$sql = mysql_query("Select customer_id from customers where username = '$username'");
			$row = mysql_fetch_array($sql);
			$customer_id = $row['customer_id'];
			
			$emailMessage = "<html><body>Dear ". $firstName . " " . $lastName. "<br><br>
				You have registred an account at Funky Monkey Records. In order to complete registration you have to click this activation
				link:<br><a href="validate.php?page=validate&customer_id=" . $customer_id . "">validate.php?page=validation&customer_id=" . $customer_id . "</a></body></html>";
			
			$to = $email;
			$from = "mmc01ms@hotmail.com";
			$message= $emailMessage;
			$subject = "Succesfully registred as ". $username;
			$headers = "From: $from";
			if(mail($to,$subject,$message,$headers)){
			} else{
				echo"<br><br>Failed sending an email";
			}
		}
		if($succesful!=1){
			error_message("Error Signing Up");
		}
	}
	
	if($_GET['action'] == "register")
	{
		create_account();
		exit;
		break;
	}else
	
	if($_GET['action'] == "login")
	{
		header("Location: login3.php");
		exit;
		break;
	}
	else
		register_form();
	
?>
robjime
Forum Commoner
Posts: 46
Joined: Sat Apr 03, 2004 12:26 pm
Location: the RO

Post by robjime »

did you run the mysql_query on all the instances you needed to?
iandunn
Forum Newbie
Posts: 16
Joined: Fri Dec 03, 2004 7:44 am
Contact:

Post by iandunn »

I don't have PHP or MySQL on this workstation, so forgive me if this is wrong, but the problem might be that you're not pulling single quotes around NULL. I can't remember if they're required or not, but I always use them and I don't see anything else wrong w/ your SQL statement.

It'd help a lot if you could post the actual error message you're recieving back from MySQL instead of just "Error Signing Up."
robjime
Forum Commoner
Posts: 46
Joined: Sat Apr 03, 2004 12:26 pm
Location: the RO

Post by robjime »

also u might want to try using a == instead of a =, the double equal signs compare, where as the single equal signs sets a variable
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

You're missing the mysql_query line in code thats run if no county is present. If you're still having problems, make your mysql_query line like this:

Code: Select all

$result = mysql_query($query) or die(mysql_error()."<pre>$query</pre>");
That'll give you the error and the query if it fails. run if no county is present. If you're still having problems, make your mysql_query line like this:

Code: Select all

$result = mysql_query($query) or die(mysql_error()."<pre>$query</pre&amp still having problems, make your mysql_query line like this:

Code: Select all

$result = mysql_query($query) or die(mysql_error().&quote;&lt;pre&gt;$query&lt;/pre&gt;&quote;);
That'll give you the error and the query if it fails.ke this:

Code: Select all

$result = mysql_query($query) or die(mysql_error()."<pre>$query</pre>");
That'll give you the error and the query if it f mysql_query line like this:

Code: Select all

$result = mysql_query($query) or die(mysql_error()."<pre>$query</pre>");
That'll give you the error and the query if it fails. mysql_query line in code thats run if no county is present. If you're still having problems, make your mysql_query line like this:

Code: Select all

$result = mysql_query($query) or die(mysql_error()."<pre>$query</pre>");[/php:1:e91023ca mysql_query line in code thats run if no county is present.  If you're still having problems, make your mysql_query line like this:

Code: Select all

$result = mysql_query($query) or die(mysql_error()."<pre>$query</pre>");
That'll give you the error and the query if it fails.e still having problems, make your mysql_query line like this:

Code: Select all

$result = mysql_query($query) or die(mysql_error()."<pre>$query</pre>");
That'll give you the error and the query if it fails., make your mysql_query line like this:

Code: Select all

$result = mysql_query($query) or die(mysql_error()."<pre>$query</pre>");
That'll give you the error and the query if it fails.no county is present. If you're still having problems, make your mysql_query line like this:

Code: Select all

$result = mysql_query($query) or die(mysql_error()."<pre>$query</pre>");
That'll give you the error and the query if it fails.sql_error()."<pre>$query</pre>");

That'll give you the error and the query if it fails.it fails.line in code thats run if no county is present. If you're still having problems, make your mysql_query line like this:

Code: Select all

$result = mysql_query($query) or die(mysql_error()."<pre>$query</pre>");
That'll give you the error and the query if it fails.amp;lt;/pre>");

That'll give you the error and the query if it fails."<pre>$query</pre>");

That'll give you the error and the query if it fails.p;quote;);

That'll give you the error and the query if it fails. mysql_query line in code thats run if no county is present. If you're still having problems, make your mysql_query line like this:

Code: Select all

$result = mysql_query($query) or die(mysql_error()."<pre>$query</pre>");
That'll give you the error and the query if it fails.ts run if no county is present. If you're still having problems, make your mysql_query line like this:

Code: Select all

$result = mysql_query($query) or die(mysql_error()."<pre>$query</pre>");
That'll give you the error and the query if it fails.hats run if no county is present. If you're still having problems, make your mysql_query line like this:

Code: Select all

$result = mysql_query($query) or die(mysql_error()."<pre>$query</pre>");
That'll give you the error and the query if it fails.pre>$query</pre>");

That'll give you the error and the query if it fails.or()."<pre>$query</pre>");

That'll give you the error and the query if it fails.uery if it fails.blems, make your mysql_query line like this:

Code: Select all

$result = mysql_query($query) or die(mysql_error()."<pre>$query</pre>");
That'll give you the error and the query if it fails.ne in code thats run if no county is present. If you're still having problems, make your mysql_query line like this:

Code: Select all

$result = mysql_query($query) or die(mysql_error()."<pre>$query</pre>");
That'll give you the error and the query if it fails. mysql_query line in code thats run if no county is present. If you're still having problems, make your mysql_query line like this:

Code: Select all

$result = mysql_query($query) or die(mysql_error()."<pre>$query</pre>")error()."<pre>$query</pre>");
That'll give you the error and the query if it fails.s present. If you're still having problems, make your mysql_query line like this:

Code: Select all

$result = mysql_query($query) or die(mysql_error()."<pre>$query</pre>");
That'll give you the error and the query if it failsql_query($query) or die(mysql_error().&quote;&lt;pre&gt;$query&lt;/pre&gt;&quote;);

That'll give you the error and the query if it fails. mysql_query line in code thats run if no county is present. If you're still having problems, make your mysql_query line like this:

Code: Select all

$result = mysql_query($query) or die(mysql_error()."<pre>$query</pre>");
That'll give you tuery) or die(mysql_error()."<pre>$query</pre>");

That'll give you the error and the query if it fails.1023caf4]$result = mysql_query($query) or die(mysql_error()."<pre>$query</pre>");

That'll give you the error and the query if it fails.mysql_query($query) or die(mysql_error()."<pre>$query</pre>");

That'll give you the error and the query if it fails. mysql_query line in code thats run if no county is present. If you're still having problems, make your mysql_query line like this:

Code: Select all

$result = mysql_query($query) or die(mysql_error()."<pre>$query</pre>");
That'll give you the errf you're still having problems, make your mysql_query line like this:

Code: Select all

$result = mysql_query($query) or die(mysql_error()."<pre>$query</pre>");
That'll give you the error and the query if it fails.e thats run if no county is present. If you're still having problems, make your mysql_query line like this:

Code: Select all

$result = mysql_query($query) or die(mysql_error()."<pre>$query</pre>");
That'll give you the error and the query if it fails.$result = mysql_query($query) or die(mysql_error()."<pre>$query</pre>");

That'll give you the error and the query if it fails.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply