使用循环选择数据并在div中显示MySql php

时间:2017-07-11 05:57:13

标签: php html mysql sql database

我在MySql表中有一个包含许多行记录的表(existingbankproducts表):

enter image description here

我用来选择的代码来自数据库:

$stmt2 = $DB_con->prepare("SELECT * FROM applicantpersonaldetails apd "
        . "INNER JOIN existingbankproducts ext ON apd.ApplicantID = ext.ApplicantID "
        . "WHERE apd.AccountID ='{$accountId}' AND apd.applicantType ='main';");

$stmt2->execute();

if ($stmt2->rowCount() > 0) {
    while ($row = $stmt2->fetch(PDO::FETCH_ASSOC)) {



        ?>
        <?php
    }
} else {
    ?>
    <div class="">
        <div class="alert alert-warning">
            <span class="glyphicon glyphicon-info-sign"></span> &nbsp; No Data Found ...
        </div>
    </div>
    <?php
}

我想选择它们并插入我的HTML表格,代码如下:

<table>
<tr>
<th>Financial Institution</th>
<th>Product Type</th>
<th>Balance</th>
<th>Monthly Commitment</th>


</tr>
<tr>
<td><input type = "text" name = "finanIns1" id = "finanIns1" value = ""readonly></td>
<td>
<input list = "proTypeList" name = "proType1" id = "proType1"readonly >

</td>
<td id = "balance"><input type = "number" name = "balance1" id = "balance1" value = "" min = "0"readonly></td>
<td id = "MonthyComm"><input type = "number" name = "monthlyComm1" id = "monthlyComm1" value = "" min = "0"readonly></td>

</tr>

<tr>
<td><input type = "text" name = "finanIns2" id = "finanIns2" value = ""readonly></td>
<td>
<input list = "proTypeList" name = "proType2" id = "proType2" readonly>

</td>
<td id = "balance"><input type = "number" name = "balance2" id = "balance2" value = "" min = "0"readonly></td>
<td id = "MonthyComm"><input type = "number" name = "monthlyComm2" id = "monthlyComm2" value = "" min = "0"readonly></td>

</tr>

</table>

实际上,还有更多行,这是一个例子。

另外,我以value="<?php echo $row['Financialinstitution'] " ?>为例,然而,所有记录都出来了。

有没有办法按顺序显示HTML表格的结果。

2 个答案:

答案 0 :(得分:1)

1st:您需要像这样循环记录集

第二名:您的输入值应该填充右列,如此

<input type = "text" name = "finanIns1" id = "finanIns1" value="<?php echo $row['Financialinstitution']; ?>" readonly>

注意:您需要回显每列desired td input。我只回显了一列

第3名:使用预备好的声明是好的。你还需要使用bindparam。像这样

$stmt2 = $DB_con->prepare("SELECT * FROM applicantpersonaldetails apd "
        . "INNER JOIN existingbankproducts ext ON apd.ApplicantID = ext.ApplicantID "
        . "WHERE apd.AccountID =:accountId AND apd.applicantType ='main';");

$stmt2->bindParam(':accountId', $accountId, PDO::PARAM_INT); 
//if account id data type is varchar change the last parameter to `PDO::PARAM_str`
$stmt2->execute();

PHP:

if ($stmt2->rowCount() > 0) {

    ?>
    <table>
    <tr>
    <th>Financial Institution</th>
    <th>Product Type</th>
    <th>Balance</th>
    <th>Monthly Commitment</th> 
    </tr>
    <?php


    while ($row = $stmt2->fetch(PDO::FETCH_ASSOC)) {
    ?>
     <tr>
        <td><input type = "text" name = "finanIns1" id = "finanIns1" value="<?php echo $row['Financialinstitution']; ?>" readonly></td>
        // like above td you need to echo all your data for following td 
        <td>
        <input list = "proTypeList" name = "proType1" id = "proType1" readonly >

        </td>
        <td id = "balance"><input type = "number" name = "balance1" id = "balance1" value = "" min = "0"readonly></td>
        <td id = "MonthyComm"><input type = "number" name = "monthlyComm1" id = "monthlyComm1" value = "" min = "0"readonly></td>

    </tr>

        <?php
    }
} else {
    ?>
    <div class="">
        <div class="alert alert-warning">
            <span class="glyphicon glyphicon-info-sign"></span> &nbsp; No Data Found ...
        </div>
    </div>
    <?php
}

答案 1 :(得分:0)

无论我从你的问题中理解什么,你都可以看到如下:

if ($stmt2->rowCount() > 0) {
    while ($row = $stmt2->fetch(PDO::FETCH_ASSOC)) {
?>
<tr>
<td><input type = "text" name = "finanIns1" id = "finanIns1" value = "<?php $row['columnName']?>" readonly></td>
<td>
<input list = "proTypeList" name = "proType1" id = "proType1"readonly >

</td>
<td id = "balance"><input type = "number" name = "balance1" id = "balance1" value = "<?php $row['columnName']?>" min = "0"readonly></td>
<td id = "MonthyComm"><input type = "number" name = "monthlyComm1" id = "monthlyComm1" value = "" min = "0"readonly></td>

</tr>

<tr>
<td><input type = "text" name = "finanIns2" id = "finanIns2" value = "<?php $row['columnName']?>" readonly></td>
<td>
<input list = "proTypeList" name = "proType2" id = "proType2" readonly>

</td>
<td id = "balance"><input type = "number" name = "balance2" id = "balance2" value = "<?php $row['columnName']?>" min = "0"readonly></td>
<td id = "MonthyComm"><input type = "number" name = "monthlyComm2" id = "monthlyComm2" value = "<?php $row['columnName']?>" min = "0"readonly></td>

</tr>
<?php


        ?>
        <?php
    }