I am trying to wrap my head around how to properly use OOP in PHP when dealing with form submissions.
The example below is a form for editing course information. I have a class called Course which has methods for loading the class with information from the database and saving information into the database.
Everything works but I feel like I am doing this all wrong - any critique/suggestion would be nice.
edit_course.php
<?php
require_once 'header_includes.php';
$course = new Course();
$errors = array();
// get course ID
$cid = filter_input(INPUT_GET, 'cid', FILTER_VALIDATE_INT);
// check course ID
if ($cid && $course->load_data($cid)) {
$filter = array(
'code' => FILTER_SANITIZE_STRING,
'name' => FILTER_SANITIZE_STRING,
'hid-submit' => FILTER_VALIDATE_BOOLEAN
);
$inputs = filter_input_array(INPUT_POST, $filter);
// check if form submitted
if (!empty($inputs['hid-submit'])) {
// show error messages if any
$course->code = $inputs['code'];
if (empty($inputs['code'])) {
$errors['code'] = 'This is not a valid code.';
}
$course->name = $inputs['name'];
if (empty($inputs['name'])) {
$errors['name'] = 'This is not a valid name.';
}
$course->description = $inputs['description'];
// save course information
if (!count($errors)) {
$saved = $course->save_data();
}
}
}
require_once 'header.php'; // header template
?>
<?php if ($course->error !== false) { ?>
<div id="error"><?php echo $course->error ?></div>
<?php } ?>
<form name="form1" action="" method="post">
<div>
<label>Code:</label>
<input type="text" name="code" value="<?php echo $course->code ?>" maxlength="10" size="8" />
<?php if (array_key_exists('code', $errors)) { ?>
<div class="error"><?php echo $errors['code'] ?></div>
<?php } ?>
</div>
<div>
<label>Name:</label>
<input type="text" name="name" value="<?php echo $course->name ?>" maxlength="45" size="25" />
<?php if (array_key_exists('name', $errors)) { ?>
<div class="error"><?php echo $errors['name'] ?></div>
<?php } ?>
</div>
<div>
<input type="submit" value="Save" />
<input type="hidden" name="hid-submit" value="1" />
</div>
</form>
<?php
require_once 'footer.php'; // footer template
?>
Course.class.php
<?php
if(!class_exists('Course')) {
class Course {
private $id = NULL;
private $code = '';
private $name = '';
private $error = FALSE;
public function __get($property) {
if (property_exists($this, $property)) {
return $this->$property;
}
}
public function __set($property, $value) {
if (property_exists($this, $property)) {
$this->$property = $value;
}
return $this;
}
/**
* Load course data from database
*
* @param int
* @return bool
*/
public function load_data($id) {
// SQL statement
$sql = 'SELECT id, code, name FROM courses WHERE id = :id';
try {
$core = Core::getInstance();
//
$sth = $core->dbh->prepare($sql);
$sth->bindParam(':id', $id, PDO::PARAM_INT);
if ($sth->execute()) {
// if course is found
if ($row = $sth->fetch(PDO::FETCH_OBJ)) {
$this->id = intval($row->id);
$this->code = $row->code;
$this->name = $row->name;
return TRUE;
}
} else {
throw new Exception('A problem has occurred while retrieving information from database.');
}
$sth->closeCursor();
} catch (Exception $e) {
$this->error = $e->getMessage();
}
return FALSE;
}
/**
* Save course information
*
* @return bool
*/
public function save_data() {
// SQL statements
if ($this->id) {
$sql = 'UPDATE courses
SET code = :code, name = :name
WHERE id = :id';
} else {
$sql = 'INSERT INTO courses(id, code, name)
VALUES(:id, :code, :name)';
}
try {
$core = Core::getInstance();
// update/insert into database
$sth = $this->core->dbh->prepare($sql);
$sth->bindParam(':id', $this->id, PDO::PARAM_INT);
$sth->bindParam(':code', $this->code, PDO::PARAM_STR);
$sth->bindParam(':name', $this->title, PDO::PARAM_STR);
if (!$sth->execute()) throw new Exception('A problem has occurred while updating database.');
return TRUE;
} catch (Exception $e) {
$this->error = $e->getMessage();
}
return FALSE;
}
}
}
?>