首页 > 解决方案 > 未发布使用 php foreach 生成的第一行的值

问题描述

需要以下代码的帮助。仅在为第一个生成的行点击提交后,“customer_id”的值不会发布。第二项及以下工作按预期进行。

其他页面使用相同的格式进行删除,但不要像这个那样重定向到另一个页面。删除没有问题。

<html>
    <!-- the body section -->
    <body>
        <main>
            <section>
                <!-- display a table of customers -->
                <h2>Results</h2>
                <table>
                    <tr>
                        <th>Name</th>
                        <th>Email Address</th>
                        <th>City</th>
                        <th>&nbsp;</th>
                    </tr>
                    <?php foreach($customers as $cus) : ?>
                    <tr>
                        <td><?php echo $cus['firstName'].' '.$cus['lastName']; ?></td>
                        <td><?php echo $cus['email']; ?></td>
                        <td class="right"><?php echo $cus['city']; ?></td>
                        <td><form action="view_and_update.php" method="post" id="search">
                            <input type="text" name="customer_id" value="<?php echo $cus['customerID']; ?>">
                            <input name="submit" type="submit" value="Select">
                            <?php include "redirect.php";?>
                            </form></td>
                    </tr>
                    <?php endforeach; ?>
                </table>
            </section>
        </main>
    </body>
</html>

redirect.php 看起来像这样:

<?php

    if(isset($_POST["submit"])){
        //  To redirect form on a particular page
        header("Location: view_and_update.php");
    }

?>

非常感谢任何帮助。这是一个作业,我们使用 XAMPP/myphpadmin 指定的语言。在任何其他情况下检索、编辑、添加或删除数据都没有问题。这是唯一的问题页面。

标签: phphtml

解决方案


通过使用以下示例代码,我得到了 view_and_update.php 的正确帖子

它必须是别的东西,而不是代码。

<?php $customers = [ 
['firstName' => 'Alice', 'lastName' => 'Al', 'email' => 'alison@domain.com', 'city' => 'MyCity', 'customerID' => 123],
['firstName' => 'Bob', 'lastName' => 'Bo', 'email' => 'bob@domain.com', 'city' => 'MyCity', 'customerID' => 124]
]; ?>
<html>
<!-- the body section -->
<body>
    <main>
        <section>
            <!-- display a table of customers -->
            <h2>Results</h2>
            <table>
                <tr>
                    <th>Name</th>
                    <th>Email Address</th>
                    <th>City</th>
                    <th>&nbsp;</th>
                </tr>
                <?php foreach($customers as $cus) : ?>
                <tr>
                    <td><?php echo $cus['firstName'].' '.$cus['lastName']; ?></td>
                    <td><?php echo $cus['email']; ?></td>
                    <td class="right"><?php echo $cus['city']; ?></td>
                    <td><form action="view_and_update.php" method="post" id="search">
                        <input type="text" name="customer_id" value="<?php echo $cus['customerID']; ?>">
                        <input name="submit" type="submit" value="Select">
                        </form></td>
                </tr>
                <?php endforeach; ?>
            </table>
        </section>
    </main>
</body>
</html>

推荐阅读