【问题标题】:PHP to display more columns of a SQL row on new page, then saving itPHP在新页面上显示SQL行的更多列,然后保存它
【发布时间】:2014-02-26 02:42:35
【问题描述】:

这里的任何帮助都会令人难以置信:
所需的工作流程:用户浏览 sql 行(第 1 页),仅显示选择性数据。单击一行后,它会路由(第 2 页上的脚本)到显示该行所有数据的页面(第 3 页)。并且该页面保存在目录中。

第 1 页 (index.php)

<?php
include("db.php");
$query="SELECT * FROM `stories` WHERE id
        BETWEEN 1 AND 200 order by RAND() LIMIT 50";
$result=mysqli_query($connection,$query);
?>
<?php
while ($data = mysqli_fetch_assoc($result)):
    $id = $data['id'];
    $title = $data['title'];
?>
    <li onclick="location.href='create_page.php?id=<?php echo $id; ?>';">
        <h3><?php echo $title; ?></h3>
    </li>
<?php
endwhile;
mysqli_close($connection);
?>

第 2 页 (create_page.php)

<?php
include("db.php");
$_GET['id'];
$query="SELECT * FROM `stories` WHERE id=$_GET[id]";
$result=mysqli_query($connection,$query);
while ($data = mysqli_fetch_assoc($result)):
    $id = $data['id'];
    $author = $data['author'];
    $title = $data['title'];
    $page_path = $data['page_path'];
    if(is_null($page_path)){$page_path = "#";}
    $data2 = file_get_contents("single-page.php");
    $data2 = str_replace($title2, $title, $data2);
    $data2 = str_replace($author2, $author, $data2);

    file_put_contents($page_path, $data2);

endwhile;
mysqli_close($connection);
?>

第 3 页(单页.php)

<!DOCTYPE HTML>
<html>
    <head>
        <title>
            <?php echo $title2; ?>
        </title>
    </head>
    <body>
        <?php echo $title2; ?>
        <?php echo $author2; ?>
    </body>
</html>

用户单击行后,脚本开始运行,但冻结(我检查过,页面源在浏览器中为空白)。参数“ID”在 url (. . .php?id= . .) 中,但之后没有。 'page_path' 是 sql 表中的一列(对每一行都是唯一的),并指示要保存页面的位置。 id 是唯一的 auto_num 列。还有,文件和文件夹权限都是777。

提前谢谢你!

替代方法:

<?php
// Start the buffering //
ob_start();
?>
Your page content bla bla bla bla ...

<?php
 echo '1';

 // Get the content that is in the buffer and put it             
In your file //
file_put_contents('yourpage.html',  
ob_get_contents());
 ?>

【问题讨论】:

  • 尝试打印 mysqli_query('----') 或 die('query not executed') 之类的错误。在第二页中 GET[id]-->更改为 GET[' id']
  • 当我将 [id] 更改为 ['id'] 时,dreamweaver 中出现语法错误

标签: php mysql get file-get-contents str-replace


【解决方案1】:

你的问题,好像就在这里

$query="SELECT * FROM `stories` WHERE id=$_GET[id]";

$_GET[id] 为空,您的变量为$_GET['id']

只是,在第 2 页测试这段代码

if(isset($_GET['id'])){
 if(is_numeric($_GET['id'])) {  //check if $_GET['id'] is a number
  $id=$_GET['id'];


  include("db.php");
  $query="SELECT * FROM `stories` WHERE id=$id";
echo $query;  //show the query.
  $result=mysqli_query($connection,$query);
  while ($data = mysqli_fetch_assoc($result)):
    $id = $data['id'];
    $author = $data['author'];
    $title = $data['title'];
    $page_path = $data['page_path'];
echo "id=".$id." author=".$author." title=".$title." page_path=".$page_path;
//show your variables.
    if(is_null($page_path)){$page_path = "#";}

if(is_file($page_path)){
        $data2 = file_get_contents("single-page.php");
        $data2 = str_replace($title2, $title, $data2);
        $data2 = str_replace($author2, $author, $data2);

        file_put_contents($page_path, $data2);
     }
else
echo "file".$page_path." doesnt exist :(";
  endwhile;
  mysqli_close($connection);
  }else echo "ID is not numeric";
 }else echo "error on ID data";

【讨论】:

  • 我在 dreamweaver 中遇到语法错误。 .第 4 行 - 在定义 id 时,它不喜欢有 'id'。脚本不会运行,也不会报告您添加的错误。虽然我需要改变我对包括它们的想法,但错误的回声是个好主意。还有什么建议吗?非常感谢您的宝贵时间。
  • @buzrw 现在检查,我添加了 is_numeric 条件。
  • 他们都已经是数字了。我做了建议的更改,我还读到文件路径需要是本地服务器而不是 http,因为我使用的是 Linux。仍然无法正常工作,我调整了代码以将标签放在其写入页面的括号中。我将上面的代码编辑为我当前使用的代码。此外,当我运行此代码并查看它冻结运行的页面源时,我得到以下信息:
  • 这里是 $page_path 值 single-pages/1-trebort-ouzzub-coatomer-proteinomplexubunit-gamma.php 的示例
  • 好的,让我们专注于第 2 页...当您在浏览器中输入 localhost/path_to_file/create_page.php?id=1 时发生了什么? ...它显示了什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
相关资源
最近更新 更多