Hi, I am trying to do a database input with my Mysql class with prepared statements but I can't get the user class variables to set with the data from my form and I can't work out why.
I have commented out all of the code concerning the database entry and things at the moment as I am trying to pinpoint the cause of the problem.
here is my code:
User.php
<?php
require_once('initialize.php');
class User {
public $id = '';
public $first_name;
public $last_name;
public $user_name;
public $password;
protected static $db_fields = array('id', 'first_name', 'last_name', 'user_name', 'password');
protected static $table_name = "users";
public static function authenticate($user_name, $password) {
global $database;
$password = sha1($password);
$sql = "SELECT * FROM users WHERE user_name = '{$user_name}' AND password = '{$password}' LIMIT 1 ";
$result = $database->query($sql);
if(count($result) == 1) {
return true;
} else {
return false;
}
}
private function has_attribute($attribute) {
// We don't care about the value, we just want to know if the key exists
// Will return true or false
return array_key_exists($attribute, $this->attributes());
}
protected function attributes() {
// return an array of attribute names and their values
$attributes = array();
foreach(self::$db_fields as $field) {
if(property_exists($this, $field)) {
$attributes[$field] = $this->$field;
}
}
return $attributes;
}
public static function instantiate($record) {
$object = new self;
//$object->user_name = $record['user_name'];
//$object->password = $record['password'];
//$object->first_name = $record['first_name'];
//$object->last_name = $record['last_name'];
//foreach($record as $attribute=>$value){
// if($object->has_attribute($attribute) && !isset($object->$attribute)) {
// $object->$attribute = $value;
// }
//}
return $object;
}
public function create($records) {
global $database;
self::instantiate($records);
// if(isset($this->user_name)) {echo $this->user_name; }else{echo "damb";}
//$data = array('id' => '', 'first_name' => 'Matt','last_name' => 'hale', 'user_name' =>'matt', 'password'=>'pass' );
//echo "user : " . $this->user_name;
$data = $this->attributes();
echo $data;
//$insert = $database->insert(self::$table_name, $data);
//if($insert){
// return true;
//} else {
// return false;
//}
}
}
$user = new User();
register.php
<?php
require_once('/includes/initialize.php');
if(isset($_POST['submit'])) {
//$arr = array();
//foreach($_POST as $attr => $val) {
// if(!empty($_POST[$attr])) {
// $arr[$attr] = $val;
// }
//}//print_r($arr);
if($user->create($_POST)) {
echo "done";
} else {
echo "not done";
}
}
?>
<form action="register.php" method="post" >
<fieldset>
<ol>
<li>
<label for="first_name">First Name:</label>
<input id="first_name" name="first_name" type="text" required autofocus>
</li>
<li>
<label for="last_name">Last Name:</label>
<input id="last_name" name="last_name" type="text" required>
</li>
<li>
<label for="user_name">Username:</label>
<input id="user_name" name="user_name" type="text" required>
</li>
<li>
<label for="password">Password:</label>
<input id="password" name="password" type="password" required>
</li>
<li>
<label for="r_password">Repeat Password:</label>
<input id="r_password" name="r_password" type="password" required>
</li>
<li>
<input type="submit" name="submit" value="Submit" />
</li>
</ol>
</fieldset>
</form>
Any help would be much appreciated, I just cat't work out the problem
Thanks,
Matt