无法保存到数据库

时间:2014-10-29 16:36:37

标签: php database pdo

我现在已经找了一个星期,因为错了。我仍然无法找出出错的地方! 我希望有人能看到什么错误?

和registration.php:

<form action="saveregistration.php" onsubmit="return validateForm()" class="pure-form" id="form" method="POST"> 
<label for="initials">Initials:</label><input type="text" name="initials" id="initials" placeholder="initials" required><br />
<label for="firstname">First name:</label><input type="text" name="firstname" id="firstname" placeholder="firstname" required><br />
<label for="surname">Surname:</label><input type="text" name="surname" id="surname" placeholder="surname" required><br />
<label for="country">Country:</label><input type="text" name="country" id="country" placeholder="country" required><br />
<label for="state">State:</label><input type="text" name="state" id="state" placeholder="state" ><br />
<label for="province">Province:</label><input type="text" name="province" id="province" placeholder="province" required><br />
<label for="city">City:</label><input type="text" name="city" id="city" placeholder="city" required><br />
<label for="houseaddress">Houseaddress:</label><input type="text" name="houseaddress" id="houseaddress" placeholder="houseaddress" required><br />
<label for="phone">Phone:</label><input type="text" name="phone" id="phone" placeholder="phone" required><br />
<label for="email">E-mail:</label><input type="text" name="email" id="email" placeholder="email" required><br />

client.class.php:

class Client{
    private $initials;
    private $name;
    private $surname;
    private $country;
    private $state;
    private $province;
    private $city;
    private $houseaddress;
    private $phone;
    private $email;


        public function getInitials() {
            return $this->initials;
        }

        public function getName() {
            return $this->name;
        }

        public function getSurname() {
            return $this->surname;
        }

        public function getCountry() {
            return $this->country;
        }

        public function getState() {
            return $this->state;
        }

        public function getProvince() {
            return $this->province;
        }

        public function getCity() {
            return $this->city;
        }

        public function getHouseaddress() {
            return $this->houseaddress;
        }

        public function getPhone() {
            return $this->phone;
        }

        public function getEmail() {
            return $this->email;
        }
        public function __construct($initials, $name, $surname, $country, $state, $province, $city, $houseaddress, $phone, $email) {
            $this->initials = $initials;
            $this->name = $name;
            $this->surname = $surname;
            $this->country = $country;
            $this->state = $state;
            $this->province = $province;
            $this->city = $city;
            $this->houseaddress = $houseaddress;
            $this->phone = $phone;
            $this->email = $email;
        }


        function setInitials($initials) {
            $this->initials = $initials;
        }

        function setName($name) {
            $this->name = $name;
        }

        function setSurname($surname) {
            $this->surname = $surname;
        }

        function setCountry($country) {
            $this->country = $country;
        }

        function setState($state) {
            $this->state = $state;
        }

        function setProvince($province) {
            $this->province = $province;
        }

        function setCity($city) {
            $this->city = $city;
        }

        function setHouseadress($houseaddress) {
            $this->houseaddress = $houseaddress;
        }

        function setPhone($phone) {
            $this->phone = $phone;
        }

        function setEmail($email) {
            $this->email = $email;
        }
        function getClientArray(){

        }
?>

database.php中:

<?php


class Database {
    private $pdo;

    public function __construct() {
        // Connection information
        $host   = 'localhost:3307';
        $dbname = 'californiahotel';
        $user   = 'root';
        $pass   = 'usbw';

        // Attempt DB connection
        try
        {
            $this->pdo = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
            $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            echo 'Successfully connected to the database!';
        }
        catch(PDOException $e)
        {
            echo $e->getMessage();
        }

    }

    public function prepareRegistration($client){
            $initials = $client->getInitials();
            $name = $client->getName();
            $surname = $client->getSurname();
            $country = $client->getCountry();
            $state = $client->getState();
            $province = $client->getProvince();
            $city = $client->getCity();
            $houseaddress = $client->getHouseAddress();
            $phone = $client->getPhone();
            $email = $client->getEmail();
    //}
    //public function saveRegistration($client){
            $sql = "INSERT INTO client (Initials, Name, Surname, Country, State, Province, City, Houseadress, Phone, Email) VALUES(:Initials, :Name, :Surname, :Country, :State, :Province, :City, :Houseadress, :Phone, :Email)";
            $sth = $this->pdo->prepare($sql);
            $sth->bindParam(':Initials', $initials);
            $sth->bindParam(':Name', $name);
            $sth->bindParam(':Surname', $surname);
            $sth->bindParam(':Country', $country);
            $sth->bindParam(':State', $state);
            $sth->bindParam(':Province', $province);
            $sth->bindParam(':City', $city);
            $sth->bindParam(':Houseaddress', $houseaddress);
            $sth->bindParam(':Phone', $phone);
            $sth->bindParam(':Email', $email);
            $sth->execute();

            //return $sql;
    }

saveregistration.php

$initials = $_POST['initials'];
$name = $_POST['firstname'];
$surname = $_POST['surname'];
$country = $_POST['country'];
$state = $_POST['state'];
$province = $_POST['province'];
$city = $_POST['city'];
$houseaddress = $_POST['houseaddress'];
$phone = (int)$_POST['phone'];
$email = $_POST['email'];

//$sql = "INSERT INTO client (Initials, Name, Surname, Country, State, Province, City, Houseadress, Email) VALUES('AJ', 'Arjan', 'Oskam', 'Holland', 'None', 'ZuidHolland', 'Boskoop', 'Laagboskoop53', '0615115309', 'arjanoskam97@gmail.com')";

echo $phone;
$client = new Client("", $initials, $name, $surname, $country, $state, $province, $city, $houseaddress, $phone, $email);
//var_dump($client);
//echo $client;
//$clientarray = (array)$client;
//$clientarrayprint = $client;
//foreach($client as $value) {
//  print $value . "<br />";
//}
//var_dump($clientarray);
$database = new Database();
$database->prepareRegistration($client);
//var_dump($client);
//$database->saveRegistration();
//header("Location: index.php");

也许有一个人看到了问题,但我找不到了! 提前谢谢!

1 个答案:

答案 0 :(得分:1)

查看构造函数以及如何实例化对象:

构造

public function __construct($initials, $name, $surname, $country, $state, $province, $city, $houseaddress, $phone, $email) {

新对象:

$client = new Client("", $initials, $name, $surname, $country, $state, $province, $city, $houseaddress, $phone, $email);
                     ^^^ why is this here?

您使用一个参数调用构造函数太多,您应该删除第一个emtpy字符串:

$client = new Client($initials, $name, $surname, $country, $state, $province, $city, $houseaddress, $phone, $email);