I have a class naming user with the code below
require_once(LIB_PATH.DS.'database.php');
class User extends DatabaseObject {
protected static $table_name="users";
public $id;
public $username;
public $password;
public $first_name;
public $last_name;
}
i extended this User class from a DatabaseObject class where i had put up common methods for database CRUD at the moment i had just put up a Create method below is my DatabaseObject Class
require_once(LIB_PATH.DS.'database.php');
class DatabaseObject {
public static function attributes() {
$class_name = get_called_class();
$object = new $class_name;
return get_object_vars($object);
}
public static function sanitized_attributes() {
global $database;
$clean_attributes = array();
foreach(static::attributes() as $key => $value){
$clean_attributes[$key] = $database->escape_value($value);
}
return $clean_attributes;
}
public static function create() {
global $database;
$attributes = static::sanitized_attributes();
$sql = "INSERT INTO ".static::$table_name." (";
$sql .= join(", ", array_keys($attributes));
$sql .= ") VALUES ('";
$sql .= join("', '", array_values($attributes));
$sql .= "')";
if($database->query($sql)) {
return $sql;
} else {
return false;
}
}
}
when i m running a test from my test.php to check whether the Create works or not with this code
$user = new User();
$user->username = "johnsmith";
$user->password = "abcd12345";
$user->first_name = "John";
$user->last_name = "Smith";
echo User::create();
it just returns this query with empty values
INSERT INTO users
(id, username, password, first_name, last_name) VALUES ('', '', '', '', '')
and when i check phpmyadmin a row is inserted but with empty values whats is the mistake i m making please help
Regards
Tapos