如何判断用户是否已将表单的一部分留空

时间:2011-11-28 09:50:12

标签: php sql

我创建了一个php页面,允许用户在我的数据库中输入不同的产品。我已经设置了正确工作的不同验证,但无法弄清楚如何判断用户是否将表单的一部分留空或整个空白。它们有三个文本框,用于id,item和cost。有谁知道我怎么能看到这些文本框中的每一个都是空白的?感谢

<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Adding Products</title>
    </head>
    <body>
        <?php
         $db_server = "server";
    $db_username = "name";
    $db_password = "pass";

       $con = mysql_connect($db_server, $db_username, $db_password);if (!$con)
                {
                    die('Could not connect: ' . mysql_error());
                }

               $database = "Product";  

              $er = mysql_select_db($db_username);
        if (!$er) 
        {
         print ("Error - Could not select the database");
         exit;
        }       

        $tbl_name ="Product";

        $ProductCode=$_POST['ProductCode'];
        $Description=$_POST['Description'];
        $Price=$_POST['Price']; 


            $ProductCode=mysql_real_escape_string($ProductCode);
            $Description=mysql_real_escape_string($Description);
            $Price=mysql_real_escape_string($Price);


             $status = "OK";

            if(strlen($ProductCode)>10)
            {
                echo('Your product code can only be a max of ten characters<BR>');
                $status= "NOTOK";
            }

            if(strlen($Description)>40)
            {
                echo('Your description of the product can only be 40 characters<BR>');
                $status ="NOTOK"; 
            }


            if(is_float($Price))
            {
                $english_format_number = number_format($Price); //Changing number to floating point
                //echo ('Your number must be in floating point.<BR>');
                //$status ="NOTOK";
            }

               if($status<>"OK")
               { 
                   echo('Some of your inputs were not in the correct format.<BR>');
               }
               else
               {
            $sql="INSERT INTO $tbl_name(Product_Code, Description, Price)VALUES('$ProductCode', '$Description', '$Price')";
            $result=mysql_query($sql);
            echo ('Your items have been inserted into the databae.');
               }



 ?>

4 个答案:

答案 0 :(得分:1)

您可能正在寻找empty()。基本上你需要的东西是这样的:

if (empty($_POST['field1']) || empty($_POST['field2'])) {
    // handle error
}

答案 1 :(得分:1)

if (!empty($_POST['ProductCode'])) {
    $ProductCode = $_POST['ProductCode'];
} else {
    echo 'Please, fill in Product Code field';
}

答案 2 :(得分:1)

我认为

empty()就是你所追求的。

e.g。

if (empty($_POST['ProductCode'])) {
  // product code is empty
}

但是,有一点需要注意:empty()认为'0'(包含零的字符串)为空。如果这是您的某个字段的有效值,那么您最好这样做:

if (!isset($_POST['ProductCode']) || trim($_POST['ProductCode']) === '') {
  // product code is empty
}

答案 3 :(得分:1)

if(!isset($ProductCode) && empty($ProductCode))
        {
            echo('your message');
            $status= "NOTOK";
        }

        if(!isset($Description) && empty($Description))
        {
           echo('your message');
            $status ="NOTOK"; 
        }


        if(!isset($Price) && empty($Price))
        {
            echo('your message');

            //$status ="NOTOK";
        }

编辑:这可能会有所帮助

相关问题