从 php / sql 响应中填充多个字段值



我有以下HTML标头(下面的h6标签(,我想从下面的PHP/SQL查询结果中填充它们。我已经分别完成了这两个组件,但需要将我的 PHP/SQL 查询中的值获取到下面的 h6 标签中。我将如何实现这一点?

.HTML:

<div class="card-body">
<h5 class="card-title m-b-40" >Company Name</h5>
    <h6 style='font-weight: bold;'>Company Overview</h6>
    <h6>Annual Revenue: 2,000,000,000</h6>
    <h6>Employees: 150,000</h6>
    <h6>Industry: xxx</h6>
    <h6>Inherent Risk Industry: xxx</h6>
</div>

.PHP:

<?php
    $servername = "xxx";
    $username = "xxx";
    $password = "xxx";
    $dbname = "xxx";
    $id = intval($_GET['id']); //casting to int type!
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }else{
        echo '<script>console.log("Connection successful!")</script>';
    }
    $SELECT2 = mysqli_query($conn,"SELECT * FROM `organization` WHERE organizationId=$id");
    if($SELECT2 != false)
    {
        while($rows2 = mysqli_fetch_array($SELECT2)){
    }
    }else{
        echo "
            <tr>
            <td colspan='3'>Something went wrong with the query</td>
            </tr>
        ";
    }
    <?php

假设您可以将PHP和HTML放在同一页面上,它看起来像这样:

<?php
$servername = "xxx";
$username = "xxx";
$password = "xxx";
$dbname = "xxx";
$id = intval($_GET['id']);
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}else{
    echo '<script>console.log("Connection successful!")</script>';
}
$SELECT2 = mysqli_query($conn,"SELECT * FROM `organization` WHERE organizationId=$id");
if($SELECT2 != false)
{
    $rows2 = $SELECT2->fetch_assoc();
}
}else{
    echo "
        <tr>
        <td colspan='3'>Something went wrong with the query</td>
        </tr>
    ";
}
?>
<div class="card-body">
  <h5 class="card-title m-b-40" ><?php echo $rows2['name']; ?></h5>
  <h6 style='font-weight: bold;'><?php echo $rows2['overview']; ?></h6>
  <h6>Annual Revenue: <?php echo $rows2['annual_revenue']; ?></h6>
  <h6>Employees: <?php echo $rows2['employees']; ?></h6>
  <h6>Industry: <?php echo $rows2['industry']; ?></h6>
  <h6>Inherent Risk Industry: <?php echo $rows2['risk']; ?></h6>
</div>

请注意,愚蠢的 while 循环已经消失了,因为该查询应该只有一个结果,我们使用 fetch_assoc(( 来获取一个关联数组而不是一个编号数组,以便您可以使用列名并防止它成为人类不可读的混乱,我们只是在 html 中回显您需要的数据。 当然,我正在对您的列名做出假设。

如果这些不能在同一页面上,您需要先从 HTML 页面向 PHP 脚本发出请求,然后执行类似操作。

这个 HTML 与脚本是不同的页面吗?因为如果是这种情况,您不应该使用 GET 变量从脚本发送此日期以在第二页中接收吗?

将数组上的每个字段分配给一个变量,并通过 http://example.com/yourpage.php?value1=$var1 等发送它们

最新更新