【问题标题】:how to create link depending on user input如何根据用户输入创建链接
【发布时间】:2014-01-26 00:04:15
【问题描述】:

这是用户发布帖子的代码。

if(islet($_POST['submit'])) {
    $name = $_POST['name'];
    $keywords = $_POST['keywords'];
    $description = $_POST['description'];
    if(islet($_GET['user_id'])) {
        $_SESSION['user_id'] = $_GET['user_id'];
    } //Then I just have a inset into statement for my database with these 4 variables.
}

我有一个由用户创建帖子的网络表单。我现在想让用户能够返回专用于该帖子的页面,以便他们进行编辑、添加等。

【问题讨论】:

  • islet 是什么?是你定义的函数吗?
  • 你想开个论坛吗? @SharanyaDutta,我认为他的意思是 isset()
  • 让您的代码更好地供我们的助手阅读。选择代码文本后使用快捷键 ctrl+K ;)
  • 对不起。我做这种很快。小岛是小岛的意思。对不起。它有点像论坛,但更像是 fivver。我不知道您是否听说过它,但它允许用户创建他们想做的工作,并且他们可以随时返回并编辑它。我正在尝试创建一个类似于这种类型的表单。
  • “小岛的意思是小岛。”这不是不言自明吗? StackOverflow 允许您编辑您的帖子,以便您可以将islet 更改为isset,如果您是这个意思。但是,您可以随意创建一个函数 islet 来处理(如果您想对函数的名称公正的话)小岛。

标签: php mysql forms web hyperlink


【解决方案1】:

这是高级解决方案:

  1. 您需要创建一个需要 post_id 作为查询字符串参数的页面。该页面的访问方式如下:http://yoursite.com/show-post.php?post_id=136
  2. 在 PHP 中,检索该 post_id:$_GET['post_id']
  3. 从该帖子 ID 中,从数据库中提取与该 ID 关联的信息。类似SELECT * FROM post WHERE post_id = $_GET['post_id']
  4. 然后使用 SQL 查询返回的信息显示帖子。
  5. 如果您想显示当前用户的帖子列表,请创建另一个页面http://yoursite.com/my-posts.php
  6. 在该页面中,根据当前用户 ID 编写 SQL 查询:SELECT * FROM post WHERE user_id = $_SESSION['user_id'](假设您在 post 表中有一个 user_id,并且该用户已经过身份验证并且他的 ID 存储在会话中)。
  7. 这将为您获取帖子列表,并循环访问它们以获取其详细信息。

请注意,您应该对查询字符串中传递的参数进行转义。方法很多,这里就不赘述了。

编辑:

这是生成链接的方式:

<?php foreach ($mysql_result as $row) { ?>
    <a href="http://yoursite.com/edit-post.php?post_id=<?= $row['post_id'] ?>">link</a>
<?php } ?>

然后,在 edit-post.php 中,您可以通过 $postId = $_GET['post_id']; 获取 post_id,然后在查询中使用它来获取有关该特定帖子的所有信息。

【讨论】:

  • 当你在 url 中使用 post_id=136 时,我应该完全按照那个还是应该怎么做?
  • 我已经做到了这一点。我只想知道他们是否可以为我的帖子页面上返回的每个帖子添加链接
  • 这只是一个示例,您可以根据需要构建您的 url。您是否在询问如何生成链接?或者关于如何构建显示一篇文章信息的页面?或者关于如何“编辑”帖子?
  • 我只需要知道如何为每个帖子 ID 生成唯一链接。
  • 生成是处理页面的脚本和“生成” URL 的页面之间的契约。如果您的其中一个查询的结果返回 id 为 136 的帖子,则在您的 HTML 文件中执行 yoursite.com/edit-post.php?post_id=<?= $queryRow['post_id'] ?>">link
【解决方案2】:

确保您的数据库中有一个唯一的 ID 列 - 将其称为“postid”,并将其设置为随着每个新条目自动递增。

这样您就可以确保您插入到数据库中的每个条目都有一个唯一标识符。

然后您需要检索数据库中的项目列表,包括 postid,并为数据库中的每一行提供一个链接来显示它。

如果在 url 中选择了 postid,则显示该帖子的信息。如果不是,则显示一个列表:

<?

$dbh = new PDO('mysql:host=localhost;dbname=databasename;charset=utf8', 'username', 'password');

    ///No post id selected, display a list of everything
    if(!$_GET['postid']) {

    print "You're listing all the rows in the database<br/>"; 

    $query = $dbh->prepare("SELECT `postid`,`name` FROM `table` ORDER by postid ASC");
    $query->execute();
    while ($row = $query->fetch(PDO::FETCH_ASSOC)) {

      print "<a href=\"?post_id=".$row['postid']."\">".$row['name']."</a><br/>";
    }

    } else { //A post ID is selected

    $postid = $_GET['postid'];

    Print "Select from the database where postid = ".$postid;

    $query = $dbh->prepare("SELECT `postid`, `name`, `keywords`, `description` FROM `table` WHERE `postid` = '$postid' LIMIT 1");
    $query->execute();
    while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
    $name = $row['name'];
    $keywords = $row['keywords'];
    $description = $row['description'];

      print 'Displaying details for '.$name.': '.$keywords.', '.$description.' - create edit links here... <br/><br/>';
    }


    } //End post id is selected




?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-18
    • 2017-09-11
    • 1970-01-01
    • 1970-01-01
    • 2015-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多