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 关于#网络安全#的问题:求ensp的网络安全,不要步骤要完成版文件
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥20 使用Photon PUN2解决游戏得分同步的问题
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM