Go Back   Tech Support Forum > Design Forum > Web Serving and Management

User Tag List

[SOLVED] PHP MySQL Login / Register

This is a discussion on [SOLVED] PHP MySQL Login / Register within the Web Serving and Management forums, part of the Tech Support Forum category. Ive been trying to code a login and registration form using PHP and MySQL but cannot get it to work


Closed Thread
 
Thread Tools Search this Thread
Old 04-27-2013, 09:55 PM   #1
Registered Member
 
Join Date: Oct 2009
Posts: 162
OS: windows 7



Ive been trying to code a login and registration form using PHP and MySQL but cannot get it to work at all, So far i have two files db.PHP and User.PHP

When i run the db.php file using EasyPHP 12.1 it runs with no errors but when i run the User.php i get the following error message:

Parse error: syntax error, unexpected ';' in C:\Program Files(x86)\EasyPHP-12.1\www\login_FormTest\User.php on line 80

User.PHP
Code:
<?php
	// User class to store user information for logged user
class User {
	private $uid; // User id
	private $fields; // Other records on file
	
	// Initialise our user object
	public function __construct() {
		$this->uid = null;
		$this->fields = array('username' => '', 'password' => '', 'emailAddr' => '', 'isActive' => false);
	}
	
	// Overide magic method to retrieve properties
	public function __get($field) {
		if($field == 'userId') {
			return $this-uid;
		}else{
			return $this->fields[$field];
		}
	}
	
	// Overide magic method to set properties
	public function __set($field, $value){
		if(array_key_exists($field, $this->fields)) {
			$this->fields[$field] = $value;
		}
	}
	
	// Return if username is valid format
	public static function validateUsername($username) {
		return preg_match('/^[A-Z0-9]{2,20}$/i', $username);
	}
	
	// Return if email is valid format
	public static function validateEmailAddr($email) {
		return filter_var($email, FILTER_VALIDATE_EMAIL);
	}
	
	// Return an object populated based on a users ID
	public static function getById($user_id) {
		$user = new User();
		$query = sprintf('SELECT USERNAME, PASSWORD, EMAIL_ADDR, IS_ACTIVE FROM %sUSER WHERE USER_ID = %d', DB_TBL_PREFIX, $user_id);
		$result = mysql_query($query, $GLOBALS['DB']);
		if(mysql_num_rows($result)) {
			$row = mysql_fetch_assoc($result);
			$user->username = $row['USERNAME'];
			$user->password = $row['PASSWORD'];
			$user->emailAddr = $row['EMAIL_ADDR'];
			$user->isActive = $row['IS_ACTIVE'];
			$user->uid = $user_id;
		}
		mysql_free_result($result);
		return $user;
	}
	
	// Return an object populated based on a username
	public static function getByUsername($username) {
		$user = new User();
		$query = sprintf('SELECT USER_ID, PASSWORD, EMAIL_ADDR, IS_ACTIVE FROM %sUSER WHERE USERNAME = "%s"', DB_TBL_PREFIX, mysql_real_escape_string($username, $GLOBALS['DB']));
		$result = mysql_query($query, $GLOBALS['DB']);
		if(mysql_num_rows($result)) {
			$row = mysql_fetch_assoc($result);
			$user->username = $username;
			$user->password = $row['PASSWORD']; 
			$user->emailAddr = $row['EMAIL_ADDR'];
			$user->isActive = $row['IS_ACTIVE'];
			$user->uid = $row['USER_ID'];
		}
		mysql_free_result($result);
		return $user;
	}
	
