PHP表单验证不起作用(总是说输入为空)

时间:2016-08-23 17:16:33

标签: php forms validation

我编写了一些代码,它从提交的表单中获取信息并将其添加到数据库的表中。代码在将数据添加到数据库时没有任何问题,但是我在表单验证方面遇到了问题。如果表单中的任何字段为空,我已编写一些代码来显示错误消息,但无论字段是否为空,都会显示错误消息。以下是表单的代码:

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

    <div class = "form-group">
        <label for = "item_name">Item Name</label>
        <input type = "text" class = "form-control" name = "item_name" id = "item_name" placeholder="Item Name">
    </div>

    <div class = "form-group">
        <label for = "item_desc">Item Description</label>
        <input type = "text" class = "form-control" name = "item_desc" id = "item_desc" placeholder="Item Description">
    </div>

    <div class = "form-group">
        <label for = "item_price">Item Price</label>
        <input type = "text" class = "form-control" name = "item_price" id = "item_price" placeholder="Item Price">
    </div>

    <div class = "form-group">
        <label for = "item_cat">Item Category</label>
        <select class = "form-control" name = "item_cat" id = "item_cat">
            <?php

            //Get categories and names from the category table
            $query = "SELECT category_id, category_title FROM item_category ORDER BY category_title";
            $result = mysqli_query($db,$query);
            while($row=mysqli_fetch_assoc($result))
            {
                print_r($row);
                echo "<option value = '" . $row['category_id'] . "'>";
                echo $row['category_title'];
                echo "</option>";
            }
            ?>
        </select>
    </div>

        <div class = "row">

            <div class = "col-sm-6"><input type = "submit" class="btn btn-info" value="Submit"></div>
            <div class = "col-sm-6"><a href = "admin.php" class = "btn btn-default" role = "button">Return</a></div>

        </div>

</form>

以下是验证的代码:

//A function to remove unnecessary data and turn special characters into their escape codes
function testInput($data)
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}

//a function to display an error and exit
function dispErr($error)
{
    echo $error;
    exit();
}

if($_SESSION['authuser']!=1)
{
    echo "Access denied";
    exit();
}

//Make sure all values are set
if($_SERVER["REQUEST_METHOD"] == "POST")
{
    //Check no fields are blank
    if(empty($POST["item_name"]))
    {
        dispErr("Item name must not be empty");
    }
    if(empty($POST['item_desc']))
    {
        dispErr("Item description must not be empty");
    }
    if(empty($POST['item_cat']))
    {
        dispErr("Item category must not be empty");
    }
    if(empty($POST['item_price']))
    {
        dispErr("Item price must not be empty");
    }

    $item_name = testInput($_POST['item_name']);
    $item_desc = testInput($_POST['item_desc']);
    $item_cat  = testInput($_POST['item_cat']);
    $item_price = testInput($_POST['item_price']);
}
else
{
    exit();
}

0 个答案:

没有答案