我需要使用数据库信息填充表。但是,回显的HTML没有被插入到我希望它去的DOM中(在表内)。相反,它落在桌子上方。奇怪的是,它低于<h1>
标签,所以我知道在将内容加载到页面之前没有发生回声。
检查动态内容会发现我的所有标签都没有包含在内,并且所有数据都作为单个字符串转储。
我做错了什么,或者是否有不同的方法来确保表数据实际上在表中?
HTML
<h1>Contact Us Form Data</h1>
<table>
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Interest</th>
<th>Employment</th>
<th>City</th>
<th>State</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<?php echo display_records() ?>
</tbody>
</table>
PHP
function fetch_records() {
$dbhost = "localhost";
$dbuname = "user";
$dbpass = "pass";
$dbname = "db_dev";
$conn = mysqli_connect($dbhost, $dbuname, $dbpass, $dbname);
$query = "
SELECT * FROM `contact_us_form`
";
return mysqli_query($conn, $query);
}
function record_to_html( $record ) {
$html = "<td>" .
"<tr>" . $record->first_name . "</tr>" .
"<tr>" . $record->last_name . "</tr>" .
"<tr>" . $record->email . "</tr>" .
"<tr>" . $record->interest . "</tr>" .
"<tr>" . $record->employment . "</tr>" .
"<tr>" . $record->city . "</tr>" .
"<tr>" . $record->state . "</tr>" .
"<tr>" . $record->date . "</tr>" .
"</td>";
return $html;
}
function display_records() {
$result = fetch_records();
$html = "";
while ( $row = $result->fetch_object() ) {
$html .= record_to_html( $row );
}
return $html;
}
答案 0 :(得分:1)
问题是这段代码:
$html = "<td>" .
"<tr>" . $record->first_name . "</tr>" .
"<tr>" . $record->last_name . "</tr>" .
"<tr>" . $record->email . "</tr>" .
"<tr>" . $record->interest . "</tr>" .
"<tr>" . $record->employment . "</tr>" .
"<tr>" . $record->city . "</tr>" .
"<tr>" . $record->state . "</tr>" .
"<tr>" . $record->date . "</tr>" .
"</td>";
<tr>
和</tr>
应为<td>
和</td>
,反之亦然。