Php 使用类在数据库中保存表单数据
我有一张表格。(Registration.php) 和数据库连接:(Database.php) 我已经找了几个星期了,想知道如何将表单数据先送到类(客户端),然后送到类(数据库),然后再保存到数据库!但是我没有找到我需要的东西。Php 使用类在数据库中保存表单数据,php,database,forms,class,Php,Database,Forms,Class,我有一张表格。(Registration.php) 和数据库连接:(Database.php) 我已经找了几个星期了,想知道如何将表单数据先送到类(客户端),然后送到类(数据库),然后再保存到数据库!但是我没有找到我需要的东西。 也许有人能解释一下如何做到这一点?您需要使用反射来获取私有和受保护的类属性。下面是一个简单的函数,用于返回类的实例。第一个参数是类名,第二个参数是数组数据,在本例中,它将是表单数据。Registration类必须具有与表单相同的变量名称 public funct
也许有人能解释一下如何做到这一点?您需要使用反射来获取私有和受保护的类属性。下面是一个简单的函数,用于返回类的实例。第一个参数是类名,第二个参数是数组数据,在本例中,它将是表单数据。Registration类必须具有与表单相同的变量名称
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;
}
为您的
客户端添加所有get
方法
类。
例如:
class Client{
public function getClientID(){
return $this->clientID;
}
}
将addClient
方法添加到数据库
类
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);
你有一个可以在数据库中存储这些信息的表吗?我有一个以所有列为表单的数据库!命名客户端。是的,我认为您可以使用整个对象来插入它,但是我;我不确定它是否适用于私人物业。只要尝试一下,就会得到错误:严格的标准:在Sth->Bindparam(':Something',$client->getSomething())的每一行中,C:\Users\Arjan\Spullen\Programmas\PHP server\root\Ala\Project\Project\Arjanoskam\database.PHP中只应通过引用传递变量;
class Client{
public function getClientID(){
return $this->clientID;
}
}
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();
}
}
$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);