使用类在数据库中保存表单数据

时间:2014-10-22 08:11:55

标签: php database forms class

我有一张表格。 (为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!';
    }
}

我一直在寻找几周来找出如何将表单数据提供给类(客户端),然后到类(数据库),然后将其保存到数据库中!但我没有找到我需要的任何东西。 也许有人可以解释如何做到这一点?

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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; 
}