2014-10-29 8 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

관련 문제