我想插入数据,其中从php代码返回到现有的html表(name =“userDetails”)。
根据从mysql数据库接收的结果,html表中的行数会有所不同。我怎么能这样做?
代码如下,
<?php
$connection = mysqli_connect('localhost', 'root', '', 'users');
$sql = "SELECT id, firstname,email FROM usertable";
$result = mysqli_query($connection, $sql);
$userDetailsArray = array();
if(mysqli_num_rows($result) > 0){
$index = 0;
while ($row = mysqli_fetch_assoc($result)) {
$userDataArray[$index] = $row;
$row = $userDataArray[$index];
$userID = $row['id'];
$firstName = $row['firstname'];
$email = $row['email'];
$index++;
}
}
?>
<html>
<head></head>
<body>
<table name = "userDetails">
<tr>
<th>User ID</th>
<th>First Name</th>
<th>email</th>
</tr>
</table>
</body>
<html/>
答案 0 :(得分:1)
看起来你正在寻找这样的东西:
<html>
<head></head>
<body>
<table name = "userDetails">
<tr>
<th>User ID</th>
<th>First Name</th>
<th>email</th>
</tr>
<?php
$connection = mysqli_connect('localhost', 'root', '', 'users');
$sql = "SELECT id, firstname,email FROM usertable";
$result = mysqli_query($connection, $sql);
if(mysqli_num_rows($result) > 0){
while ($row = mysqli_fetch_assoc($result)) {
echo '<tr>';
echo '<td>'. $row['id'] .'</td>';
echo '<td>'. $row['firstname'] .'</td>';
echo '<td>'. $row['email'] .'</td>';
echo '</tr>';
}
}
?>
</table>
</body>
<html/>
您只需在while
循环中生成HTML即可。不是最好的策略,但肯定会产生结果。
答案 1 :(得分:0)
使用相同的页面意味着它将起作用。试试这个
public sealed class CompareCustomAttribute : System.Web.Mvc.CompareAttribute
{
public CompareCustomAttribute(string otherProperty)
: base(otherProperty)
{
}
public string ResourceKey { get; set; }
public string ClassKey { get; set; }
public override string FormatErrorMessage(string name)
{
return Convert.ToString(HttpContext.GetGlobalResourceObject(this.ClassKey, this.ResourceKey));
}
}
答案 2 :(得分:0)
您的代码中这些行的要求是什么? $ userDetailsArray = array(); $ userDataArray [$ index] = $ row; $ row = $ userDataArray [$ index]; ??甚至是$ index变量的要求是什么?
我正在为您添加更改程序。
<?php
$connection = mysqli_connect('localhost', 'root', '', 'users');
?>
<html>
<head></head>
<body>
<table name = "userDetails">
<tr>
<th>User ID</th>
<th>First Name</th>
<th>email</th>
</tr>
<?php
$sql = "SELECT id, firstname,email FROM usertable";
$result = mysqli_query($connection, $sql);
if(mysqli_num_rows($result) > 0){
while ($row = mysqli_fetch_assoc($result)) {
?>
<tr>
<td>$row['id'];</td>
<td>$row['firstname'];</td>
<td>$row['email'];</td>
</tr>
<?php
}
}
else
{
?>
<tr>
<td colspan=3>Data is not available</td>
</tr>
<?php
}
?>
</table>
</body>
<html/>