PHP-如何在if-isset条件中嵌套while循环?

PHP-如何在if-isset条件中嵌套while循环?,php,forms,mysqli,Php,Forms,Mysqli,我有以下代码的table元素: <?php if(isset($_POST["submit"])){ if (strlen($cIdMsg = 0) && strlen($cFirstNameMsg = 0) && strlen($cLastNameMsg = 0) && strlen($pCodeMsg = 0)) { require_once("conn.php"); $sql2 = "SELECT

我有以下代码的table元素:

<?php
if(isset($_POST["submit"])){
    if (strlen($cIdMsg = 0) && strlen($cFirstNameMsg = 0) && strlen($cLastNameMsg = 0) && strlen($pCodeMsg = 0)) {
        require_once("conn.php");
        $sql2 = "SELECT * FROM customer;";
        $results = mysqli_query($conn, $sql2)
        or die ('Problem with query' . mysqli_error($conn));
        echo "no errors found";
    }
}
?>

<table>
<tr>
    <th>Customer ID</th>
    <th>FIrst Name</th>
    <th>Last Name </th>
</tr>

<?php
    while ($row = mysqli_fetch_array($results)) { ?>
    <tr>
        <td><?php echo $row["customerID"]?></td>
        <td><?php echo $row["firstName"]?></td>
        <td><?php echo $row["lastName"]?></td>
    </tr>
<?php } ?>
</table>

客户ID
名字
姓

在这个表的上面,我有一段php代码,它在if-isset条件中进行sql查询,这样它只能在表单上按submit之后加载。我想对桌子做同样的事。也就是说,只有在按下submit后才能加载它。因为在页面加载时,它试图在一个不存在的$result上执行mysqli_fetch_数组,但将整个表包装在其中:

<?php if (isset($result)) { ?>
<table>
<tr>
    <th>Customer ID</th>
    <th>FIrst Name</th>
    <th>Last Name </th>
</tr>

<?php
    while ($row = mysqli_fetch_array($results)) { ?>
    <tr>
        <td><?php echo $row["customerID"]?></td>
        <td><?php echo $row["firstName"]?></td>
        <td><?php echo $row["lastName"]?></td>
    </tr>
<?php } ?>
</table>
<?php } ?>

客户ID
名字
姓

根据你所说的,我使用了
isset($result)
。您可以通过检查
count($\u POST)
或类似内容(检查
isset($\u POST[“submit”])
)来检查POST值。如果要获取AJAX响应,最好使用不同的单独文件。

将整个表包装在以下文件中:

<?php if (isset($result)) { ?>
<table>
<tr>
    <th>Customer ID</th>
    <th>FIrst Name</th>
    <th>Last Name </th>
</tr>

<?php
    while ($row = mysqli_fetch_array($results)) { ?>
    <tr>
        <td><?php echo $row["customerID"]?></td>
        <td><?php echo $row["firstName"]?></td>
        <td><?php echo $row["lastName"]?></td>
    </tr>
<?php } ?>
</table>
<?php } ?>
<?php if(mysqli_num_rows($result)!=0) { ?>
<table>
<tr>
    <th>Customer ID</th>
    <th>FIrst Name</th>
    <th>Last Name </th>
</tr>

<?php
    while ($row = mysqli_fetch_array($results)) { ?>
    <tr>
        <td><?php echo $row["customerID"]?></td>
        <td><?php echo $row["firstName"]?></td>
        <td><?php echo $row["lastName"]?></td>
    </tr>
<?php } ?>
</table>
<?php } ?>

客户ID
名字
姓
根据你所说的,我使用了
isset($result)
。您可以通过检查
count($\u POST)
或类似内容(检查
isset($\u POST[“submit”])
)来检查POST值。如果要获取AJAX响应,最好使用不同的单独文件。


<?php if(mysqli_num_rows($result)!=0) { ?>
<table>
<tr>
    <th>Customer ID</th>
    <th>FIrst Name</th>
    <th>Last Name </th>
</tr>

<?php
    while ($row = mysqli_fetch_array($results)) { ?>
    <tr>
        <td><?php echo $row["customerID"]?></td>
        <td><?php echo $row["firstName"]?></td>
        <td><?php echo $row["lastName"]?></td>
    </tr>
<?php } ?>
</table>
<?php } ?>
客户ID 名字 姓

客户ID
名字
姓

发布您的完整代码!!您想使用Ajax请求在后台获取数据,并仅在得到查询结果时插入行吗?发布完整代码!!您想使用Ajax请求在后台获取数据,并仅在得到查询结果时插入行吗?谢谢,这非常有效。请问你为什么不推荐使用,isset($_POST[“submit”]),因为这是我们被告知要使用的postback@nanjero05一些愚蠢的浏览器不会发送
Submit
值,许多开发人员也会忘记命名
Submit
按钮。以防万一<代码>:)谢谢你这工作做得很好。请问你为什么不推荐使用,isset($_POST[“submit”]),因为这是我们被告知要使用的postback@nanjero05一些愚蠢的浏览器不会发送
Submit
值,许多开发人员也会忘记命名
Submit
按钮。以防万一<代码>:)