注册表单提交到数据库PHP

时间:2016-03-22 19:38:26

标签: php html mysql forms

我是PHP的初学者,我正在尝试为项目构建一个注册表单,我使所有的验证成为可能,但是我不知道应该去哪里,我希望表单验证以下所有内容然后提交,如果一切都是真的。

PHP代码:

Record with colours added -> Gulp runs -> New colour variables are used within the stylesheet generation. 

3 个答案:

答案 0 :(得分:0)

查看我的评论。有不同的方法来处理提交,并且页面上通常有多个表单。我们假设您的表单有一个提交按钮。

<input type="submit" name="submit-form" value="Done">

伪代码就是这样的:

$formErrors = array();
// You would need to add this as a global to all your functions, and as mentioned in my comment, add your errors to this array rather than echoing them out from the functions

if (($_SERVER['REQUEST_METHOD'] == 'POST') && isset($_POST['submit-form'])) && checkblank() && checkPass() && ...etc) {
    //form was submitted and is ok
} else {
    //You echo out your form.
}

答案 1 :(得分:0)

为什么不使用jquery验证器插件?这是一个非常好的选择。

请检查:http://jqueryvalidation.org/

答案 2 :(得分:0)

我知道你是php的新手,但你必须记住所有后端代码都必须得到保护。这就是你需要进行大量检查的原因。此外,当您处理数据库时,您需要保护代码免受注入。现在你会发现有些人告诉你用javascript做这件事,不要听他们这样做!

任何人都可以在不使用你的js代码的情况下进行帖子查询,因此你的所有js检查都会被绕过。当然,您可以在发送到服务器之前在浏览器中进行一层检查,但您也必须在服务器中进行检查。

因此,当您检查发送给服务器的元素时,您必须检查它是否存在(!=null),这可以使用函数isset轻松完成:

    if ( isset($_POST['reg-username'],$_POST['reg-email'],$_POST['reg-password'], $_POST['reg-confirmPassword'] ){
         // Further checks
    }else{
         // call a function that will return an error message
         error($PARAMETERS_NOT_SET)
    }

在数据库查询中使用客户端参数时,必须记住的第二件事是,检查是否有注入。这可能有点痛苦,这就是你必须使用prepared queries

的原因
$statement ="SELECT * FROM users WHERE username LIKE :username OR email LIKE :email ";
$sth = $dbh->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$sth->execute(array("username"=>$regUsername,"email"=>$regEmail));
$resultList= $sth->fetchAll();

PS:在stackoverflow中看到阿拉伯语非常酷。

相关问题