Quantcast
Channel: Sutana Ryan's Blog and Portfolio Site » sign in
Viewing all articles
Browse latest Browse all 2

PHP and MySQL Sign Up Script

$
0
0

One of the most common site functionality is Sign Up or Registration page for Application, Mail purposes and many more, this also help to improve site organic traffic by providing great tutorial via email and today I will try to share another great PHP and MySQL tutorial which is PHP and MySQL Sign Up Script.

Before we begin you need to have full access in your site PHPMyAdmin and a basic knowledge in PHP and MySQL.

Okay Let’s start.

Demo

PHP and MySQL Sign Up Page

Database

Table structure for table ‘signup’.

CREATE TABLE IF NOT EXISTS 'signup' (
  'id' int(11) NOT NULL AUTO_INCREMENT,
  'firstname' varchar(225) NOT NULL,
  'lastname' varchar(225) NOT NULL,
  'email' varchar(225) NOT NULL,
  're_email' varchar(225) NOT NULL,
  'password' varchar(225) NOT NULL,
  'gender' varchar(225) NOT NULL,
  PRIMARY KEY ('id'),
  UNIQUE KEY 'email' ('email')
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

config.php

Database configuration file, this information includes our hostname, mysql username, mysql password and database name.

<?php
$host='localhost';
$user = 'root';
$pass = '';
$db='signup';

$con = mysql_connect($host, $user, $pass) or die('Error Connecting'.mysql_error());
mysql_select_db($db, $con) or die('Database not selected ::'. mysql_error());
?>

index.php

This contains HTML and PHP code for our sign up page

<div class="container">
	<?php
		include_once('config.php');
		
		$errs = '';
		$success = '';
		if(isset($_POST['task'])) {
			
			$firstname = mysql_real_escape_string($_POST['firstname']);
			$lastname = mysql_real_escape_string($_POST['lastname']);
			$email = mysql_real_escape_string($_POST['email']);
			$re_email = mysql_real_escape_string($_POST['re_email']);
			$gender = mysql_real_escape_string($_POST['gender']);
			$password = mysql_real_escape_string($_POST['password']);
			
			if( $firstname == "" || $lastname == "" || $email == "" || $re-email == "" || $gender == "" || $password == "") {
				$errs='<p class="error">You must fill in all of the fields.</p>';
			} else if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
				$errs='<p class="error">Please enter a valid email address.</p>';
			} else if( $email <> $re_email ) {
				$errs='<p class="error">Your emails do not match. Please try again.</p>';
			} else {
				$sql = "INSERT INTO signup(firstname, lastname, email, re-email, gender, password) VALUES ('$firstname', '$lastname', '$email', '$re_email', '$gender', '".md5($password)"')";
				$qry = mysql_query( $sql );
				
				if( $qry ):
					$success = '<p class="success">You\'re Successfully registered.</p>';
					unset($_POST);
				endif;
				
			}
		}
	?>

	<form method="post" action="index.php">
		<fieldset>
			<input type="hidden" name="task" value="signup" />
			<table border="0" cellpadding="2" cellspacing="2">
				<tr>
					<td colspan="2" class="heading">PHP and MySQL Sign Up Page</td>
				</tr>
				<tr>
					<td colspan="2">
						<div><?php echo $errs; ?></div>
						<div><?php echo $success; ?></div>
						<div><?php echo (isset($_GET['log']) ? '<p class="success">You are now logged out.</p>' : ''); ?></div>
					</td>
				</tr>
				<tr>
					<td><label for="firstname">First Name:</label></td>
					<td><input type="text" name="firstname" value="<?php echo (isset($_POST['firstname']) ? $_POST['firstname'] : '') ?>"  /></td>
				</tr>
				<tr>
					<td><label for="lastname">Last Name:</label></td>
					<td><input type="text" name="lastname" value="<?php echo (isset($_POST['lastname']) ? $_POST['lastname'] : '') ?>" /></td>
				</tr>
				<tr>
					<td><label for="email">Your Email:</label></td>
					<td><input type="text" name="email" value="<?php echo (isset($_POST['email']) ? $_POST['email'] : '') ?>" /></td>
				</tr>
				<tr>
					<td><label for="re-email">Re-enter Email:</label></td>
					<td><input type="text" name="re_email" value="<?php echo (isset($_POST['re_email']) ? $_POST['re_email'] : '') ?>" /></td>
				</tr>
				<tr>
					<td><label for="password">Password:</label></td>
					<td><input type="password" name="password" value="<?php echo (isset($_POST['password']) ? $_POST['password'] : '') ?>" /></td>
				</tr>
				<tr>
					<td><label for="password">Gender:</label></td>
					<td>
						<select name="gender">
							<option value="">Select Gender:</option>
							<option value="female" <?php echo (isset($_POST['gender']) ? (($_POST['gender']=='female') ? 'selected="true"' : '') : '' ) ?> >Female</option>
							<option value="male" <?php echo (isset($_POST['gender']) ? (($_POST['gender']=='male') ? 'selected="true"' : '') : '' ) ?> >Male</option>
						</select>
					</td>
				</tr>
				<tr>
					<td><input type="submit" value="Sign Up" /></td>
				</tr>
			</table>
		</fieldset>
	</form>
</div>

style.css

To look our Sign Up page alive

body{ font: normal 14px Tahoma;}
fieldset, img { border: 0;}
input[type="text"], input[type="password"]{ width: 200px;}
input[type="text"], input[type="password"], textarea, select { padding: 4px;}

table { 
   background: #F9F9F9; 
   border: 1px solid #ccc; 
   padding: 8px;
}
table tr td label{ 
   font: bold 12px Tahoma; 
   color: #666666;
}

.heading { 
   font-weight: bold; 
   border-bottom: 1px solid #DADADA; 
   padding: 10px 2px;
}
p.error { background: #FFEBE8; 
   border: 1px solid #DD3C10; 
   font: normal 12px Tahoma; 
   padding: 8px; 
   color: #DF2839;
}
p.success { 
   background: #FFFFE0; 
   border: 1px solid #E6DB55;  
   font: normal 12px Tahoma; 
   padding: 8px; 
   color: #000;
}

That’s it, you may be also interested in my other PHP and MySQL tutorial which is PHP and MySQL Login Script.

Happy coding ^_^


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images