gpt4 book ai didi

PHP 在新页面上显示 SQL 行的更多列,然后保存

转载 作者:行者123 更新时间:2023-11-30 00:37:34 25 4
gpt4 key购买 nike

这里的任何帮助都将是令人难以置信的:
所需的工作流程:用户浏览 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 页 (single-page.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());
?>

最佳答案

你的问题似乎就在这里

$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";

关于PHP 在新页面上显示 SQL 行的更多列,然后保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22030655/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com