php非空验证

2020-12-13 05:37

阅读:453

标签:style   blog   class   c   code   java   

  我想说这种方法是不是很常用的非空验证,现在的普遍使用的是javascript来验证非空,但是作为学习php的一些知识点,还是可以看看的。

先来看看commit.php中的方法

soscw.com,搜素材
php 
$db = mysql_connect(‘localhost‘,‘root‘,‘Ctrip07185419‘);
mysql_select_db(‘moviesite‘,$db) or die(mysql_error($db));
mysql_query(‘set names "gb2312"‘,$db);
?>

php
switch($_GET[‘action‘])
{
    case "add":
    {
        switch($_GET[‘type‘])
        {
            case ‘movie‘:
                $error = array();
                $movie_name = isset($_POST[‘movie_name‘]) ? trim($_POST[‘movie_name‘]) : ‘‘;
                if(empty($movie_name))
                {
                    $error[] = urlencode(‘Please enter an movie name‘);
                }
                
                $movie_type = isset($_POST[‘movie_type‘]) ? trim($_POST[‘movie_type‘]) : ‘‘;
                if(empty($movie_type))
                {
                    $error[] = urldecode(‘Please enter an movie type‘);
                }
                
                $movie_year = isset($_POST[‘movie_year‘]) ? trim($_POST[‘movie_year‘]) : ‘‘;
                if(empty($movie_year))
                {
                    $error[] = urldecode(‘Please enter an movie year‘);
                }
                
                $movie_leadactor = isset($_POST[‘movie_leadactor‘]) ? trim($_POST[‘movie_leadactor‘]) : ‘‘;
                if(empty($movie_leadactor))
                {
                    $error[] = urldecode(‘Please enter an movie leadactor‘);
                }
                
                $movie_director = isset($_POST[‘movie_director‘]) ? trim($_POST[‘movie_director‘]) : ‘‘;
                if(empty($movie_director))
                {
                    $error[] = urldecode(‘Please enter an movie director‘);
                }
                
                $movie_release = isset($_POST[‘movie_release‘]) ? trim($_POST[‘movie_release‘]) : ‘‘;
                if(empty($movie_release))
                {
                    $error[] = urldecode(‘Please enter an movie release‘);
                }
                else if(!preg_match(‘|^\d{2}-\d{2}-\d{4}$|‘ , $movie_release))
                {
                    $error[] = urlencode(‘Please enter a date in dd-mm-yyyy format‘);
                }
                else
                {
                    list($day, $month, $year) = explode(‘-‘, $movie_release);
                    if(!checkdate($month , $day , $year))
                    {
                        $error[] =  urldecode(‘please enter a validate date‘);
                    }
                    else
                    {
                        $movie_release = mktime(0, 0, 0, $month, $day, $year);
                    }
                }

                $movie_rating = isset($_POST[‘movie_rating‘]) ? trim($_POST[‘movie_rating‘]) : ‘‘;
                if(empty($movie_rating))
                {
                    $error[] = urldecode(‘Please enter an movie leadactor‘);
                }
                else if(!is_numeric($movie_rating))
                {
                    $error[] = urlencode(‘Please enter a numeric rating‘);
                }
                else if($movie_rating $movie_rating > 10)
                {
                    $error[] = urlencode(‘Please enter a rating between 1 and 10‘);
                }
                
                if(empty($error))
                {    
                    //var_dump($movie_rating);
                    //注意是movie_type不是mvioe_type,大错特错
                    $query = ‘insert into movie(movie_name,movie_year,movie_type,movie_leadactor,movie_director,movie_release,movie_rating) 
                    values("‘.$movie_name.‘",‘.$movie_year.‘,‘.$movie_type.‘,‘.$movie_leadactor.‘,‘.$movie_director.‘,‘.$movie_release.‘,‘.$movie_rating.‘)‘;
                
                    
                }
                else
                {
                    //注意是Location不是Lacotion,大错特错
                    header(‘Location:movie_checkformat.php?action=add&error=‘ . join($error , urlencode(‘
))); //var_dump($error); //var_dump($_POST); } break; } break; } case ‘edit‘: switch($_GET[‘type‘]) { case ‘movie‘: { $error = array(); $movie_name = isset($_POST[‘movie_name‘]) ? trim($_POST[‘movie_name‘]) : ‘‘; if(empty($movie_name)) { $error[] = urlencode(‘Please enter an movie name‘); } $movie_type = isset($_POST[‘movie_type‘]) ? trim($_POST[‘movie_type‘]) : ‘‘; if(empty($movie_type)) { $error[] = urldecode(‘Please enter an movie type‘); } $movie_year = isset($_POST[‘movie_year‘]) ? trim($_POST[‘movie_year‘]) : ‘‘; if(empty($movie_year)) { $error[] = urldecode(‘Please enter an movie year‘); } $movie_leadactor = isset($_POST[‘movie_leadactor‘]) ? trim($_POST[‘movie_leadactor‘]) : ‘‘; if(empty($movie_leadactor)) { $error[] = urldecode(‘Please enter an movie leadactor‘); } $movie_director = isset($_POST[‘movie_director‘]) ? trim($_POST[‘movie_director‘]) : ‘‘; if(empty($movie_director)) { $error[] = urldecode(‘Please enter an movie director‘); } $movie_release = isset($_POST[‘movie_release‘]) ? trim($_POST[‘movie_release‘]) : ‘‘; if(empty($movie_release)) { $error[] = urldecode(‘Please enter an movie release‘); } else if(!preg_match(‘|^\d{2}-\d{2}-\d{4}$|‘ , $movie_release)) { $error[] = urlencode(‘Please enter a date in dd-mm-yyyy format‘); } else { list($day, $month, $year) = explode(‘-‘, $movie_release); if(!checkdate($month , $day , $year)) { $error[] = urldecode(‘please enter a validate date‘); } else { $movie_release = mktime(0, 0, 0, $month, $day, $year); } } $movie_rating = isset($_POST[‘movie_rating‘]) ? trim($_POST[‘movie_rating‘]) : ‘‘; if(empty($movie_rating)) { $error[] = urldecode(‘Please enter an movie leadactor‘); } else if(!is_numeric($movie_rating)) { $error[] = urlencode(‘Please enter a numeric rating‘); } else if($movie_rating $movie_rating > 10) { $error[] = urlencode(‘Please enter a rating between 1 and 10‘); } if(empty($error)) { //var_dump($_POST); $query = ‘update movie set movie_name="‘.$_POST[‘movie_name‘].‘", movie_type=‘.$_POST[‘movie_type‘].‘, movie_year=‘.$_POST[‘movie_year‘].‘, movie_leadactor=‘.$_POST[‘movie_leadactor‘].‘, movie_director=‘.$_POST[‘movie_director‘]. ‘ movie_release=‘.$_POST[‘movie_release‘].‘ movie_rating=‘.$_POST[‘movie_rating‘].‘ where movie_id=‘.$_POST[‘movie_id‘]; } else { //注意是Location不是Lacotion,大错特错 header(‘Location:movie_checkformat.php?action=add&error=‘ . join($error , urlencode(‘
))); } } break; } break; default: break; } if(isset($query)) { mysql_query($query,$db) or die(mysql_error($db)); } ?> Commit

