类不写入数据库PDO

时间:2014-01-04 03:14:07

标签: php mysql class pdo

我有一个类设置来将一些数据写入一个似乎并没有实际写入数据的mysql数据库。我认为问题出在某个地方的PDO声明中。我仔细检查了网站上其他脚本的查询和数据库连接,它们工作正常。有什么想法吗?

这是我的表格:

<?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';
?>

这是我的班级:

<?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";
            }
        }
    }
?>

0 个答案:

没有答案