	// Save the record to the database
	public function save() {
		if($this->uid {
			$query = sprintf('UPDATE %sUSER SET USERNAME = "%s", PASSWORD = "%s", EMAIL_ADDR = "%s", IS_ACTIVE = %d WHERE USER_ID = %d', DB_TBL_PREFIX,
			mysql_real_escape_string($this->username, $GLOBALS['DB']),
			mysql_real_escape_string($this->password, $GLOBALS['DB']),
			mysql_real_escape_string($this->emailAddr, $GLOBALS['DB']),
			$this->isActive, $this->userId);
			return mysql_query($query, $GLOBALS['DB']);		
		}else{
		// If the user has not register yet		
			$query = sprintf('INSERT INTO $USER (USERNAME, PASSWORD, EMAIL_ADDR, IS_ACTIVE) VALUES ("%s", "%s", "%s", %d)', DB_TBL_PREFIX,
			mysql_real_escape_string($this->username, $GLOBALS['DB']),
			mysql_real_escape_string($this->password, $GLOBALS['DB']),
			mysql_real_escape_string($this->emailAddr, $GLOBALS['DB']),
			$this->isActive);
			if(mysql_query($query, $GLOBALS['DB'])) {
				$this->uid = mysql_insert_id($GLOBALS['DB']);
				return true;
			}		
	}
}
?>
DB.PHP:
Code:
<?php
// Database connection and schema constants

define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_SCHEMA', 'login_tutorial');
define('DB_TBL_PREFIX', 'BHZ_');

// Establish a connection to the database
if(!$GLOBALS['DB'] = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD)) {
	die('Error: Unable to connect to the database');
}
if (!mysql_select_db(DB_SCHEMA, $GLOBALS['DB'])) {
	mysql_close($GLOBALS['DB']);
	die('Error: Unable to select database schema');
}

?>
From what i can see though there is no unexpected ; on line 80 or any of the lines for that matter. Can anyone see what ive done wrong? If not can you point me in the direction of a good tutorial to help me with this. any help would be much appreciated, thankyou.
owain is offline  
Sponsored Links
Advertisement
 
Old 04-28-2013, 09:04 PM   #2
Moderator
- Design Team
- Networking Team
 
Fjandr's Avatar
 
Join Date: Sep 2012
Location: Spokane, WA
Posts: 2,483
OS: Windows XP SP3, Windows 7 SP1, Debian, CentOS

My System


On line 75
Code:
if($this->uid {
the ( is unpaired.
__________________

Fjandr is offline  
Old 04-29-2013, 12:32 AM   #3
Manager
- Alternative Computing
- Design
 
wmorri's Avatar
 
Join Date: May 2008
Location: New York
Posts: 6,492
OS: Window 7/Ubuntu/Fedora/Backtrack 5

My System

Send a message via AIM to wmorri Send a message via MSN to wmorri

Good find.
__________________


Linux Forever!|html5 boilerplate
wmorri is offline  
Sponsored Links
Advertisement
 
Old 04-29-2013, 05:49 AM   #4
Registered Member
 
Join Date: Oct 2009
Posts: 162
OS: windows 7



Oh yeah haha cheers, Great find
owain is offline  
Old 04-29-2013, 05:52 AM   #5
Registered Member
 
Join Date: Oct 2009
Posts: 162
OS: windows 7



Was also missing a } at the end again thanks
owain is offline  
Old 04-29-2013, 09:22 AM   #6
Moderator
- Design Team
- Networking Team
 
Fjandr's Avatar
 
Join Date: Sep 2012
Location: Spokane, WA
Posts: 2,483
OS: Windows XP SP3, Windows 7 SP1, Debian, CentOS

My System


Oh yep. Missed that the class declaration was never closed.

Good luck with the rest of it.
__________________

Fjandr is offline  
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] PHP MYSQL How to add data to database using HTML form?
Hi, I have a database with different people in it. They all have a username which is a number. Eg 12345 What i want to do is on the website when the username is inserted into a textbox and the submit button is pressed i want it to display the name that corresponds with that username. ...
huggies12345 Web Design & Development 3 12-02-2011 08:58 PM
Apache, PHP, MySQL cluster
Hello there guys, As a VPS provider taking part in a Social Networking project, I have been lumbered with the server-backed of it all.. So, I have been doing some reading on the best way to achieve as what we see as the server-based way-forward for the network. We are running from OpenVZ...
nhanhi Linux Support 0 10-30-2011 12:39 PM
Updating MySQL data using PHP
Hi, I'm currently trying to create a page that can be used to add gigs to a database and allow editing of the data too. I have created some php scripts using tutorials online and have most of the task done however I have a problem. (obviously I hear some of you say :sigh:) Let me describe the pages...
Overtones Web Design & Development 1 09-21-2011 11:39 AM

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is on
Smilies are on
[IMG] code is on
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Post a Question


» Site Navigation
 > FAQ
  > 10.0.0.2
Powered by vBadvanced CMPS v3.2.3


All times are GMT -7. The time now is 08:04 PM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2020, vBulletin Solutions, Inc.
vBulletin Security provided by vBSecurity v2.2.2 (Pro) - vBulletin Mods & Addons Copyright © 2020 DragonByte Technologies Ltd.
User Alert System provided by Advanced User Tagging v3.1.0 (Pro) - vBulletin Mods & Addons Copyright © 2020 DragonByte Technologies Ltd.
Copyright 2001 - 2018, Tech Support Forum

Windows 10 - Windows 7 - Windows XP - Windows Vista - Trojan Removal - Spyware Removal - Virus Removal - Networking - Security - Top Web Hosts