Required fields and other newbie questions

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
fjjode
Forum Newbie
Posts: 2
Joined: Thu Apr 09, 2009 5:53 pm

Required fields and other newbie questions

Post by fjjode »

hi everyone, I'm new to PHP so I'm sorry if these questions are too basic.. I'm working on login and registration script for my website and i got them to work in a very basic form but I need to add some features. first here's my code:

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.";  
}  
?>
login.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");
 
$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!
tech603
Forum Commoner
Posts: 84
Joined: Thu Mar 19, 2009 12:27 am

Re: Required fields and other newbie questions

Post by tech603 »

Here is a tutorial on how to setup required fields using javascript.

http://www.willmaster.com/library/javas ... quired.php

If you wanted to do it via php, you would have to check the fields after they were submitted, and then echo out an error message.

Example:

Code: Select all

 
if(isset($_POST['username']) && $_POST['username'] != ""){
$username = $_POST['username'];
}else{
echo 'You did not enter a valid username, please hit the back button and try again';
}
 
Hope that helps.
fjjode
Forum Newbie
Posts: 2
Joined: Thu Apr 09, 2009 5:53 pm

Re: Required fields and other newbie questions

Post by fjjode »

thanks for the help!
does anyone know how to check if username is already taken?
Post Reply