为什么我的if语句评估为true?

时间:2015-02-27 02:46:33

标签: php if-statement

我不确定为什么我的if声明总是在任何情况下验证为假。我一直在阅读代码,看起来并没有什么不对。如果有人能帮助我,那就太好了。

有问题的if语句是

if($dataValid && $_POST)

这是我的代码:

<?php
$instrument = trim($_POST["itemName"]);
$description = trim($_POST["description"]);
$supplier = trim($_POST["supplierCode"]);
$cost = trim($_POST["cost"]);
$price = trim($_POST["price"]);
$onHand = trim($_POST["onHand"]);
$reorderPoint = trim($_POST["reorder"]);
$dataValid = true ;

$instrumentChk = "/^[a-z0-9:; -,]+$/i";
$descriptionChk = "/^[a-z0-9\., -'\r\n]+$/im";
$supplierChk = "/^[a-z0-9 -]+$/i";
$costChk = "/^\d{0,}\.\d\d$/";
$priceChk = "/^\d{0,}\.\d\d$/";
$onHandChk = "/^\d*$/";
$reorderPointChk = "/^\d*$/";


if($_POST){
    if(!preg_match($instrumentChk, $instrument)) {
        $instrumentErr = "<span style='color:#f00'>Instrument name must be either letters, spaces,
        colon, semi-colon, dash, comma, apostrophe or numeric character (0-9)</span>";
        $dataValid = false;
    }

    elseif($instrument == ""){
        $instrumentErr = "<span style='color:#f00'>Instrument name must not be blank</span>";
        $dataValid = false;
    }

    if(!preg_match($descriptionChk, $description)) {
        $descriptionErr = "<span style='color:#f00'>description must be either letters, spaces,
        dashes, commas, apostrophes or digits</span>";
        $dataValid = false;
    }

    elseif($description == "") {
        $descriptionErr = "<span style='color:#f00'>Description must not be blank</span>";
        $dataValid = false;
    }

    if(!preg_match($supplierChk, $supplier)) {
        $supplierErr = "<span style='color:#f00'>Supplier code must be either
        letters, spaces, dashes, or numeric characters(0-9)</span>";
        $dataValid = false;
    }

    elseif($supplier == "") {
        $supplierErr = "<span style='color:#f00'>Supplier must not be blank</span>";
        $dataValid = false;
    }

    if(!preg_match($costChk, $cost)) {
        $costErr = "<span style='color:#f00'>You must enter monetary amounts only</span>";
        $dataValid = false;
    }

    elseif($cost == null) {
        $costErr = "<span style='color:#f00'>Cost cannot be blank</span>";
        $dataValid = false;
    }

    if(!preg_match($priceChk, $price)) {
        $priceErr = "<span style='color:#f00'>You must enter monetary amounts only</span>";
        $dataValid = false;
    }

    elseif($price == null) {
        $priceErr = "<span style='color:#f00'>Price cannot be blank</span>";
        $dataValid = false;
    }

    if(!preg_match($onHandChk, $onHand)) {
        $onHandErr = "<span style='color:#f00'>You must enter digits only</span>";
        $dataValid = false;
    }

    elseif($onHand == null) {
        $onHandErr = "<span style='color:#f00'>Instruments on hand cannot not be blank</span>";
        $dataValid = false;
    }

    if(!preg_match($reorderPointChk, $reorderPoint)) {
        $reorderPointErr = "<span style='color:#f00'>You must enter digits only</span>";
        $dataValid = false;
    }

    elseif($reorderPoint == null){
        $reorderPointErr = "<span style='color:#f00'>Reorder point cannot not be blank</span>";
        $dataValid = false;
    }

}

