dongxi8993 2015-12-10 08:48
浏览 40

php使用过滤器和验证插入数据

I have a database and I want to make sure that fields are not empty, and if not I want to save them into database with this sanitized function. So after fields are filled and sanitized I want to save them to database.

<?php
// Initialize variables to null.
$nameError ="";
$emailError ="";

                    // On submitting form below function will execute.
                    if(isset($_POST['submit'])){
                            if (empty($_POST["name"])) {
                                $nameError = "Name is required";
                                } else {
                                $name = test_input($_POST["name"]);
                                // check name only contains letters and whitespace
                                if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
                                $nameError = "Only letters and white space allowed";
                            }
                            }

                    if (empty($_POST["email"])) {
                        $emailError = "Email is required";
                        } else {
                        $email = test_input($_POST["email"]);
                        // check if e-mail address syntax is valid or not
                        if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) {
                        $emailError = "Invalid email format";
                    }
                    }   


}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
//php code ends here
?>

<!DOCTYPE html>
<html>
<head>
<title>Form Validation with PHP - Demo Preview</title>
<meta content="noindex, nofollow" name="robots">
<!--<link href="style.css" rel="stylesheet">-->
</head>
<body>
<div class="maindiv">
<div class="form_div">
<div class="title">
<h2>Form Validation with PHP.</h2>
</div>
<form action="index.php" method="post">
<h2>Form</h2>
<span class="error">* required field.</span><br/>
Name:
<input class="input" name="name" type="text" value=""><br/>
<span class="error">* <?php echo $nameError;?></span><br/>
E-mail:
<input class="input" name="email" type="text" value=""><br/>
<span class="error">* <?php echo $emailError;?></span><br/>

<input class="submit" name="submit" type="submit" value="Submit">
</form>
</div>
</body>
</html>
  • 写回答

1条回答 默认 最新

  • dousao6313 2015-12-10 09:49
    关注

    html side: For checking on inputs like name and email, I suggest using the html attribute required on your input tags. And also, you can utilize the type of input like email:

    <input name="name" type="name" required>
    <input name="email" type="email" required>
    

    required attribute in html checks during submission of form whether you have inputted a value. If there are no values, then a warning will prompt and tell you that it is required.

    an email type in input tag validates whether the user input has proper format for the email.

    php side: your sanitize method and validation have no problems, but if you are looking for the best implementation regarding form validation, I suggest using a function or better, add it in a form validator class. There is a simple example here of a good validator class: Easiest Form validation library for PHP

    You can make your own class based on your preferences and of course you can experience the advantages of using oop design in your code.

    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?