can a form serve as both "register.php" & "update_details.ph
Posted: Thu May 27, 2010 7:00 am
hi :rtfm: the following was written mostly by my college teacher, but i can't quite get it working. Does anyone out there know how i can make "RegoForm.php" recognize a user that is logged-in, and allow this user to edit his/her details (stored in the database) using the same form?
Here's the code for the form:
<?php
ob_start();
require_once("lib/class_loader.php");
session_start();
ob_flush();
$user = $_SESSION['user'];
$userDetails = null;
if($user != null)
{
$userDetails = $user->getDetails();
}
?>
<html>
<body>
<form name="join" id="client" name="client" action="register.php" onsubmit="return validate()" method="post">
username : <input type="username" name="username" id="userNameID" size=10 maxlength=15 value="<?php echo ($user != null)?$userDetails->username:"";?>" />
password : <input type="password" name="password" id="passwordID" size=10 maxlength=15 value="<?php echo ($user != null)?$userDetails->password:"";?>" />
</form>
</body>
</html>
Here's the "register.php" (form action) code:
<?php
include("lib/ConnectToDb.php");
include("lib/User.php");
//print_r($_POST);
if(isset($_POST['username']) && isset($_POST['password']))
{
$user = new User();
if($user->register(
$_POST['username']
,$_POST['password']
}
?>
<?php
include("home.php");
?>
Any help would be much appreciated (by my team members as well)
Thanx
georgehowell
oh, here's the "lib/User.php"
<?php
class User extends ConnectToDb {
public $username = "";
public function __construct() {
parent::__construct();
}
public function login($username,$password) {
$ps = $this->db->prepare("Select username, password from sz_users where username = ? and password = ?");
$this->username = $username;
$ps->execute(array($username,$password));
return ($ps->rowCount() == 1);
}
public function register($username,$password)
{
$ps = $this->db->prepare("Insert into SZ_users (username,password) values (?,?)");
$ps->execute(array($username,$password));
return ($ps->rowCount() == 1);
}
function get_username() {
return $this->username;
}
public function __sleep() {
return array("username","cart","products");
}
public function __toString() {
return "user = " . $username;
}
}
?>
Here's the code for the form:
<?php
ob_start();
require_once("lib/class_loader.php");
session_start();
ob_flush();
$user = $_SESSION['user'];
$userDetails = null;
if($user != null)
{
$userDetails = $user->getDetails();
}
?>
<html>
<body>
<form name="join" id="client" name="client" action="register.php" onsubmit="return validate()" method="post">
username : <input type="username" name="username" id="userNameID" size=10 maxlength=15 value="<?php echo ($user != null)?$userDetails->username:"";?>" />
password : <input type="password" name="password" id="passwordID" size=10 maxlength=15 value="<?php echo ($user != null)?$userDetails->password:"";?>" />
</form>
</body>
</html>
Here's the "register.php" (form action) code:
<?php
include("lib/ConnectToDb.php");
include("lib/User.php");
//print_r($_POST);
if(isset($_POST['username']) && isset($_POST['password']))
{
$user = new User();
if($user->register(
$_POST['username']
,$_POST['password']
}
?>
<?php
include("home.php");
?>
Any help would be much appreciated (by my team members as well)
Thanx
georgehowell
oh, here's the "lib/User.php"
<?php
class User extends ConnectToDb {
public $username = "";
public function __construct() {
parent::__construct();
}
public function login($username,$password) {
$ps = $this->db->prepare("Select username, password from sz_users where username = ? and password = ?");
$this->username = $username;
$ps->execute(array($username,$password));
return ($ps->rowCount() == 1);
}
public function register($username,$password)
{
$ps = $this->db->prepare("Insert into SZ_users (username,password) values (?,?)");
$ps->execute(array($username,$password));
return ($ps->rowCount() == 1);
}
function get_username() {
return $this->username;
}
public function __sleep() {
return array("username","cart","products");
}
public function __toString() {
return "user = " . $username;
}
}
?>