【问题标题】:display data in single field of a table在表格的单个字段中显示数据
【发布时间】:2014-08-15 21:55:10
【问题描述】:

我正在尝试在表格的单个字段中显示姓名和年龄,并且我希望将数据显示在   的位置。我尝试了下面的代码,但它不起作用。

代码:

<?php
    $con=mysqli_connect("localhost","root","","dva");
    // Check connection
    if (mysqli_connect_errno()) {
       echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $result = mysqli_query($con,"SELECT * FROM apform");

    while($row = mysqli_fetch_array($result)) {
        echo "My Mood: ";
        echo $row['mood'];
        echo "<br>";
    }
?>

<thead>
    <tr>    
        <th>ID</th>
        <th>Name</th>
        <th>Doctor</th>
        <th>Telephone</th>
        <th>Mobile</th>
        <th>Meeting Time</th>
        <th>Fee</th>
        <th>Date</th>
    </tr>
</thead>
<tbody>
    <tr>
        <td data-title="Name"><span> <?php echo $row['name']; ?> </span></td>
        <td data-title="Relationship"><span>&nbsp;</span></td>
        <td data-title="Address"><span>&nbsp;</span></td>
        <td data-title="Telephone"><span>&nbsp;</span></td>
        <td data-title="Mobile"><span>&nbsp;</span></td>
        <td data-title="Contact at night"><span>&nbsp;</span></td>
        <td data-title="Next of kin"><span>&nbsp;</span></td>
        <td data-title="Delete"><span>&nbsp;</span></td>
    </tr>

【问题讨论】:

  • while循环中写入tbody代码
  • 你的代码是正确的,什么不工作?
  • 请学习PHP基础知识。

标签: javascript php jquery html mysql


【解决方案1】:

您可以轻松实现将 while 循环放入表中并回显所有相关字段所需的操作。

<?php
    $con=mysqli_connect("localhost","root","","dva");
    // Check connection
    if (mysqli_connect_errno()) {
       echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $result = mysqli_query($con,"SELECT * FROM apform");

    while($row = mysqli_fetch_array($result)) {
        echo "My Mood: ";
        echo $row['mood'];
        echo "<br>";
    }
?>

<thead>
    <tr>    
        <th>ID</th>
        <th>Name</th>
        <th>Doctor</th>
        <th>Telephone</th>
        <th>Mobile</th>
        <th>Meeting Time</th>
        <th>Fee</th>
        <th>Date</th>
    </tr>
</thead>
<tbody>
    <?php while($row = mysqli_fetch_array($result)) { ?>
    <tr>
        <td data-title="Name"><span><?php echo $row['name']; ?></span></td>
        <td data-title="Relationship"><span><?php echo $row['relationship']; ?></span></td>
        <td data-title="Address"><span><?php echo $row['address']; ?></span></td>
        <td data-title="Telephone"><span><?php echo $row['telephone']; ?></span></td>
        <td data-title="Mobile"><span><?php echo $row['mobile']; ?></span></td>
        <td data-title="Contact at night"><span><?php echo $row['contact']; ?></span></td>
        <td data-title="Next of kin"><span><?php echo $row['nok']; ?></span></td>
        <td data-title="Delete"><span><a href="delete/<?php echo $row['id']; ?>">Delete</a></span></td>
    </tr>
    <?php } ?>
</tbody>

【讨论】:

  • OP 甚至都不关心检查答案。@Pooshonk,你怎么看?
猜你喜欢
  • 1970-01-01
  • 2011-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-11
  • 1970-01-01
  • 2014-05-27
  • 1970-01-01
相关资源
最近更新 更多