【问题标题】:Populate multiple field values from php / sql response?从 php / sql 响应填充多个字段值?
【发布时间】:2018-03-23 18:39:24
【问题描述】:

我有以下 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

【问题讨论】:

  • 除非您使用 AJAX,否则您需要将 HTML 放入您的 PHP 脚本中。
  • 真的会有多个组织拥有相同的organizationId吗?为什么只需要一个while 循环来处理一行?
  • 您在将查询结果放入 HTML 时遇到什么问题?只需echo "&lt;h6&gt;Employees: {$rows2['employees']}&lt;/h6&gt;";

标签: php html sql mysqli


【解决方案1】:

假设您可以将 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 脚本发出请求,然后执行类似的操作。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-16
      相关资源
      最近更新 更多