【问题标题】:How to create a link through to database entry如何通过数据库条目创建链接
【发布时间】:2015-07-25 22:51:04
【问题描述】:

我有一个页面显示来自我的表单的响应(在网页中)。目前,所有回复都在一页上,一个接一个。我希望该页面显示所有提交日期的列表 (date_submitted) 和 ID 数字,当单击它时,它会将我们带到具有该特定响应的页面(即完整响应)。如何创建将其带到此记录的链接?

这是该页面的代码:

<?php error_reporting(E_ALL); ?>

<head>
<link rel="stylesheet" type="text/css" href="surveystyle.css" media="all" />
</head>
<body>
<div id="container2">
    <div class="logo-header"><img src="images/logo.jpg" alt="" width="205" height="119" /></div>
    <h2> Guest Questionnaire Responses </h2>
<?php

    try {
            $handler = new PDO('mysql:host=localhost;dbname=***', '***', '***');
            $handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch(PDOException $e) {
        echo $e->getMessage();
        die();
    }

class guestquestionnaireEntry
    {
        public $id, $date_submitted, $choice, $expectations, $res, $res_information, $res_staff, $further_comments1, 
        $entry;

        public function __construct()
            {
                $this->entry = "<a href=\"?ID={$this->id}\">ID</a>
    <tr style='text-align: left; font:arial;'><td><h3>Date Submitted: {$this->date_submitted}</h3></td></tr>

            <BR>
            <table border='1' align='center'>
                <tr style='background: #566890; font-size: 8pt; font-weight: bold; color: #fff;'>
                    <td colspan='3'>Prior to Arrival</td>
                </tr>
                <tr style='font-size: 8pt;'>
                    <td width='60%'>What made you choose us for your recent stay? </td>
                    <td width='40%' colspan='2'>{$this->choice}</td>
                </tr>
                <tr style='font-size: 8pt;'>
                    <td>Did our hotel meet your expectations as advertised? If no, please state why: </td>
                    <td width='40%' colspan='2'>{$this->expectations}</td>
                </tr>
                <tr style='background: #566890; font-size: 8pt; font-weight: bold; color: #fff;'>
                    <td colspan='3'>Making your Reservation</td>
                </tr>
                <BR>
                <tr style='font-size: 8pt;'>
                    <td>Ease of making your reservation: </td>
                    <td width='40%'>$img</td>
                    <td width='5%'>{$this->res}</td>
                </tr>
                <BR>
                <tr style='font-size: 8pt;'>
                    <td>Hotel information offered: </td>
                    <td width='40%'>$img2</td>
                    <td width='5%'>{$this->res_information}</td>
                </tr>
                <BR>
                <tr style='font-size: 8pt;'>
                    <td>Warmth and friendliness of staff: </td>
                    <td width='40%'>$img3</td>
                    <td width='5%'>{$this->res_staff}</td>
                </tr>
                <BR>
                <tr style='font-size: 8pt;'>
                    <td colspan='3'>Further Comments: </BR></BR>{$this->further_comments1}</td>
                </tr>
                <BR>
            </table>";
            }
    }


// Checks if the submitted is a number. If so, isolates the ID and adds "where" clause
$id      =   (!empty($_GET['ID']) && is_numeric($_GET['ID']))? " where ID = '".$_GET['ID']."'" : "";
// Add the $id to the end of the string
// A single call would be SELECT * FROM guestquestionnaire where ID = '1'
$query   =   $handler->query("SELECT * FROM guestquestionnaire{$id}");
$query->setFetchMode(PDO::FETCH_CLASS, 'guestquestionnaireEntry');

    while($r = $query->fetch()) {
        echo $r->entry, '<br>';
    }
?>
    </div>
</body>

【问题讨论】:

    标签: php database forms


    【解决方案1】:

    你需要在你的 sql 中做一个where

    HTML 按钮:

    <!-- Use whatever here to click on that triggers the ID reload -->
    <a href="?ID=<?php echo $this->id; ?>">ID</a>
    

    SQL 查询:

    // Checks if the submitted is a number. If so, isolates the ID and adds "where" clause
    $id      =   (!empty($_GET['ID']) && is_numeric($_GET['ID']))? " where ID = '".$_GET['ID']."'" : "";
    // Add the $id to the end of the string
    // A single call would be SELECT * FROM guestquestionnaire where ID = '1'
    $query   =   $handler->query("SELECT * FROM guestquestionnaire{$id}");
    $query->setFetchMode(PDO::FETCH_CLASS, 'guestquestionnaireEntry');
    

    编辑:

    // Should be something like this
    public function __construct()
        {
            $this->entry = "
            <a href=\"?ID={$this->id}\">ID</a>...etc.
    

    【讨论】:

    • 谢谢。这是否与我上面列出的代码在同一页面上?有点困惑把它放在哪里。
    • 是的,sql 查询添加行并替换您的行。 HTML 按钮将位于construct() 中的某个位置,但您想对其进行布局。虽然它可能更像&lt;a href="?ID=&lt;?php echo $this-&gt;id; ?&gt;"&gt;ID&lt;/a&gt;
    • 谢谢,但我似乎无法正常工作。您更新的最后一行(您的评论中也提到的那一行)给了我一个空白页,就好像它有错误一样。
    • 嗯,可能有错误。将&lt;?php error_reporting(E_ALL); ?&gt; 放在页面的最顶部。这会告诉你/我错误是什么。
    • 仍然给我一个空白页。如果我删除该 行并保持它应该加载页面。
    猜你喜欢
    • 1970-01-01
    • 2021-10-03
    • 1970-01-01
    • 1970-01-01
    • 2017-01-15
    • 1970-01-01
    • 2014-06-23
    • 2016-05-03
    • 1970-01-01
    相关资源
    最近更新 更多