[英]Validating a form using data from data base
i have a problem with proper validation. 我有适当验证的问题。 Please take o look on this class:
请看看这个课程:
class NewFirm {
private $hookup;
private $tableMaster;
private $sql;
private $b1_name; //name of a Firm which has to be checked
public function __construct() {
$this->hookup = UniversalConnect::doConnect();
$this->tableMaster = "b1_firm";
$this->b1_name = trim($_POST['b1_name']);
$this->insertFirm();
$this->hookup->close();
}
private function insertFirm() {
try {
$this->sql = "SELECT b1_name FROM $this->tableMaster WHERE b1_name = '".$this->b1_name."'";
$result = $this->hookup->query($this->sql);
while($row = $result->fetch_assoc()) {
if((strtolower($row['b1_name']) != strtolower($this->b1_name))) {
$this->sql = "INSERT INTO $this->tableMaster (b1_id, b1_name) VALUES (NULL, '".$this->b1_name."')";
$this->hookup->query($this->sql);
$this->sql = "SELECT MAX(b1_id) FROM $this->tableMaster";
$result = $this->hookup->query($this->sql);
while($row = $result->fetch_assoc()) {
$_SESSION['b1_id'] = $row['MAX(b1_id)'];
}
$this->sql = "SELECT b1_name FROM $this->tableMaster WHERE b1_id = '".$_SESSION['b1_id']."' ";
$result = $this->hookup->query($this->sql);
while($row = $result->fetch_assoc()) {
$_SESSION['b1_name'] = $row['b1_name'];
}
$host = $_SERVER['HTTP_HOST'];
$uri = ''; //folder
$page = 'step_2.php';
header("Location: http://$host/$page");
} else {
$_SESSION['error'] = true;
$host = $_SERVER['HTTP_HOST'];
$uri = ''; //folder
$page = 'step_1.php';
header("Location: http://$host/$page");
}
}
} catch (Exception $e) {
print "There is a problem: ".$e->getMessage();
}
}
} }
When a firm name is found in the data base everything is ok. 在数据库中找到公司名称后,一切正常。 The problem appears when a new firm name isn't found in the data base - it shows a 'blank screen' :/
当在数据库中找不到新的公司名称时,就会出现问题-它显示“空白屏幕”:/
By adding a flag to see if the contact is found or not, you can do something like redirecting to your first step where there is not contact found : 通过添加标记来查看是否找到联系人,您可以执行类似重定向到未找到联系人的第一步的操作:
class NewFirm {
private $hookup;
private $tableMaster;
private $sql;
private $b1_name; //name of a Firm which has to be checked
public function __construct() {
$this->hookup = UniversalConnect::doConnect();
$this->tableMaster = "b1_firm";
$this->b1_name = trim($_POST['b1_name']);
$this->insertFirm();
$this->hookup->close();
}
private function insertFirm() {
try {
$this->sql = "SELECT b1_name FROM $this->tableMaster WHERE b1_name = '".$this->b1_name."'";
$result = $this->hookup->query($this->sql);
//setting a flag
$not_found = true;
while($row = $result->fetch_assoc()) {
//at least one contact was found
$not_found = false;
if((strtolower($row['b1_name']) != strtolower($this->b1_name))) {
$this->sql = "INSERT INTO $this->tableMaster (b1_id, b1_name) VALUES (NULL, '".$this->b1_name."')";
$this->hookup->query($this->sql);
$this->sql = "SELECT MAX(b1_id) FROM $this->tableMaster";
$result = $this->hookup->query($this->sql);
while($row = $result->fetch_assoc()) {
$_SESSION['b1_id'] = $row['MAX(b1_id)'];
}
$this->sql = "SELECT b1_name FROM $this->tableMaster WHERE b1_id = '".$_SESSION['b1_id']."' ";
$result = $this->hookup->query($this->sql);
while($row = $result->fetch_assoc()) {
$_SESSION['b1_name'] = $row['b1_name'];
}
$host = $_SERVER['HTTP_HOST'];
$uri = ''; //folder
$page = 'step_2.php';
header("Location: http://$host/$page");
} else {
$_SESSION['error'] = true;
$host = $_SERVER['HTTP_HOST'];
$uri = ''; //folder
$page = 'step_1.php';
header("Location: http://$host/$page");
}
}
//if the contact was not found, redirect to the first step.
if ($not_found){
$_SESSION['error'] = true;
$host = $_SERVER['HTTP_HOST'];
$uri = ''; //folder
$page = 'step_1.php';
header("Location: http://$host/$page");
}
} catch (Exception $e) {
print "There is a problem: ".$e->getMessage();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.