Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?
- // a class
- class User
- {
- public $user, $pass, $fname, $lname, $bday;
- public $user_err, $pass_err, $name_err;
- public function __construct( $post_array ) {
- $this->user = $_POST['username'];
- $this->pass = $_POST['password'];
- $this->pass2 = $_POST['password2'];
- $this->fname = $_POST['first_name'];
- $this->lname = $_POST['last_name'];
- $this->bday = $_POST['birthday'];
- $this->city = $_POST['city'];
- $this->state = $_POST['state'];
- $this->address = $_POST['address'];
- $this->zip = $_POST['zip'];
- $this->user_err = NULL;
- $this->pass_err = NULL;
- $this->name_err = NULL;
- $this->birth_err = NULL;
- $this->city_err = NULL;
- $this->state_err = NULL;
- $this->address_err = NULL;
- $this->zip_err = NULL;
- }
- public function validate() {
- // username isn't a duplicate
- if( !$this->user ) {
- $this->user_err = "Please specify username";
- } else if( duplicate( $this->user ) ) {
- $this->user_err = "That username is already in use";
- }
- // passwords match and are at least 6 chars
- if( !$this->pass || strlen( $this->pass ) < 6 ) {
- $this->pass_err = "Password must be at least 6 characters";
- } else if( $this->pass != $this->pass2 ) {
- $this->pass_err = "Passwords do not match";
- }
- else
- $this->vpass = $_POST['password'];
- // first/last name aren't blank
- if( !$this->fname && !$this->lname ) {
- $this->name_err = "Please provide a first and last name";
- }
- else if(!$this->fname){
- $this->name_err = "Please provide a first name";
- }
- else if(!$this->lname){
- $this->name_err = "Please provide a last name";
- }
- if(!$this->bday){
- $this->birth_err = "Please enter your birthday";
- }
- if(!$this->city){
- $this->city_err = "Please enter a city";
- }
- if(!$this->state){
- $this->state_err = "Please enter a state";
- }
- if(!$this->address){
- $this->address_err = "Please enter your address";
- }
- if(!$this->zip){
- $this->zip_err = "Please enter your zip code";
- }
- return !$this->has_errors();
- }
- public function has_errors() {
- return $this->user_err || $this->pass_err || $this->name_err || $this->birth_err || $this->city_err || $this->state_err || $this->address_err || $this->zip_err;
- }
- public function insert()
- {
- $sql = "
- INSERT INTO users
- (username, password, first_name, last_name, birthdate, city, state, address, zip)
- VALUES ( '$this->user', '$this->vpass' ,
- '$this->fname', '$this->lname', '$this->bday', '$this->city', '$this->state', '$this->address', '$this->zip');";
- mysql_query( $sql ) or die( "Error( $sql): " . mysql_error() );
- }
- }
- function duplicate( $username )
- {
- $sql = "SELECT id FROM users WHERE username = '$username'";
- $result = mysql_query( $sql ) or die( "Error( $sql): " . mysql_error() );
- return mysql_num_rows( $result ) > 0;
- }
- ?>
Add Comment
Please, Sign In to add comment