INSERT查询不起作用,空数组?

时间:2012-12-03 17:41:56

标签: php methods insert pdo

<?php
    class Worker extends Core {
        public $name;
        public $surname;
        public $dob;
        public $skills;
        public $postcode;
        public $street;
        public $email;
        public $tel;
        public $ern;
        public $result;

        public function __construct () {
            $this->name = 'name';
            $this->surname = 'surname';
            $this->dob = 'dob';
            $this->skills = 'skills';
            $this->postcode = 'postcode';
            $this->street = 'street';
            $this->email = 'email';
            $this->tel = 'tel';
            $this->ern = 'ern';
        }


        //Saving worker data to database, need provide group name (table name)
        public function saveWorker($group) {
            if(!(isset($this->conn))) parent::__construct();
                try
                {
                    $this->conn ->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //catch exceptions
                    $q = 'INSERT INTO :group (name, surname, dob, skills, postcode, street, email, tel, erefnumber) VALUES (
                    :name,
                    :surname,
                    :dob,
                    :skills,
                    :postcode,
                    :street,
                    :email,
                    :tel,
                    :erefnumber)'; //sql query with group name
                    $stmt = $this->conn->prepare($q);
                    $stmt -> bindValue(':group', $group, PDO::PARAM_STR);
                    $stmt -> bindValue(':name', $this->name, PDO::PARAM_STR);
                    $stmt -> bindValue(':surname', $this->surname, PDO::PARAM_STR);
                    $stmt -> bindValue(':dob', $this->dob, PDO::PARAM_STR);
                    $stmt -> bindValue(':skills', $this->skills, PDO::PARAM_STR);
                    $stmt -> bindValue(':postcode', $this->postcode, PDO::PARAM_STR);
                    $stmt -> bindValue(':street', $this->street, PDO::PARAM_STR);
                    $stmt -> bindValue(':email', $this->email, PDO::PARAM_STR);
                    $stmt -> bindValue(':tel', $this->tel, PDO::PARAM_STR);
                    $stmt -> bindValue(':erefnumber', $this->erefnumber, PDO::PARAM_STR);

                    $results = $stmt->execute();
                    if($results > 0)
                    {
                              return 'Dodano: '.$ilosc.' rekordow';
                    }
                    else
                    {
                              return 'Wystapil blad podczas dodawania rekordow!';
                    }
                }
                catch(PDOException $e) 
                {
                  return 'There was some error: ' . $e->getMessage();
                }
                unset($stmt);
        }




            //no exceptions



    public function getWorker()
    {
        $workerData = array (
            "name" => $this->name,
            "surname" => $this->surname,
            "dob" => $this->dob,
            "skills" => $this->skills,
            "postcode" => $this->postcode,
            "street" => $this->street,
            "email" => $this->email,
            "tel" => $this->tel,
            "tel" => $this->erefnumber
        );

        return $workerData;
    } // end getWorker();  

    public function searchWorker($name, $surname, $dob, $skills, $postcode, $street, $email, $tel, $erefnumber) {

    }
    function deleteWorker() {

    }
    function getEmployer() {}
    public function __sleep () {
        parent::__sleep();
    }
} // end Person;




//DB connection

    class Core {
        public $conn;
        public function __construct() {
            $this->dbConnect();
        }
        public function dbConnect() {

            $host = 'localhost';
            $port = '3307';
            $username = 'modium_test';
            $password = 'test';
            $database ='modium_test';   
            try{
                    $this->conn = new PDO('mysql:host='.$host.';dbname='.$database.';port='.$port, $username, $password );
                    echo 'Connection successful!';
                    echo var_dump($this->conn);
            }
            catch(PDOException $e){
                    echo 'Error: ' . $e->getMessage();
            }   
        }

        public function __sleep () {
            unset($this->conn);
        }
    }


    }

查询不起作用。以前的每个函数都有效,但是当我尝试通过sql查询INSERT表时,没有任何事情发生。

Worker是一个很好的对象,然后我分配了一些POST数组,也可以正常工作然后我尝试saveWorker但它什么都没有。

调用行:

 var_dump($worker);
 if (isset($worker)) echo 'worker is set';
 if (isset($worker->conn)) echo 'thers connection is set';
 $worker->saveWorker('workers');

增加了一行:

 echo "\nPDO::errorInfo():\n";
 print_r($stmt->errorInfo());
 print_r($this->conn->errorInfo());
 echo "end of error info";

它给了我:

PDO::errorInfo(): 
Array ( [0] => ) Array ( [0] => 00000 )
end of error info

1 个答案:

答案 0 :(得分:1)

$stmt->execute()返回boolean(Manual)。尝试,

$results = $stmt->execute();
if($results !== FALSE)  {
 return 'Dodano: '.$ilosc.' rekordow';
} else {
 return 'Wystapil blad podczas dodawania rekordow!';
 }

此外,您无法绑定tablename