This is a basic db access class used with a few databases and many tables.
I want to add in functionality that allows me to collate error information when adding a record.
- Person submits form to add as new user.
- My script checks to ensure username requested not taken + other tings etc.
- details of any errors are then tracked as the checks are made.
- If can’t add record, then errors are shown.
Where would I add it and integrate it with this code?
class Database
{
private $host;
private $user;
private $pwd;
// etc accesses the db and does all the db stuff
}
class User
{
private $db;
private $id;
public $name;
public function __construct($database, $id)
{
$this->db = $database;
$this->id = $id
}
public function update_record()
{
$this->db->query("UPDATE employee_table SET name='$this->name' WHERE id='$this->id'");
}
// etc...
}
// Now using these classes is very simple. Every object of type 'User' will
// need to know what database to work on.
// Instantiate our employee database
$employee_db = new Database();
$employee_db->init('localhost', 'mysql_user', 'mysql_password', 'employee_db_name');
// Instantiate our customer database
$customer_db = new Database();
$customer_db->init('localhost', 'mysql_user', 'mysql_password', 'customer_db_name');
// Now on to the users, lets work with an employee-style user
$some_employee = new User($employee_db);
$some_employee->name = "John Doe"; // Change some data
$some_employee->update_record(); // Update database record
// And using the second database...
$some_customer = new User($customer_db);
// etc...