php字段验证出错

时间:2016-11-07 08:29:29

标签: php html

我在PHP页面上有一个表单,它将联系人添加到MySQL表中。

但我的验证拒绝工作。我已经编辑了几次但是当我点击添加新联系人按钮时,即使某些字段为空,它也会添加它们。

我仍然掌握了PHP,但我确信我的错误很简单。

<?php
// Don't post the form until the submit button is pressed.
$requiredFields = array (
        "name",
        "email",
        "extension",
        "extension" 
); // Add the 'name' for all required fields to this array
$errors = false;
if (isset ( $_POST ['Submit'] )) {
    // Clean all inputs
    array_walk ( $_POST, 'check_input' );

    // Loop over requiredFields and output error if any are empty
    foreach ( $requiredFields as $r ) {
        if (strlen ( $_POST [$r] ) == 0) {
            $errors = true;
            break;
        }
    }

    // Error/success check
    if ($errors == true) {
        echo 'Fields marked with a * are required';
    } else {
        // no errors
        // ...
    }
}

// check_input function
function check_input(&$data) {
    $data = trim ( $data );
    $data = stripslashes ( $data );
    $data = htmlspecialchars ( $data, ENT_QUOTES );
    return $data;
}
?>
<h2>Add Contact</h2>
<form name="form1" action="&lt;?=$_SERVER['PHP_SELF'];?&gt;?mode=added"
    method="post">
    <table class="tableStyleClassTwo">
        <tr>
            <td>Name:</td>
            <td><div align="left">
                    <input type="text" name="name" />
            </div></td>
        </tr>
        <tr>
            <td>Phone:</td>
            <td><div align="left">
                    <input type="text" name="phone" />
            </div></td>
        </tr>
        <tr>
            <td>Email:</td>
            <td><div align="left">
                    <input type="text" name="email" />
            </div></td>
        </tr>
        <tr>
            <td>Extension:</td>
            <td><div align="left">
                    <input type="text" name="extension" />
            </div></td>
        </tr>
        <tr>
            <td>Department:</td>
            <td><select name="department">
                    <option value="ADMIN">ADMIN</option>
                    <option value="AFTER-SALES DIRECTOR">AFTER-SALES DIRECTOR</option>
                    <option value="ALPINE DEALER PRINCIPAL">ALPINE DEALER PRINCIPAL</option>
                    <option value="AUTO ARMOUR/AUTO ENHANCE - FITMENT CENTRE (Smash and Grab)">AUTO ARMOUR/AUTO ENHANCE - FITMENT CENTRE (Smash and Grab)</option>
                    <option value="BANDIT-VW">BANDIT-VW</option>
                    <option value="BOOKINGS VW">BOOKINGS VW</option>
                    <option value="DRIVEWAY/WASHBAYS">DRIVEWAY/WASHBAYS</option>
                    <option value="FINANCE AND INSURANCE">FINANCE AND INSURANCE</option>
                    <option value="IT DEPARTMENT">IT DEPARTMENT</option>
                    <option value="MARKETING DEPARTMENT">MARKETING DEPARTMENT</option>
                    <option value="MASTER CARS">MASTER CARS</option>
                    <option value="MAYOR OF PINETOWN">MAYOR OF PINETOWN</option>
                    <option value="NEW CAR PREP DEPARTMENT">NEW CAR PREP DEPARMENT</option>
                    <option value="NUMBER PLATES">NUMBER PLATES</option>
                    <option value="PANELBEATER - EASIFIX - CAR CARE">PANELBEATER - EASIFIX - CAR CARE</option>
                    <option value="PARTS">PARTS</option>
                    <option value="PARTS DISPATCH">PARTS DISPATCH</option>
                    <option value="PARTS TELESALES">PARTS TELLESALES</option>
                    <option value="USED CAR PREP AND ORDERS">USED CAR PREP AND ORDERS</option>
                    <option value="VW NEW CARS ADMIN AND STOCK CONTROL">VW NEW CARS ADMIN AND STOCK CONTROL</option>
                    <option value="VW NEW VEHICLE SHOWROOM">VW NEW VEHICLE SHOWROOM</option>
                    <option value="VW SERVICE ADVISORS">VW SERVICE ADVISORS</option>
                    <option value="VW WORKSHOP">VW WORKSHOP</option>
                    <option value="VW WORKSHOP FOREMEN">VW WORKSHOP FOREMEN</option>
                    <option value="WARRANTY &amp; CLAIMS">WARRANTY & CLAIMS</option>
                    <option value="WORKSHOP DRIVERS">WORKSHOP DRIVERS</option>
            </select></td>
        </tr>
        <tr>
            <td colspan="2" align="centre"><a href="javascript:history.go(-1);">Back</a>
                | <input name="Submit" type="submit" id="Submit" value="Add New Contact" <?php if($disable ==1){?> disabled <?php } ?> />
            </td>
        </tr>
        <input type="hidden" name="mode" value="added">
    </table>
</form>
<?php
break;
//added a record
case 'added':
//first setup the vars
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$extension = $_POST['extension'];
$department = $_POST['department'];
//then lets use'em
$sql = "INSERT INTO address (name, phone, email, extension, department) VALUES ('" . $name . "','" . $phone . "','" . $email . "','" . $extension . "','" . $department . "')";
//echo $sql;
//return;
mysql_query($sql);
//done take me back to the main page
header('location: ' . $_SERVER['PHP_SELF']);
break;

0 个答案:

没有答案