Done!

soscw.com,搜素材

其实逻辑很简答的,看下面的代码

$movie_leadactor = isset($_POST[‘movie_leadactor‘]) ? trim($_POST[‘movie_leadactor‘]) : ‘‘;
if(empty($movie_leadactor))
{
$error[] = urldecode(‘Please enter an movie leadactor‘);
}

其实就是使用isset方法来验证输入是否为空,如果是空的话把错误信息记入到一个数组$error中,然后使用header方法跳转到填写信息的页面中,跳转的方法看上去有点糟糕,如下header(‘Location:movie_checkformat.php?action=add&error=‘ . join($error , urlencode(‘
‘)));,是将所有错误信息使用换行符来连接起来放到url中来跳转,很我觉得这个有点不合适的。

 

来看填写信息的页面movie_checkformat.php,

soscw.com,搜素材
php 
$db = mysql_connect(‘localhost‘,‘root‘,‘Ctrip07185419‘) or die(‘can not connect to mysql‘);
mysql_select_db(‘moviesite‘,$db) or die(mysql_error($db));
mysql_query(‘set names gbk‘,$db);
if(isset($_GET[‘action‘]) && $_GET[‘action‘] == ‘edit‘) 
{
    $query = ‘SELECT movie_name,movie_type,movie_year,movie_leadactor,movie_director,movie_release,movie_rating FROM movie WHERE movie_id=‘.$_GET[‘id‘];
    //echo $query;
    $result = mysql_query($query , $db) or die(mysql_error($db));
    extract(mysql_fetch_assoc($result));
}
else
{
    $movie_name=‘‘;
    $movie_type=0;
    $movie_year=date(‘Y‘);
    $movie_leadactor=0;
    $movie_director=0;
    $movie_release=time();
    $movie_rating=5;
}
?>

    
<?php <span>echo <span>ucfirst</span>(<span>$_GET</span>[‘action‘]);?> Moviephp 
            if(isset($_GET[‘error‘]) && $_GET[‘error‘] != ‘‘)
            {
                echo
‘.$_GET[‘error‘].‘
; } ?>
Movie Name
Movie Type
Movie Year
Lead actor
Director
Movie release date
(dd-mm-YYYY)
Movie rating
php if(‘edit‘ == $_GET[‘action‘]) { echo; } ?>
soscw.com,搜素材

