0

I have a class setup to write some data to a mysql database that doesn't seem to be actually writing the data. I believe the issue lies in the PDO statements somewhere. I double checked the query and the database connection on other scripts on the site and they work fine. Any ideas?

Here is my form:

<?php
    $navsection = 'addClass';
    $dir = $_SERVER['DOCUMENT_ROOT'] . "/grades/";
    // load php-login components
    require_once $dir . 'php-login.php';
    $classes = new Classes();

    // load head file
    require_once $dir . 'includes/head.php';
?>

<h1>Add a Class</h1>
<?php 
    // show negative messages
    if ($classes->errors) {
        foreach ($classes->errors as $error) {
            echo $error;
        }
    }

    // show positive messages
    if ($classes->messages) {
        foreach ($classes->messages as $message) {
            echo $message;
        }
    } 
?>
<br />
<form method='post' action='<?php $siteurl; ?>/grades/pages/addClass.php' name='addClass_form'>
    <label for='className'>Class Name:</label>
    <input id='className' type='text' name='className' required /><br />
    <label for='classProfessor'>Professor's Name:</label>
    <input id='classProfessor' type='text' name='classProfessor' /><br />
    <label for='classPeriod'>Class Period:</label>
    <select id='classPeriod' name='classPeriod'>
        <option value='Spring 2014'>Spring 2014</option>
        <option value='Fall 2013'>Fall 2013</option>
    </select><br />
    <label for='classStartDate'>Class Start Date:</label>
    <input id='classStartDate' type='date' name='classStartDate' /><br />
    <label for='classEndDate'>Class End Date:</label>
    <input id='classEndDate' type='date' name='classEndDate' /><br />
    <input type='submit' name='addClass' value='Submit' />
</form>

<?php
    //load footer file
    require_once $dir . 'includes/footer.php';
?>

Here is my class:

<?php
    class Classes
    {
        private $db_connection          = null;
        public $classAdd_successful         = false;
        public $classDelete_successful      = false;
        public $classEdit_successful        = false;
        public $errors              = array();
        public $messages                = array();

        public function __construct()
        {
            session_start();

            if (isset($_POST["addClass"])) {
                $this->addNewClass($_POST['className'], $_POST['classProfessor'], $_POST['classPeriod'], $_POST['classStartDate'], $_POST['classEndDate']); 
            }
        }

        private function databaseConnection()
        {
            if ($this->db_connection != null) {
                return true;
            } else {
                try {
                    $this->db_connection = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME, DB_USER, DB_PASS);
                    return true;
                } catch (PDOException $e) {
                    $this->errors[] = "Database error";
                    return false;
                }
            }
        }

        private function addNewClass ($className, $classProfessor, $classPeriod, $classStart, $classEnd)
    {
            if(empty($className)) {
                $this->errors[] = "Please enter a class name.";
            } elseif(empty($classProfessor)) {
                $this->errors[] = "Please enter a class professor.";
            } elseif(empty($classPeriod)) {
                $this->errors[] = "Please select a class period";
            } elseif(empty($classStart)) {
                $this->errors[] = "Please enter a class start date.";
            } elseif(empty($classEnd)) {
                $this->errors[] = "Please enter a class end date.";
            } 

            if ($this->databaseConnection() == true) {
                //Write data to database
                $query_new_class_insert = $this->db_connection->prepare('INSERT INTO classes (class_name, user_id, professor_name, class_start, class_end, school_period) VALUES(:className, :userID, :professorName, :classStart, :classEnd, :schoolPeriod)');
                $query_new_class_insert->bindvalue(':className', $className, PDO::PARAM_STR);
                $query_new_class_insert->bindvalue(':userID', $_SESSION['user_id'], PDO::PARAM_INT);
                $query_new_class_insert->bindvalue(':professorName', $classProfessor, PDO::PARAM_STR);
                $query_new_class_insert->bindvalue(':classStart', $classStart, PDO::PARAM_STR);
                $query_new_class_insert->bindvalue(':classEnd', $classEnd, PDO::PARAM_STR);
                $query_new_class_insert->bindvalue(':schoolPeriod', $schoolPeriod, PDO::PARAM_STR);
                $query_new_class_insert->execute();

                $this->classAdd_successful = true;
            } else {
                $this->errors[] = "Database write error";
            }
        }
    }
?>
Hopeful Llama
  • 728
  • 5
  • 26
user2363217
  • 695
  • 1
  • 7
  • 15
  • Where is `$siteurl` defined? And you need to echo it `
    – vee Jan 04 '14 at 03:15
  • Look over [PDO error handling](http://www.php.net/manual/en/pdo.error-handling.php) and configure your PDO object to throw exceptions via `$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION)`. By default, PDO errors _silently_ – Michael Berkowski Jan 04 '14 at 03:17
  • See also http://stackoverflow.com/questions/3726505/how-to-squeeze-error-message-out-of-pdo – Michael Berkowski Jan 04 '14 at 03:18
  • possible duplicate of [PDO Prepared Statements Fatal Error](http://stackoverflow.com/questions/20916147/pdo-prepared-statements-fatal-error) – Funk Forty Niner Jan 04 '14 at 03:19
  • This is a seriously bad implementation of a class. Here are some things to fix: `session_start()` should be at the top of your code, not buried in a constructor; you have dependencies on $_POST and some DB-related constants that should be injected in the constructor or through public methods; all your methods are private so why bother having a class?; there's no error checking on your PDO activity other than to check the database connection is set up; your methods don't return any useful values (which they can't, since they're private). I'm sure there's more. –  Jan 04 '14 at 03:31
  • possible duplicate of [Reference - What does this error mean in PHP?](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – hjpotter92 Jan 04 '14 at 20:00

0 Answers0