mayfish 数据入库验证代码_PHP
一般在把数据写入数据库之前,先对将要写入的数据进行校验,可以避免出现比较严重的安全问题(例如一般性的SQL注入攻击)。
mayfish 可以灵活的自定义将要执行写入的数据内容的校验规则,以减少开发人员手动对每一个字段的数据进行校验的麻烦。
例子如下:
一、首先定义数据库模块
复制代码 代码如下:
class MemberModel extends AppModel
{
/** 设置数据库表名称 **/
protected $tableName = "members";
/**
* 数据验证规则
*/
protected $verify = array(
array("NotEmpty", "username", "用户名不能留空"),
array("hasOne", "username", "此用户已经存在,请换另一个用户名称再试一次"),
array("NotEmpty", "password", "密码不能留空"),
array("NotEmpty", "email", "邮箱地址不能留空"),
array("isEmail", "email", "邮箱地址格式不正确"),
array("hasOne", "email", "邮箱地址已经被占用")
);
/**
* 覆盖父类添加数据入库的方法
* 先对用户密码进行md5加密,再调用父类的方法写入数据库中
*/
public function create($data) {
$data = array_map("addslashes", $data); //将数据中的标点符号(单、双引号)进行安全转义
$data["password"] = md5($data["password"]);
return parent::create($data);
}
}
?>
二、执行数据写入操作
复制代码 代码如下:
//执行写入数据的片段...
//执行数据入库的操作
private function PostData() {
$fields = array("username", "password", "email");
$post = array_map("trims", $_POST); //清除所有数据两边多余的空格
$post = parseHTML($post, $fields); //将指定的字段内容进行清除HTML处理
$data = parseFields($post, $fields); //提取可以写入数据库的字段(防止别人绕过你的页面进行提交一些别有用心的数据)
$DB = & M("member");
//进行数据验证
if (!$DB->verify($data)) {
//验证失败,取出失败的原因,并提交到模板页面中
$this->assign("error", $DB->getVerifyError());
//把提交过来的数据也提交到模板中(用以实现用户好像没有离开过页面的感觉)
$this->assign("default", $post);
//渲染注册页面模板
$this->display("/register.html");
}
else {
//写入数据库
$result = $DB->create($data);
//返回布尔型,说明数据写入失败,渲染注册页面模板
if (is_bool($result)) {
$this->assign("default", $post);
$this->display("/register.html");
}
else {
//注册成功,渲染注册成功页面模板
$this->assign("username", $data["username"]);
$this->display("/reg_success.html");
}
}
}
可执行验证的规则有
NotEmpty 不能为空
Number 只能是整数
isEmail 邮箱地址是否正确
hasOne 是否是唯一(是否重复,是否已经存在)
Regex 自定义正则表达式
验证的格式为
array(验证方法, 进行验证的字段名称, 验证错误的提示信息)
对于正则表达示的验证
array("Regex", "mobile", '/^13\d{9}$/', "用户名不能留空")
MayFish 下载

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.
