问题描述
我不知道为什么没有任何数据显示,它只是显示我有硬编码的文件夹数据。但是,即使数据库中有数据,它也不会显示数据库中的任何数据。有人可以帮忙吗?我已经解决了整整一天,甚至这个问题可能会重复并且我无法搜索我想要的答案,因此可能需要别人的帮助。感激。
以下是pdf调用“ generate_pdf.PHP”的代码
<?PHP
require 'config.PHP';
?>
<html>
<head>
<title>8D Generator</title>
</head>
<body>
<form method='POST' action='generate_pdf.PHP'>
<select name='pdfID'>
<?PHP
$query=MysqLi_query($conn,'select * from images');
while($gpdf=MysqLi_fetch_array($query))
{
echo "<option value='".$gpdf['Id']."'>".$gpdf['Id']."</option>";
}
?>
</select>
<input type='submit' value='Generate'>
</form>
</body>
</html>
这是可以选择ID并通过单击生成按钮“ formselect.PHP”来生成PDF的表单。
char* ReadFile(const char* filename) {...}
void WriteFile(const char* filename,const char* data) {
FILE* fptr = fopen(filename,"r+");
if (fptr)
std::cout << filename << " opened Succesfully.\n";
else {
std::cout << "\nFile not found! Do you want to create one?\n";
std::cout << "Type Y,y or 1 to create the file that doesn't exist press any other key to exit\n";
std::string input;
std::cin >> input;
while (input.at(0) == 'Y' || input.at(0) == 'y' || input.at(0) == '1') {
fptr = fopen(filename,"w");
if (fptr) {
std::cout << filename << " created Succesfully\n";
break;
} else {
std::cout << "Error creating " << filename << std::endl;
break;
}
}
while (input.at(0) != 'Y' || input.at(0) != 'y' || input.at(0) != '1') {
exit(0);
}
}
while ((*data)) {
fputc(*data,fptr);
++data;
}
if (!fclose(fptr))
std::cout << filename << " closed\n";
else
std::cout << "Error closing " << filename << std::endl;
}
int main(int argv,const char* argc[]) {
const char* filebuffer = ReadFile(argc[1]);
for (int i = 2; i < 10; ++i) {
if (argc[i] != (char)0 )
WriteFile(argc[i],filebuffer);
}
return 0;
}
它应该以pdf格式显示,但是现在输出仅显示pdf的设计,而不会显示所有数据。有人可以帮忙吗?
解决方法
首先,您必须更正此行(请参阅@Simone Rossaini评论):
/* error:
$query=mysqli_query($conn,"select * from images where Id = '".isset($_POST['pdfId'])."'");
*/
因为isset()函数返回boolean
(对或错)。因此,您的查询将永远无法工作,因为Id
无效。 (Id应该是字符串或整数,对吗?)
将查询更改为以下内容:
$pdf_id = isset($_POST['pdfId']) ? mysqli_real_escape_string($conn,$_POST['pdfId']) : "";
if (empty($pdf_id)) {
// no id was given -> alert the user
die('Error: no id!');
}
$query=mysqli_query($conn,"select * from images where Id = '".$pdf_id."'");
它起作用了吗?运行查询后,尝试添加var_dump($gpdf);
,以检查是否获得正确的数据。