2014-10-22 100 views
-1

我有一个表单。 (和registration.php)将数据保存到数据库中使用类

<form action="reservation.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="houseadress">Houseadress:</label><input type="text" name="houseadress" id="houseadress" placeholder="houseadress" 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 /> 
<input type="submit" value="register" name="register"> 
</form> 

而且我有一个类:(client.class.php)

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

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

和一个数据库连接:(database.php中)

class Database { 
    public $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 __destruct() 
    { 
     // Disconnect from DB 
     $this->pdo = null; 
     //echo 'Successfully disconnected from the database!'; 
    } 
} 

我已经在寻找几个星期来了解如何将表单数据传递给类(客户端),然后到类(数据库),然后将其保存到数据库!但我没有找到任何我需要的东西。 也许有人可以解释它是如何做到的?

+0

你有一个表,这可能会存储这些信息在数据库中? – 2014-10-22 08:15:10

+0

我有一个数据库的所有列作为窗体!命名客户端。 – 2014-10-22 08:16:58

+0

是的,我认为你可以使用整个对象来插入它,但我不知道它是否可以在私有属性上工作。只是尝试它 – Ghost 2014-10-22 08:18:51

回答

1

添加所有get方法为您Client类。 例如:

class Client{ 
    public function getClientID(){ 
     return $this->clientID; 
    } 
    } 

添加addClient方法您Database

class Database { 
    public function addClient($client){ 

    //Get all vars 
    $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() 

    //Create the query 
    $sth = $this->pdo->prepare('INSERT INTO Client (Initials, Name, Surname, Country, State, Province, City, HouseAddress, Phone, Email) VALUES (:initials, :name, :surname, :country, :state, :province, :city, :houseaddress, :phone, :email)'); 
    $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); 

    //Execute the query 
    $sth->execute(); 
    } 
} 

在reservation.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 = $_POST['phone']; 
$email = $_POST['email']; 
$client = new Client("", $initials, $name, $surname, $country, $state, $province, $city, $houseadress, $phone, $email); 
$database = new Database(); 
$database->addClient($client); 
+0

我得到的错误:严格的标准:只有变量应通过引用传递在C:\ Users \ Arjan \ Spullen \ Programmas \ PHP服务器\根\丙\项目\项目\ Arjanoskam \ database.php在每一行Sth-> Bindparam(':Something',$ client-> getSomething()); – 2014-10-22 11:03:42

+1

编辑答案。核实。 :) – 2014-10-22 12:21:57

0

您将需要使用反射来获取私有和受保护的类属性。以下是我用来返回一个类的实例的简单函数。第一个参数是类名,第二个参数是数组数据,在你的情况下它将是表单数据。注册类必须与您的表单具有相同的变量名称。

public function toEntity($entityName, array $data){ 
    $entity = new $entityName(); 

    $refClass = new ReflectionClass($entity); 
    $properties = $refClass->getProperties(); 

    foreach($properties as $property){ 
     $property->setAccessible(true); 
     $propertyName = $property->getName(); 

     if(array_key_exists($propertyName, $data)){ 
      $value = $data[$propertyName]; 
      $property->setValue($entity, $value); 
     } 
    } 
    return $entity; 
}