在表的单个字段中显示数据

时间:2014-06-25 11:04:17

标签: javascript php jquery html mysql

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

代码:

<?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>

1 个答案:

答案 0 :(得分:0)

您可以轻松实现将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>