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

时间:2015-05-16 07:45:04

标签: php mysql fopen createfile

简单的用户帖子形式,它们都很好用,但希望它生成一个使用数据库中user_id的php文件。这样我就可以为该页面生成一个链接来查看单个帖子。我正在使用fopen来创建也适用的页面。

如何让它生成' user_id' .php作为新文件名?你可以看到它命名文件' 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}' )");   
}

2 个答案:

答案 0 :(得分:0)

请尝试以下代码:

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

答案 1 :(得分:0)

好的,所以我想出来了,而不是创建我动态创建的实际文件。

像这样 -

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

这会创建动态链接..

$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>';
    }