GET方法,用于每个具有自己页面的表行


GET method for each table row having its own page

假设我有一个名为"people"的数据库表,在"name"列下有"Bill"、"Karen"和"Ted"。

在我的php文件中,我只想使用一个模板,并使用这些行,为每个名称都有一个单独的页面(如"myfile.php?name=Bill")。据我所知,我必须使用GET,但我对此仍然很困惑和缺乏经验,那么我该如何在这里实现我的目标呢?

如果您想根据get变量中的名称创建页面,请使用此选项。这基本上会获取名称并将其带到数据库中,然后选择所有与该名称相关的字段,然后回显变量并将其放置在您想要的位置。

<?php
$name = $_GET["name"];//gets name value from your url that you supplied in your post
$con=mysqli_connect("127.0.0.1","db-username","db-pass","db-name");
mysqli_real_escape_string($con,$name);
$sql = mysqli_fetch_assoc(mysqli_query($con,"SELECT * FROM people WHERE names='$name'"));
$id = $sql['id'];//id would be a field in the people table
$age = $sql['age'];//age would be a field in the people table
echo '<h1>'.$name.'</h1>';
echo '<p>'. $id .'<br/>'. $ age .'</p>';
?>