PDO:如何使用数据库 table 中的行填充 html table?
PDO: how to populate html table with rows from database table?
这是我在数据库中的 table 信息。
id name age
1 Vale 23
2 Alex 13
3 Scott 15
4 Tino 18
我有 2 个 select 框,当我从第一个 "Vale" 和第二个 select 框选择 "Scoot" 时,我喜欢生成 table这个:
id name age
1 Vale 23
3 Scott 15
选择框。
<select name="name1">
<option value="Vale">Vale</option>
<option value="Tino">Tino</option>
</select>
<select name="name2">
<option value="Alex">Alex</option>
<option value="Scoot">Scoot</option>
</select>
.
.
.
我的page_to_process.php。我还需要什么,或者我的错误在哪里。
<?php
require('includes/config.php');\CONNECTION to Database
$sql = $dbh->prepare("SELECT * FROM info WHERE name = :name");
$sql->setFetchMode(PDO::FETCH_ASSOC);
$sql->execute([':name' => $name1]);
if($sql->rowCount() != 0) {
?>
<table border="0">
<tr COLSPAN=2 BGCOLOR="#6D8FFF">
<td>ID</td>
<td>Name</td>
<td>Age</td>
</tr>
<?php
while($row=$sql->fetch())
{
echo "<tr>".
"<td>".$row["id"]."</td>".
"<td>".$row["name"]."</td>".
"<td>".$row["age"]."</td>".
"</tr>";
}
}
else
{
echo "don't exist records for list on the table";
}
?>
</table>
试试这个方法:
<?php
require('includes/config.php');
function get_info($dbh, $name)
{
$sql = $dbh->prepare("SELECT * FROM info WHERE name = :name");
$sql->setFetchMode(PDO::FETCH_ASSOC);
$sql->execute([':name' => $name]);
if ($row = $sql->fetch()) {
return $row;
}
return false;
}
?>
<table border="0">
<tr COLSPAN=2 BGCOLOR="#6D8FFF">
<td>ID</td>
<td>Name</td>
<td>Age</td>
</tr>
<?php
if (isset($_POST['name1'])) {
if ($row = get_info($dbh, $_POST['name1'])) {
echo "<tr>" .
"<td>" . $row["id"] . "</td>" .
"<td>" . $row["name"] . "</td>" .
"<td>" . $row["age"] . "</td>" .
"</tr>";
} else {
echo "don't exist records for list on the table";
}
}
?>
</table>
这是我在数据库中的 table 信息。
id name age
1 Vale 23
2 Alex 13
3 Scott 15
4 Tino 18
我有 2 个 select 框,当我从第一个 "Vale" 和第二个 select 框选择 "Scoot" 时,我喜欢生成 table这个:
id name age
1 Vale 23
3 Scott 15
选择框。
<select name="name1">
<option value="Vale">Vale</option>
<option value="Tino">Tino</option>
</select>
<select name="name2">
<option value="Alex">Alex</option>
<option value="Scoot">Scoot</option>
</select>
.
.
.
我的page_to_process.php。我还需要什么,或者我的错误在哪里。
<?php
require('includes/config.php');\CONNECTION to Database
$sql = $dbh->prepare("SELECT * FROM info WHERE name = :name");
$sql->setFetchMode(PDO::FETCH_ASSOC);
$sql->execute([':name' => $name1]);
if($sql->rowCount() != 0) {
?>
<table border="0">
<tr COLSPAN=2 BGCOLOR="#6D8FFF">
<td>ID</td>
<td>Name</td>
<td>Age</td>
</tr>
<?php
while($row=$sql->fetch())
{
echo "<tr>".
"<td>".$row["id"]."</td>".
"<td>".$row["name"]."</td>".
"<td>".$row["age"]."</td>".
"</tr>";
}
}
else
{
echo "don't exist records for list on the table";
}
?>
</table>
试试这个方法:
<?php
require('includes/config.php');
function get_info($dbh, $name)
{
$sql = $dbh->prepare("SELECT * FROM info WHERE name = :name");
$sql->setFetchMode(PDO::FETCH_ASSOC);
$sql->execute([':name' => $name]);
if ($row = $sql->fetch()) {
return $row;
}
return false;
}
?>
<table border="0">
<tr COLSPAN=2 BGCOLOR="#6D8FFF">
<td>ID</td>
<td>Name</td>
<td>Age</td>
</tr>
<?php
if (isset($_POST['name1'])) {
if ($row = get_info($dbh, $_POST['name1'])) {
echo "<tr>" .
"<td>" . $row["id"] . "</td>" .
"<td>" . $row["name"] . "</td>" .
"<td>" . $row["age"] . "</td>" .
"</tr>";
} else {
echo "don't exist records for list on the table";
}
}
?>
</table>