用于一次填充多个列表框的SQL语句

时间:2015-10-21 01:59:17

标签: php sql loops mysqli

我正在尝试允许用户指定他们想要添加到客户购买的订单表单中的行数。这允许用户具有购买产品所需的行数而不是具有设置列表。我的功能正常,如果您键入3并提交,它将为您提供三行输入产品订单信息。 我遇到的问题是我填充一个列表框,其中包含要选择的用户的产品ID和名称。它填充第一行的列表框,但以下列表框仅获取“ - ”而不是$ row []值。好像它不再传递sql语句了,为什么会这样?

这是我的代码中我遇到功能问题的区域:

<?
            if (isset($_POST['update']))
            {
                //Execute this code if the update button is clicked.
                $num = $_POST['rows'];

                for ($i=0; $i<$num; $i++) { ?>

                    <tr>
                        <td class="inputCol2">
                            <select name="'product<?= $i ?>">
                                <option value="selectProduct">Select Product</option>
                                <!-- Populate listbox with Product ID and Product Name -->
                                <?
                                do { ?>

                                    <option value="<?= $row[0]; ?>"><?= $row[0] . " - " . $row[2]; ?></option>

                                <? } while($row = mysqli_fetch_array($result)) ?>

                            </select>
                        </td>
                        <td class="inputCol2"><input type="text" name="'quantity<?= $i ?>" ></td>
                        <td class="inputCol2">$<input type="text" name="'unit<?= $i ?>" value=""></td>
                        <td class="inputCol2">$<input type="text" name="'total<?= $i ?>" value="" ></td>
                    </tr>
                <? } ?>

这是我的全部代码:

<? 
        connectDB();

        $sql = "SELECT * FROM product";
        $sql2 = "SELECT DISTINCT emp_id, emp_fname, emp_lname FROM employee";
        $sql3 = "SELECT DISTINCT status_id FROM salesorder ORDER BY status_id asc";
        $sql4 = "SELECT * FROM salesorder ORDER BY order_id desc";

        $result = mysqli_query($db, $sql) or die("SQL error: " . mysqli_error());
        $result2 = mysqli_query($db, $sql2) or die("SQL error: " . mysqli_error());
        $result3 = mysqli_query($db, $sql3) or die("SQL error: " . mysqli_error());
        $result4 = mysqli_query($db, $sql4) or die("SQL error: " . mysqli_error());

        $row = mysqli_fetch_array($result);
        $row2 = mysqli_fetch_array($result2);
        $row3 = mysqli_fetch_array($result3);
        $row4 = mysqli_fetch_array($result4);

        ?>

    <div id="order-wrap">
        <form method="post" action="order.php">
            <table class="orderInfo"><br>
                <tr>
                    <th class="textCol">Product Rows:</th>
                    <td class="inputCol"><input type="text" name="rows"></td>
                    <td><input class="update" type="submit" name="update" value="Update"></td>
                    <td class="inputCol"></td>
                </tr>
            </table>
        </form><!-- Order Rows -->
        <form class="orderform" action ="order-report.php"  METHOD = "post">
            <h2>Order Form</h2>
            <h3>Piedmont Furnishings</h3>
            <img id="couch-img" src="couch.jpg" alt="couch">
            <table class="orderInfo">
                <tr>
                    <th class="textCol">Order Number:</th>
                    <td class="inputCol"><input type="text" name="orderNumber" value="<?= $row4[0] + 1; ?>" disabled></td>
                    <th class="textCol">Order Date:</th>
                    <td class="inputCol"><input type="text" name="orderDate" value="<?= date("Y-m-d") ?>"></td>
                </tr>
                <tr>
                    <th class="textCol">Customer:</th>
                    <td class="inputCol"><input type="text" name="customer"></td>
                    <td class="textCol"></td>
                    <td class="inputCol"></td>
                </tr>
                <tr>
                    <th class="textCol">Sales Agent:</th>
                    <td class="inputCol">
                        <select name="salesAgent">
                            <option value="selectAgent">Select One</option>
                            <!-- Populate listbox with Sales Agents ID -->
                            <?
                            do { ?>

                                <option value="<?= $row2[0]; ?>"><?= $row2[1] . " " . $row2[2]; ?></option>

                            <? } while($row2 = mysqli_fetch_array($result2)) ?>

                        </select>
                    </td>
                    <th class="textCol">Order Status:</th>
                    <td class="inputCol">
                        <select name="orderStatus">
                            <option value="selectStatus">Select One</option>
                            <!-- Populate listbox with Status ID -->
                            <?
                            do { ?>

                                <option value="<?= $row3[0]; ?>"><?= $row3[0] ?></option>

                            <? } while($row3 = mysqli_fetch_array($result3)) ?>

                        </select>
                    </td>
                </tr>
            </table>

        <!-- Where the product rows input show go ??? -->
        <table class="bottomTable">
            <tr>
                <th class="textCol">Product</th>
                <th class="textCol">Quantity</th>
                <th class="textCol">Unit Price</th>
                <th class="textCol">Total Price</th>
            </tr>
        <?
            if (isset($_POST['update']))
            {
                //Execute this code if the update button is clicked.
                $num = $_POST['rows'];

                for ($i=0; $i<$num; $i++) { ?>

                    <tr>
                        <td class="inputCol2">
                            <select name="'product<?= $i ?>">
                                <option value="selectProduct">Select Product</option>
                                <!-- Populate listbox with Product ID and Product Name -->
                                <?
                                do { ?>

                                    <option value="<?= $row[0]; ?>"><?= $row[0] . " - " . $row[2]; ?></option>

                                <? } while($row = mysqli_fetch_array($result)) ?>

                            </select>
                        </td>
                        <td class="inputCol2"><input type="text" name="'quantity<?= $i ?>" ></td>
                        <td class="inputCol2">$<input type="text" name="'unit<?= $i ?>" value=""></td>
                        <td class="inputCol2">$<input type="text" name="'total<?= $i ?>" value="" ></td>
                    </tr>
                <? } ?>
                    <tr>
                        <td class="textCol"></td>
                        <td class="textCol"></td>
                        <td class="textCol">Total Order:</td>
                        <td class="inputCol2">$<input type="text" name="totalfinal"></td>
                    </tr>
                    <input class="submit" type="submit" value="Submit" name="orderSubmit"/> 
                </table>
            </form>
            <? } else {?>
                    <tr>
                        <td class="textCol"></td>
                        <td class="textCol"></td>
                        <td class="textCol">Total Order:</td>
                        <td class="inputCol2">$<input type="text" name="totalfinal"></td>
                    </tr>
                    <input class="submit" type="submit" value="Submit" name="orderSubmit"/> 
                </table>
            </form>
            <? } ?>
       <?
            mysqli_free_result($result);
            mysqli_close($db);
        ?>
    </div>

1 个答案:

答案 0 :(得分:1)

您的代码的问题是第一次迭代while($row = mysqli_fetch_array($result)) $result的内部指针到达末尾...所以对于下一次迭代$i=1,{{1}中没有任何内容但是当你使用do-while循环时,循环必须运行至少一次,而$result为null,所以你只得到“ - ”。解决您需要稍微更改代码的问题。

删除此行$row[0] & $row[2]

并添加

$row = mysqli_fetch_array($result);

然后在for循环中改变:

$options = '<option value="selectProduct">Select Product</option>';

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

    $options .= '<option value="'.$row[0].'">'.$row[0].' - '.$row[1].'</option>';
}