简体   繁体   English

使用数据库中的user_id作为文件名创建动态php页面

[英]Create dynamic php page using user_id from database as filename

Simple user post form which all works great but would like it to generate a php file that uses the user_id in the database. 简单的用户发布表单,效果很好,但是希望它生成一个使用数据库中的user_id的php文件。 This way I can generate a link for that page to view the individual post. 这样,我可以为该页面生成一个链接以查看单个帖子。 I am using fopen to create the page which also works. 我正在使用fopen创建也可以使用的页面。

How do I have it generate the 'user_id'.php as the new filename? 如何让它生成“ user_id” .php作为新文件名? You can see its naming the file 'newfile.php' currently. 您可以看到其当前正在命名文件“ newfile.php”。

// Create Post
session_start();
if(isset($_POST['createPostBtn'])){
  $myusername = $_SESSION['username'];
  $postTitle = $_POST['postTitle'];
  $postDesc = $_POST['postDesc'];
  $id = $row['user_id'];

  $myfile = fopen('newfile.php', 'w') or die("can't open file");

  $txt = "<?php include('init.inc.php'); ?>";
  $txt2 = "<?php include('header.php'); ?>";
  fwrite($myfile, $txt);
  fwrite($myfile, $txt2);
  fwrite($myfile, $myusername);
  fwrite($myfile, $id);
  fwrite($myfile, $postTitle);
  fwrite($myfile, $postDesc);
  fclose($myfile);

  mysql_query("INSERT INTO `users` (`post_title`, `post_description`, `username`) VALUES ('{$postTitle}', '{$postDesc}', '{$myusername}' )");   
}

Try the following code: 尝试以下代码:

$id = '$row['user_id'].'.php'';
$myfile = fopen($id, 'w') or die("can't open file")

Ok so I figured this out, instead of creating the actual file I create it dynamically. 好的,所以我弄清楚了,不是动态创建文件,而是创建实际文件。

like this - 像这样 -

$forum .= '<div id="ptitle">' . '<a href=\mysite/thread.php?cid=' .$id . '>' . $ptitle.'</a>' . '</div>' ;

This creates the dynamic link.. 这将创建动态链接。

$cid = $_GET['cid'];
$sql = "SELECT postid, username, post_title, post_description FROM post WHERE postid='".$cid."' LIMIT 1";
$res = mysql_query($sql) or die (mysql_error());
    if(mysql_num_rows($res) == 1) {
        $sql2 = "SELECT * FROM post WHERE postid='" . $cid. "'";
        $res2 = mysql_query($sql2) or die (mysql_error());
    if(mysql_num_rows($res2) > 0) {

    while ($row = mysql_fetch_assoc($res2)) {
        $myid = $row['postid']; 
        $myname = $row['username'];
        $mytitle = $row['post_title'];
        $mydesc = $row['post_description'];

        echo '<div id="mainCont">';
        echo '<div id="mainTitle">' . $mytitle . '</div>';
        echo '<div id="mainDesc">' . $mydesc . '</div>';
        echo '<div id="mainName">' . 'posted by: ' . $myname . '</div>';
        echo '</div>';
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM