dongyuxiao6295 2015-10-08 14:11
浏览 46
已采纳

如何在动态网页中使用名称而不是id

I just started learning php and mysql and i might already be way ahead of myself. The thing i would like to create is a webpage where ppl can sign up for an event, so far so good, the form to submit their first name, last name, age and email adress is working and its actually sending te information to the database.

Next thing i want to create is a page where i can display all the database records submitted (except for the email adress). This is also working, but I wanted to play around with dynamic urls.

When i visit my page http://www.example.com/ppl.php?id=1 i get the information of the first database record displayed but i also wanted to see if i could get this to work with names instead of ids so i tried to edit my code and use http://www.example.com/ppl.php?name=john this does only return an error and however there are a few people called john in the database no records are displayed.

So i would like to know if what i want is actually possible and how do i get this to work with my current code.

<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "event";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
    $id = $_GET['id'];
    $firstname = 'firstname';
    $lastname = 'lastname';
    $age = 'age';

$sql = "SELECT * FROM people WHERE id = $id";

$result = $conn->query($sql);

    echo "<table id='display' width='600' align='center'>";
    echo"<tr><td>&nbsp;Firstname</td> <td>&nbsp;Lastname</td> <td>&nbsp;Age</td>";

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {

        echo"<tr><td width='33%'>&nbsp;$row[$firstname]</td> <td width='33%'>&nbsp;$row[$lastname]</td> <td width='33%'>&nbsp;$row[$age] cm</td></tr>";


    }
} else {
    echo "0 results";

}
$conn->close();
?>
  • 写回答

2条回答 默认 最新

  • dongying6179 2015-10-08 14:23
    关注

    Try to change the query:

    $sql = "SELECT * FROM people WHERE id = $id";
    

    To:

    $name = $_GET['name'];
    $sql = "SELECT * FROM people WHERE name LIKE '%$name%'";
    

    Then echo for each one John you find.

    Also consider using CSS like this.

    <style>
        #display {
            width: 600px;
        }
        #display td {
            width: 33%;
        }
    </style>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本