Thanks.
Below are the required files.
Can you see anything that may be causing the problem I am having? As I can't think of anything else:
articleclass.php:
<?php
require_once("dataobject.php");
class article extends dataObject{
protected $data = array(
"id" => "",
"articletitle" => "",
"articleimg" => "",
"articletext" => "",
"articledate" => "",
"articleauthor" => ""
);
public function insertarticle(){
$conn = parent::connect();
$sql = "INSERT INTO " . TBL_ARTICLES . " (
articletitle,
articleimg,
articletext,
articledate,
articleauthor
) VALUES(
:articletitle,
:articleimg,
:articletext,
:articledate,
:articleauthor
)";
try{
$st = $conn->prepare( $sql );
$st->bindValue( ":articletitle", $this->data["articletitle"], PDO::PARAM_STR );
$st->bindValue( ":articleimg", $this->data["articleimg"], PDO::PARAM_STR );
$st->bindValue( ":articletext", $this->data["articletext"], PDO::PARAM_STR );
$st->bindValue( ":articledate", $this->data["articledate"], PDO::PARAM_STR );
$st->bindValue( ":articleauthor", $this->data["articleauthor"], PDO::PARAM_STR );
$st->execute;
parent::disconnect( $conn );
} catch( PDOException $e ){
parent::disconnect( $conn );
die( "Query failed " . $e );
}
}
}
?>
loginclass.php:
<?php
require_once( "dataobject.php" );
class login extends dataObject{
protected $data = array(
"username" => "",
"password" => ""
);
public function authenticate(){
$conn = parent::connect();
$sql = "SELECT * FROM " . TBL_LOGIN . " WHERE username = :username AND password = password(:password)";
try{
$st = $conn->prepare($sql);
$st->bindValue( ":username", $this->data["username"], PDO::PARAM_STR );
$st->bindValue( ":password", $this->data["password"], PDO::PARAM_STR );
$st->execute();
$row = $st->fetch();
if($row){
return new login($row);
}
} catch( PDOException $e ){
parent::disconnect();
die( "Query failed! " . $e );
}
}
}
function checkLogin(){
session_start();
if( !$_SESSION["LoggedInAdministrator"] ){
$_SESSION["LoggedInAdministrator"] = "";
header( "location: login.php" );
}
}
?>
dataobject.php
<?php
require_once("config.php");
abstract class dataObject{
protected $data = array();
public function __construct( $data ){
foreach( $data as $key => $value ){
if( array_key_exists( $key, $this->data ) ){
$this->data[$key] = $value;
}
}
}
public function getValue($field){
if( array_key_exists( $field, $this->data ) ){
return $this->data[$field];
} else{
die("Field not found!");
}
}
public function getValueEncoded( $field ){
return htmlspecialchars( $this->getValue( $field ) );
}
protected static function connect(){
try{
$conn = new PDO( DSN, USERNAME, PASSWORD );
$conn->setAttribute( PDO::ATTR_PERSISTENT, true );
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch( PDOException $e ){
die( "connection failed " . $e );
}
return $conn;
}
protected static function disconnect( $conn ){
return $conn = "";
}
}
?>
config.php
<?php
define( "PAGE_SIZE", 5 );
define( "TBL_ARTICLES", "article" );
define( "TBL_LOGIN", "login" );
define( "DSN", "mysql:dbname=conservation" );
define( "USERNAME", "root" );
define( "PASSWORD", "mypass" );
?>