使用数据库中的数据验证表单

时间:2014-10-27 09:55:20

标签: php validation

我有正确验证的问题。请看看这堂课:

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();

    }
}

}

当在数据库中找到公司名称时,一切正常。如果在数据库中找不到新的公司名称,则会出现问题 - 它显示一个空白屏幕' :/

1 个答案:

答案 0 :(得分:0)

通过添加标记来查看是否找到联系人,您可以执行类似重定向到未找到联系人的第一步的操作:

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();

    }
}