在html中的body开始的时候如果传递的error参数不为空则输出错误信息,代码如下:

if(isset($_GET[‘error‘]) && $_GET[‘error‘] != ‘‘)
{
echo ‘

‘.$_GET[‘error‘].‘
‘;
}
?>

很简单,这种写法也很糟糕,完全可以放在javascript中来实现,估计实际生产中很少会有人这么写的。

这里顺便看看一些常见的判断方法:

is_array():判断变量是否是一个数组
is_binary(): 判断变量是否是一个二进制串
is_bool():判断布尔类型变量true还是false,1还是0
is_callable(): 判断变量是否是一个可以访问的函数,估计就是判断是否是代理类型

is_int(): 判断整型
is_null(): 判断是否为空
is_numeric():判断是否是数字或者数字形式的字符串吧
is_object(): 判断变量是否是一个对象
is_resource(): 判断资源类型
is_string(): 判断是否是字符串类型
is_unicode(): 判断是否是unicode字符串

某些时候使用is_xx方法比较慢,使用ctype_xx类型的方法较快,但是它只能检验字符串类型,不能检验对象,数组,资源类型。ctype_xx方法举例如下:

ctype_alnum(): 判断变量是否是数字类型字符串
ctype_alpha(): 判断变量全是字符串
ctype_cntrl(): 判断是控制字符,就是转移字符如"\n"
ctype_digit(): 判断字符串全部是数字字符,不包含任何非数组字符"."这样的也不包含
ctype_graph(): 检查是否有任何可打印字符,除了空格(补)
ctype_lower() : 检查小写字母
ctype_print(): 检查是否都是可打印字符,就是不包含转移字符,和ctype_control()是相斥的
ctype_punct(): 检查是否有任何可打印字符而不是空格或字母数字字符
ctype_space(): 检查是否都是空白字符
ctype_upper() : 检查是否都是大写字符
ctype_xdigit(): 检查是否是16进制的字符串,只能包括 “0123456789abcdef”

 

还可以使用preg_match来检验输入,这个是用正则表达式的方式来进行检验的。

php非空验证,搜素材,soscw.com

php非空验证

标签:style   blog   class   c   code   java   

原文地址:http://www.cnblogs.com/tylerdonet/p/3737895.html


评论


亲,登录后才可以留言!