if($dataValid && $_POST) {
    $secret = file('/home/int322_151a14/secret/topsecret');
    $link = mysqli_connect(trim($secret[0]), trim($secret[1]), trim($secret[2]), trim($secret[3])) or die(mysqli_connect_error());
    $deleted = 'n';
    $backOrder = (isset($_POST['backOrder']) && $_POST['backOrder'] == 'y')?"y":"n";
    $sql_insert_query = 'INSERT INTO inventory values("","' . $instrument . '", "' . $description . '", "' . $supplier . '",
                          "' . $cost . '", "' . $price . '", "' . $onHand . '", "' . $reorderPoint . '", "' . $backOrder . '", "' . $deleted . '")';
    $sql_insert_result = mysqli_query($link, $sql_insert_query) or die("Query unsuccessful " . mysqli_error($link));
    if($sql_insert_result) {
        header("Location: view.php");
    } else {
        echo "Query unsuccessful - <a href=add.php>please try again</a>";
    }

} else {
    ?>
    <?php $page_title = 'add'; ?>
    <?php include "library.php"; ?>
    <?php heading(); ?>
    <h1>Chidi's Musical Instruments</h1>
    <header>
    <nav id="navigation">
        <a href="add.php">Add</a>
        <a href="view.php">View All</a>
    </nav>
    </header>
    <br/>
    <form action="" method='post'>
        <table id="newTable">
            <tr>
                <td>
                    <label>Instrument name:</label>
                    <input type="text" name="itemName" value="<? echo $instrument ?>"><? echo $instrumentErr ?>
                </td>
            </tr>
            <tr>
                <td>
                    <label>Description:</label>
                    <textarea rows="5" cols="20" name="description"><? echo $description ?></textarea><? echo $descriptionErr ?>
                </td>
            </tr>
            <tr>
                <td>
                    <label>Supplier Code:</label>
                    <input type="text" name="supplierCode" value="<? echo $supplier ?>"><? echo $supplierErr ?>
                </td>
            </tr>
            <tr>
                <td>
                    <label>Cost:</label>
                    <input type="text" name="cost" value="<? echo $cost ?>"><? echo $costErr ?>
                </td>
            </tr>
            <tr>
                <td>
                    <label>Selling Price:</label>
                    <input type="text" name="price" value="<? echo $price ?>"><? echo $priceErr ?>
                </td>
            </tr>
            <tr>
                <td>
                    <label>Number on Hand:</label>
                    <input type="text" name="onHand" value="<? echo $onHand ?>"><? echo $onHandErr ?>
                </td>
            </tr>
            <tr>
                <td>
                    <input type="submit" value="Submit">
                </td>
            </tr>

        </table>
    </form>
<?
}
?>
<?php footer(); ?>

如果数据有效则应评估为true并返回错误,因为它无法连接到数据库,而是只删除所有字段并且不执行任何操作。如果数据实际上无效,则会按原样重新填充字段并使您重试。如果if语句的计算结果为false,那么我的代码中没有任何地方告诉它什么都不做,而这似乎是if语句返回的内容。

1 个答案:

答案 0 :(得分:0)

你应该有这样的代码:

<?php

//You must verify first if the form was send
if(isset($_POST["itemName"])){

//you should have checked that $_POST["itemName"] existed before use it
$instrument = trim($_POST["itemName"]);
$description = trim($_POST["description"]);
$supplier = trim($_POST["supplierCode"]);
$cost = trim($_POST["cost"]);
$price = trim($_POST["price"]);
$onHand = trim($_POST["onHand"]);
$reorderPoint = trim($_POST["reorder"]);
$dataValid = true ;

$instrumentChk = "/^[a-z0-9:; -,]+$/i";
$descriptionChk = "/^[a-z0-9\., -'\r\n]+$/im";
$supplierChk = "/^[a-z0-9 -]+$/i";
$costChk = "/^\d{0,}\.\d\d$/";
$priceChk = "/^\d{0,}\.\d\d$/";
$onHandChk = "/^\d*$/";
$reorderPointChk = "/^\d*$/";

    if(!preg_match($instrumentChk, $instrument)) {
        $instrumentErr = "<span style='color:#f00'>Instrument name must be either letters, spaces,
        colon, semi-colon, dash, comma, apostrophe or numeric character (0-9)</span>";
        $dataValid = false;
    }

....

//Same validation
if($dataValid && isset($_POST['backOrder']) {
    $secret = file('/home/int322_151a14/secret/topsecret');
...
相关问题