register.php
Code: Select all
<?PHP
$host="localhost";
$dbusername="username";
$dbpassword="mypass";
$db_name="dbname";
$tbl_name="users";
mysql_connect("$host", "$dbusername", "$dbpassword")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$errors = "";
if (!isset($_POST['username']))
$errors .= "Please provide an email. <br/>";
if (!isset($_POST['password']))
$errors .= "Please provide a password. <br/>";
if ($errors == "") {
mysql_query("INSERT INTO users VALUES(
'".addslashes($_POST['username'])."',
'".md5($_POST['password'])."',
'".time()."'
)") or die(mysql_error());
echo "Registration Successful!";
} else {
echo $errors."Please go back and try again.";
}
?>Code: Select all
<?php
$host="localhost";
$dbusername="username";
$dbpassword="mypass";
$db_name="dbname";
$tbl_name="users";
mysql_connect("$host", "$dbusername", "$dbpassword")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$username=$_POST['username'];
$password=$_POST['password'];
$encrypted_password=md5($password);
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
$sql="SELECT * FROM $tbl_name WHERE username='$username' and password='$encrypted_password'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
if($count==1){
session_start();
session_register("username");
session_register("password");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>Basically my forms have two fields - email (used as username) and password. However, I want to make them required so that the forms are not processed when they're left empty. Right now, I get "Registration successful" even if the fields are empty.
Also, what do I need to add to make it so that an error message is displayed if the email is already registered?
Thanks a bunch!