欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

php非空验证

程序员文章站 2022-03-16 07:55:20
...

我想说这种方法是不是很常用的非空验证,现在的普遍使用的是javascript来验证非空,但是作为学习php的一些知识点,还是可以看看的。 先来看看commit.php中的方法 ? php $db = mysql_connect ('localhost','root','Ctrip07185419' ); mysql_select_db ('movie

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

先来看看commit.php中的方法

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