这是我开始编写的一个类,用于通过注册表将新用户添加到我的数据库中。在我尝试进行后端检查以查看是否未提供所需值之前,一切都运行良好。这是代码...
class User
{
//Basic Login variables
private $uName;
private $pWord;
private $cpWord;
private $shaPass;
//Account info vars
private $fName;
private $lName;
private $email;
private $confEmail;
private $mPhone;
private $hPhone;
//Trading info vars
private $trName;
private $phone; //Defaults to home phone, or cell, if no home number specified.
private $fax;
private $web;
//Home Address vars
private $h_addr1;
private $h_addr2;
private $h_city;
private $h_state;
private $h_zip;
//Postal Address vars
private $p_addr1;
private $p_addr2;
private $p_city;
private $p_state;
private $p_zip;
private $not_required_vars;
//Post name collection
private $post_coll;
//Error collection
private $errors;
public function User()
{
$this->not_required_vars = array('fax', 'web');
$this->post_coll = array('uName', 'pWord', 'cpWord', 'fName', 'lName', 'email', 'confEmail', 'dob', 'mPhone', 'hPhone', 'trName', 'phone', 'fax', 'web', 'h_addr1', 'h_addr2', 'h_city', 'h_state', 'h_zip', 'p_addr1', 'p_addr2', 'p_city', 'p_state', 'p_zip');
//print_r($_POST);
foreach($this->post_coll as $index)
{
if(isset($_POST[$index]) && (!empty($_POST[$index])))
{
//echo $index . '<br />';
$this->{$index} = $_POST[$index];
}else
{
foreach($this->not_required_vars as $not_req)
{
if($index != $not_req)
{
//echo $index . '<br />';
$this->errors = array();
$this->errors[] = array($index=>'This field is required');
break;
//header('location: '.$_SERVER['PHP_SELF'].'?page='. $_GET['page'] . '&error=1');
}
}
}
}
print_r($this->errors);
}
让我通过解释上述内容来节省您的时间……我有一个名为“post_coll”的 $_POST 索引字符串数组。然后我让我的 foreach 循环遍历每个字符串,检查 $_POST 数组中是否设置了该字符串,如果不是,我检查它是否是必需的值。如果它确实是必需的值,我想创建一个关联数组,其中字符串(例如'uName')作为数组中的键,当我将它重定向回时,值类似于“此字段是必需的”我的表格我可以从我的班级获取错误并在表格上适当的地方显示它们。
提前感谢您的帮助。