dongyi2534 2015-02-27 02:46
浏览 22
已采纳

为什么我的if语句评估为true? [关闭]

I'm unsure why my if statementy always validates to false no matter the situation. I keep reading the code and it doesnt look like theres anything wrong. If anyone can help me out that would be great.

The if statement in question is

if($dataValid && $_POST)

Here's my code:

<?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\., -'
]+$/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(); ?>

It should evaluate to true if the data is valid and return an error as it wont be able to connect to the database, but instead it just erases all the fields and does nothing. If the data is actually invalid it repopulates the fields as it should and makes you retry. There is nowhere in my code that tells it to do nothing if the if statement evaluates to false, and it seems like that's what the if statement is returning.

  • 写回答

1条回答 默认 最新

  • dongyou9721 2015-02-27 03:04
    关注

    You should have your code like this:

    <?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\., -'
    ]+$/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');
    ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?