数据库中没有数据,但从if语句返回错误

时间:2014-07-31 21:22:28

标签: php database mysqli

<?php
    include("Header.php");

    echo "<br>";

    $stockCode  = "";
    $stockItem  = "";
    $stockLeft  = "";
    $stockOut   = "";
    $minimum    = "";
    $pricePI    = "";
    $location   = "";

    // Check if posts are set
    if(isset($_POST['stockCode'])){
        $stockCode = $_POST['stockCode'];
    }
    if(isset($_POST['stockItem'])){
        $stockItem = $_POST['stockItem'];
    }
    if(isset($_POST['stockLeft'])){
        $stockLeft = $_POST['stockLeft'];
    }
    if(isset($_POST['stockOut'])){
        $stockOut = $_POST['stockOut'];
    }
    if(isset($_POST['minimum'])){
        $minimum = $_POST['minimum'];
    }
    if(isset($_POST['pricePI'])){
        $pricePI = $_POST['pricePI'];
    }
    if(isset($_POST['location'])){
        $location = $_POST['location'];
    }

    // Do validation before adding to database
    if(
    ($stockCode == null)||
    ($stockItem == null)||
    ($stockLeft == null)||
    ($stockOut  == null)||
    ($pricePI   == null)||
    ($location  == null)||
    ($location  == "Select Location")
    ){
        echo "<div class='font3'>Fields Stock Code, Stock Item, Stock Left, Stock Out, Price Per Item and location must be filled.</div>";
    } else {
        // Write to database
        $connection = mysqli_connect($host,$user,$pass,$dbnm);

        // Check to see if the Stock Code already exists in the database
        $checkQ = "SELECT COUNT(*) FROM Ekhaya_Inventory WHERE ((ekhaya_inventory_stock_code = '" . $stockCode . "') AND (ekhaya_inventory_location = '" . $location . "'))";
        $result = mysqli_query($connection,$checkQ);



        while($row[0] = mysqli_fetch_array($result)){
            echo $row[0];
            if($row[0] == 0){
                $quick_cal = $stockLeft * $pricePI;
                $formated_cal = number_format($quick_cal,2);

                $myquery = 
                    "INSERT INTO Ekhaya_Inventory 
                    (
                        ekhaya_inventory_stock_code,
                        ekhaya_inventory_stock_item,
                        ekhaya_inventory_quantity_stock_left,
                        ekhaya_inventory_quantity_stock_out,
                        ekhaya_inventory_quantity_minimum,
                        ekhaya_inventory_price_per_item,
                        ekhaya_inventory_value_of_stock_left,
                        ekhaya_inventory_location,
                        ekhaya_inventory_date_time_modified
                    )
                    VALUES 
                    (
                        '" . $stockCode . "',
                        '" . $stockItem . "',
                        '" . $stockLeft . "',
                        '" . $stockOut . "',
                        '" . $minimum . "',
                        '" . $pricePI . "',
                        '" . $formated_cal . "',
                        '" . $location . "',
                        '" . $date . "'
                    )
                    ";

                mysqli_query($connection,$myquery);
                echo "<div class='font3'>New Item Added Successfully</div>";

                // Add a log to database
                $query = 
                "INSERT INTO Ekhaya_Logs 
                (
                    ekhaya_logs_name,
                    ekhaya_logs_surname,
                    ekhaya_logs_username,
                    ekhaya_logs_activity,
                    ekhaya_logs_ip_address,
                    ekhaya_logs_date_time
                ) VALUES (
                    '" . $_SESSION['SECURE@NAME']  . "',
                    '" . $_SESSION['SECURE@SURNAME']  . "',
                    '" . $_SESSION['SECURE@USERNAME'] . "',
                    '" . $_SESSION['SECURE@NAME']  . " " .  $_SESSION['SECURE@SURNAME']  . " added [ITEM] Stock Code: " . $stockCode . " to " . $location . "'s inventory',
                    '" . $_SERVER['REMOTE_ADDR'] . "',
                    '" . $date . "')";
                mysqli_query($connection,$query);   

            } else {
                echo "<div style='color:red' class='font3'>Stock Code " . $stockCode . " already exists for " . $location . "</div>";
            }
        }


        mysqli_close($connection);



        $stockCode  = "";
        $stockItem  = "";
        $stockLeft  = "";
        $stockOut   = "";
        $minimum    = "";
        $pricePI    = "";
        $location   = "";

    }

?>

</br>
<form action="AddItem.php" method="post">
    <table class="table_mod">
        <tr>
            <td class="td_mod">
                Stock Code:
            </td>
            <td class="td_mod">
                <input type="text" name="stockCode">
            </td>
        </tr>
        <tr>
            <td class="td_mod">
                Stock Item:
            </td>
            <td class="td_mod">
                <input type="text" name="stockItem">
            </td>
        </tr>
        <tr>
            <td class="td_mod">
                Stock Left:
            </td>
            <td class="td_mod">
                <input type="text" name="stockLeft">
            </td>
        </tr>
        <tr>
            <td class="td_mod">
                Stock Out:
            </td>
            <td class="td_mod">
                <input type="text" name="stockOut">
            </td>
        </tr>
        <tr>
            <td class="td_mod">
                Minimum:
            </td>
            <td class="td_mod">
                <input type="text" name="minimum">
            </td>
        </tr>
        <tr>
            <td class="td_mod">
                Price Per Item:
            </td>
            <td class="td_mod">
                <input type="text" name="pricePI">
            </td>
        </tr>
        <tr>
            <td class="td_mod">
                Location:
            </td>
            <td class="td_mod">
                <select class="textbox_login" name="location">
                    <option>Select Location</option>
                    <option value="Tech Stud">Tech Stud</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>
                <input type="Submit" value="Submit New Item">
            </td>
        </tr>
    </table>
</form>

有人可以告诉我为什么这不起作用?主if语句推出此项已经存在,但数据确实存在于数据库中。当我使用mysqli计数函数时,它总是返回1.

1 个答案:

答案 0 :(得分:0)

您的while循环分配中存在错误。改变这一行:

while($row[0] = mysqli_fetch_array($result)){

对此:

while($row = mysqli_fetch_array($result)){

调试时,通常可以考虑使用var_dump而不是echo。它可以更深入地了解实际情况。