【问题标题】:Show URL herf link with the use of id in PHP MySQL在 PHP MySQL 中使用 id 显示 URL href 链接
【发布时间】:2017-02-10 02:00:35
【问题描述】:

我有一个摘要页面,该页面将显示所有名为 readmsg.php 的客户名称,并且每个客户在 MySQL 数据库中都有一个唯一的 msgid。另一个页面可以查看它们的详细信息,称为 readmsgdetail.php。

但是,我在使用 msgid 仅显示一位客户的详细信息时遇到了问题。现在,我的 readmsgdetail.php 可以显示所有客户的所有详细信息。

我的 readmsg.php 的代码:

       <div class="row">
        <?php
        $stmt = $DB_con->prepare('SELECT firstname,lastname,phone,enquiry FROM user_message ORDER BY msgid DESC');
        $stmt->execute();

        if ($stmt->rowCount() > 0) {
            while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                extract($row);
                ?>
                <div class="col-xs-12">
                    <p><a class="page-header" href="readmsgdetail.php?msgdetail_id=<?php echo $row['msgid']; ?>"><?php echo $lastname .  $firstname; ?></a></p>
                </div>       
                <?php
            }
        } else {
            ?>
            <div class="col-xs-12">
                <div class="alert alert-warning">
                    <span class="glyphicon glyphicon-info-sign"></span> &nbsp; No Data Found ...
                </div>
            </div>
            <?php
        }
        ?>
    </div>

我的 readmsgdetail.php 的代码:

    <?php
require_once 'dbconfig.php';
//////------------------------------------------------
session_start();
if (empty($_SESSION) || empty($_SESSION['login_id'])) {
    header('location:login.php');
};
echo"Welcome!! " . $_SESSION['login_id'] . "   ";
echo '<a href="logout.php">log out</a>';
//-----------------------------------------------------

if (isset($_REQUEST['msgdetail_id']) && !empty($_REQUEST['msgdetail_id'])) {
    $id = $_REQUEST['msgdetail_id'];
} else {

    header("Location: readmsg.php");
}
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" />
        <title></title>
        <link href="../bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>


        <link href="../bootstrap/css/bootstrap-theme.min.css" rel="stylesheet" type="text/css"/>
    </head>
    <body>




        <div class="container">


            <div class="page-header">
                <h1 class="h2">User review <a class="btn btn-default" href="readmsg.php"> all reviews </a></h1>
            </div>



            <?php
            $stmt = $DB_con->prepare('SELECT firstname,lastname,phone,enquiry FROM user_message ORDER BY msgid DESC');
            $stmt->execute();

            if ($stmt->rowCount() > 0) {
                while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                    extract($row);
                    ?>
                    <form method="post" enctype="multipart/form-data" class="form-horizontal">




                        <table class="table table-bordered table-responsive">

                            <tr>
                                <td><label class="control-label">Name.</label></td>
                                <td><input class="form-control" type="text" name="name" value="<?php echo $lastname . $firstname; ?>" required /></td>
                            </tr>



                            <tr>
                                <td><label class="control-label">Phone </label></td>
                                <td><input class="form-control" type="text" name="phone" value="<?php echo $phone; ?>" required /></td>
                            </tr>

                            <tr>
                                <td><label class="control-label">Feedback / Enquiry.</label></td>

                                <td><input class="form-control" type="text" name="comment" value="<?php echo $enquiry; ?>" required /></td>

                            </tr>

                            <tr>
                                <td colspan="2">
                                    <a class="btn btn-default" href="readmsg.php"> <span class="glyphicon glyphicon-backward"></span> back </a>

                                </td>
                            </tr>
                        </table>
                    </form>     
                    <?php
                }
            } else {
                ?>
                <div class="col-xs-12">
                    <div class="alert alert-warning">
                        <span class="glyphicon glyphicon-info-sign"></span> &nbsp; No Data Found ...
                    </div>
                </div>
                <?php
            }
            ?>

        </div>
    </body>
</html>

MySQL的结构:

网页的当前结果:

在我将选择语句更改为

$stmt = $DB_con->prepare("SELECT firstname,lastname,phone,enquiry FROM user_message where msgid = 'msgdetail_id'");

它显示“未找到数据”。

【问题讨论】:

  • WHERE 子句添加到SELECT
  • 在我更改为 "$stmt = $DB_con->prepare("SELECT firstname,lastname,phone,enquiry FROM user_message where msgid = 'msgdetail_id'");"它显示没有找到数据。
  • where msgid = 'msgdetail_id' 那是因为 msgidint 并且你试图传递一个字符串。
  • 这里 where msgid = '$id' 应该可以修复您的代码。
  • ...我打赌有人会看到我的评论并发布答案;注意它。好的,我离开了这个。如果解决了,请告诉我。如果没有,请继续调试。我比平时呆的时间更长,没有任何回应。

标签: php mysql url


【解决方案1】:

readmsg.php 中的代码应改为:

$stmt = $DB_con->prepare('SELECT msgid, firstname,lastname,phone,enquiry FROM user_message ORDER BY msgid DESC');

readmsgdetail.php 中的代码应改为:

$stmt = $DB_con->prepare("SELECT firstname,lastname,phone,enquiry FROM user_message where msgid=$id");

现在正在醒来!感谢 cmets 的帮助!

【讨论】:

    猜你喜欢
    • 2013-11-21
    • 2011-10-06
    • 1970-01-01
    • 2023-03-18
    • 2015-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多