2014-10-29 4 views
0

Я искал неделю, потому что что-то не так. Я все еще не в состоянии выяснить, что случилось неправильно! Надеюсь, кто-то может понять, что случилось?Не удается сохранить базу данных

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', '[email protected]')"; 

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 из вас видит проблему, но я не могу ее найти! Спасибо заранее!

+4

Вы просматривали журналы ошибок? –

+1

Typo: Houseaddress vs Houseadress –

+0

Типо, как @AlexK. указали –

ответ

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); 
+0

Забыл упомянуть! : В базе данных есть столбец ClientID! –

+1

Это не имеет значения, когда вы строите свой объект. Первый параметр в конструкторе - '$ initials', поэтому, когда вы его вызываете, он должен быть таким же. – jeroen

+0

@ArjanOskam yeah jeroen - это правильно, вы должны удалить дополнительные параметры, все это похоже на то, что вы смутили его с запросом INSERT. ваш код будет искать этот конструктор, которого у вас нет, и объект '$ client' не будет создан – meda

Смежные вопросы