在php表单上验证大于/小于数字的数量

时间:2013-01-31 00:04:15

标签: php mysql forms validation

我正在尝试创建一个表单,其中登录用户可以输入销售(ltd_entry_amount),但只有200.00美元或更高的销售额,但没有运气。如果我摆脱'> 199.99'表单工作正常但是现在'请检查所有必填字段是否完整,然后再试一次。'消息显示。有人可以帮忙吗?

                <?php 

                require_once ('./includes/config.inc.php');

                $page_title = 'Log a Sale';
                include ('./includes/header.html');

                if (!isset($_SESSION['ltd_user_id'])) {

                   $url = 'http://' . $_SERVER['HTTP_HOST']
                    . dirname($_SERVER['PHP_SELF']);
                   if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
                        $url = substr ($url, 0, -1); // Chop off the slash.
                   }
                   $url .= '/login.php'; 

                ob_end_clean(); // Delete the buffer.
                header("Location: $url"); 
                exit(); // Quit the script.
                }
                $users = $_SESSION['ltd_user_id'];

                if (isset($_POST['submitted'])) {// Handle the form.

                   require_once ('database.php');
                   // Connect to the database.    

                    $errors = array(); // Initialize error array.

                    // Check for a Invoice Number.
                    if (empty($_POST['ltd_invoice_no'])) {
                       $errors[] = '<p>• You forgot to enter your Invoice Number.</p>';
                    } else {
                       $inv = escape_data($_POST['ltd_invoice_no']);
                    }

                    // Check for invoice amount.
                    if (empty($_POST['ltd_entry_amount']) < 199.99) {
                       $errors[] = '<p>• You forgot to enter your Total Amount. Please ensure it is at least $200.00</p>';
                    } else {
                       $amount = escape_data($_POST['ltd_entry_amount']);
                    }

                    // Check for business name.
                    if (empty($_POST['ltd_business_name'])) {
                       $errors[] = '• You forgot to enter your Dealership Name.';
                    } else {
                       $dealer = escape_data($_POST['ltd_business_name']);
                    }

                    if (empty($errors) && $amount)  { // If everything's OK. // If everything's OK.

                      // Make sure the invoice number is available.
                      $uid = @mysql_insert_id(); //Get the url ID.

                        // Add the user.
                        $query = "INSERT INTO ltd_sales_list (ltd_item_id , ltd_user_id, ltd_invoice_no, ltd_entry_amount, ltd_entry_userdate, ltd_business_name, ltd_entry_date) VALUES
                        ('$uid', '$users', '$inv', '$amount', '$_POST[ltd_entry_userdate]', '$dealer' , NOW())";
                        $result = @mysql_query ($query); // Run the query.


                        if (mysql_affected_rows() == 1) {
                        // If it ran OK.
                           echo 'Your sale is registered into the system.';
                           include ('./includes/footer.html'); // Include the HTML footer.
                           exit();

                        } else { // If it did not run OK.
                           echo 'You could not be registered due to a system error. We apologize for any inconvenience.';
                        }

                   } else { // If one of the data tests failed.
                      echo 'Please check all manatory fields are complete and try again.';
                   }

                   mysql_close(); // Close the database connection.

                } // End of the main Submit conditional.
                ?>                          


                <form action="logsale.php" method="post">



                                        <table width="550" border="0" cellspacing="1" cellpadding="5">

                                              <tr>
                                                <td width="250"><div align="right">Invoice Number <span class="red_light">*</span></div></td>
                                                <td width="267">
                                                <input type="text" name="ltd_invoice_no" size="30" maxlength="30" value="<?php if (isset($_POST['ltd_invoice_no'])) echo $_POST['ltd_invoice_no']; ?>" /></td>
                                              </tr>
                                              <tr>
                                                <td><div align="right">Total Amount of sale 
                                                        <em><strong>(exc. GST)</strong></em> <span class="red_light">*</span></div></td>
                                                <td>
                                                <input type="text" name="ltd_entry_amount" size="30" maxlength="30" 
                                                value="<?php if (isset($_POST['ltd_entry_amount'])) echo $_POST['ltd_entry_amount']; ?>" /></td>
                                              </tr>
                                              <tr>
                                                <td></td>
                                                <td>Please enter number only</td>
                                              </tr>
                                              <tr>
                                                <td><div align="right">Date of Invoice</div></td>
                                                <td>
                                                <script type="text/javascript">
                                                    $(function() {
                                                    $("#datepicker").datepicker({ dateFormat: "dd/mm/yy" }).val()
                                                    });
                                                    </script>

                                                <input size="30" maxlength="10" id="datepicker" name="ltd_entry_userdate" type="text" value="<?php if (isset($_POST['ltd_entry_userdate'])) echo $_POST['ltd_entry_userdate']; ?>" /></td>

                                              </tr>
                                                <tr>
                                                <td></td>
                                                <td><span class="sml_italics"><strong>Please enter as <strong>DD-MM-YYYY</strong></td>
                                              </tr>
                                              <tr>
                                                <td><div align="right">Dealership <span class="red_light">*</span></div></td>
                                                <td><input type="text" name="ltd_business_name" size="50" maxlength="60" value="<?php if (isset($_POST['ltd_business_name'])) echo $_POST['ltd_business_name']; ?>" /></td>
                                               </tr> 

                                                <tr>
                                                <td></td>
                                                <td><p><input type="submit" name="submit" value="Submit" /></p><input type="hidden" name="submitted" value="TRUE" />
                                                </td>
                                                </tr>

                                            </table>


                  </form>


                 <?php
                include ('./includes/footer.html');
                ?> 

2 个答案:

答案 0 :(得分:3)

if(empty($ _ POST ['ltd_entry_amount'])&lt; 199.99){

您正在比较布尔值和浮点数

您必须将两个条件分开:

if(empty($ _ POST ['ltd_entry_amount'])或$ _POST ['ldt_entry_amount']&lt; 199.99){

答案 1 :(得分:1)

您的if语句看起来有问题:

if (empty($_POST['ltd_entry_amount']) < 199.99)

应该是

if (empty($_POST['ltd_entry_amount']) || $_POST['ltd_entry_amount'] < 199.99)