如何插入我验证的信息? (SQL,PHP)

时间:2015-04-30 00:16:31

标签: php mysql validation

我有一个小问题,所以我写了一段HTML和PHP来注册一个帐户。现在我已经完成了验证,但是当所有验证都很好时,如何将我的数据插入到我的数据库中:所以没有错误。

我知道SQL语法,不用担心。我只想在所有验证都很好并且没有验证错误的情况下插入。

CODE:

<?php
        $usernameErr = $emailErr = $passwordErr = $password_valErr = "";
        $username = $email = $password = $password_val = "";

        if($_SERVER['REQUEST_METHOD'] == 'POST') {
            if(empty($_POST['username'])) {
                $usernameErr = "Name is required";
            } else {
                $username = validate_input($_POST['username']);
                if($username <= 6) {
                    $usernameErr = "Username must be longer as 6 characters.";
                }
                if(!preg_match("/^[a-zA-Z ]*$/", $username)) {
                    $usernameErr = "Only letters and white space allowed.";
                }       
            }

            if(empty($_POST['email'])) {
                $emailErr = "Email is required";
            } else {
                $email = validate_input($_POST['email']);
                if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                    $emailErr = "Invalid email format.";
                }           
            }

            if(empty($_POST['password'])) {
                $passwordErr = "Password is required";
            } else {
                $password = validate_input($_POST['password']);
                if($password <= 8)  {
                    $passwordErr = "Password must be longer as 8 characters.";
                }
                if(!preg_match("#[0-9]+#", $password)) {
                    $passwordErr = "Password must contain atleast 1 number.";
                }
            }

            if(empty($_POST['password_val'])) {
                $password_valErr = "Password_val is required";
            } else {
                $password_val = validate_input($_POST['password_val']);
                if($password_val != $password) {
                    $password_valErr = "Password_val must be equal to password.";
                }       
            }
        }
        ?>

        <table border="1">
            <tr>
                <td><label>Username</label><?=' <b>' . $usernameErr . '</b>';?></td>
                <td><input type="text" name="username" value="<?=$username;?>" placeholder="Enter your desired username..." /></td>
            </tr>
            <tr>
                <td><label>E-mail</label><?=' <b>' . $emailErr . '</b>';?></td>
                <td><input type="text" name="email" value="<?=$email;?>" placeholder="Enter your email address..." /></td>
            </tr>
            <tr>
                <td><label>Password<?=' <b>' . $passwordErr . '</b>';?></label></td>
                <td><input type="password" name="password" placeholder="Enter your desired password..." /></td>
            </tr>
            <tr>
                <td><label>Repeat Password<?=' <b>' . $password_valErr . '</b>';?></label></td>
                <td><input type="password" name="password_val" placeholder="Repeat your chosen password.." /></td>
            </tr>
            <tr>
                <td><input type="submit" name="register" value="Register" /></td>
            </tr>
        </table>
    </form>

1 个答案:

答案 0 :(得分:4)

试试这个:

if(empty($_POST['username'])) {
    $usernameErr="Name is required";
} else {
    $username=validate_input($_POST['username']);
    if($username<=6) {
        $usernameErr="Username must be longer as 6 characters.";
    }
    if(!preg_match("/^[a-zA-Z ]*$/",$username)) {
        $usernameErr="Only letters and white space allowed.";
    }
}
if(empty($_POST['email'])) {
    $emailErr="Email is required";
} else {
    $email=validate_input($_POST['email']);
    if(!filter_var($email,FILTER_VALIDATE_EMAIL)) {
        $emailErr="Invalid email format.";
    }
}
if(empty($_POST['password'])) {
    $passwordErr="Password is required";
} else {
    $password=validate_input($_POST['password']);
    if($password<=8) {
        $passwordErr="Password must be longer as 8 characters.";
    }
    if(!preg_match("#[0-9]+#",$password)) {
        $passwordErr="Password must contain atleast 1 number.";
    }
}
if(empty($_POST['password_val'])) {
    $password_valErr="Password_val is required";
} else {
    $password_val=validate_input($_POST['password_val']);
    if($password_val!=$password) {
        $password_valErr="Password_val must be equal to password.";
    }
}
if(!isset($usernameErr,$usernameErr,$emailErr,$passwordErr,$password_valErr)) {
    //Insert QUERY
}