php - 从数据库中检索未显示的帖子数据
问题描述
我尝试从数据库中检索数据,修复了错误消息,但仍然没有显示数据,http://localhost/webpage/info.php?post_id=76(帖子 ID 取决于我点击的帖子)。另外,我知道有些变量没有显示出来,我正在努力将所有东西都放在适当的位置,只是我放置的东西没有显示出来。还有一个到数据库的连接,只是我不会显示整个文件,因为它太大了。
给出错误截图的函数代码:
function single_post(){
if (isset($_GET['post_id'])) {
global $con;
$get_id = $_GET['post_id'];
$get_posts = "select * from posts where post_id='get_id'";
$run_posts = mysqli_query($con, $get_posts);
$row_posts = mysqli_fetch_array($run_posts);
$post_id = $row_posts['post_id'];
$user_id = $row_posts['user_id'];
$post_content = $row_posts['post_content'];
$post_content2 = $row_posts['post_content2'];
$price = $row_posts['price'];
$pclass = $row_posts['pclass'];
$specificclass = $row_posts['specificclass'];
$upload_image = $row_posts['upload_image'];
$post_date = $row_posts['post_date'];
$user = "select * from users where user_id='$user_id' AND posts='yes'";
$run_user = mysqli_query($con , $user);
$row_user = mysqli_fetch_array($run_user);
$user_name = $row_user['user_name'];
$user_image = $row_user['user_image'];
if (isset($_GET['post_id'])) {
$post_id = $_GET['post_id'];
}
$get_posts = "select post_id from users where post_id='$post_id'";
$run_user = mysqli_query($con,$get_posts);
$post_id = $_GET['post_id'];
$post = $_GET['post_id'];
$get_user = "select * from posts where post_id='$post'";
$run_user = mysqli_query($con, $get_user);
$row = mysqli_fetch_array($run_user);
$p_id = $row['post_id'];
if ($p_id != $post_id) {
echo "<script>alert('ERROR')</script>";
}
else{
echo"
<h2><strong>Información del producto:</strong></h2><br>
<p><strong>Nombre del producto: </strong>$post_content</p><br>
<p><strong>Información: </strong>$post_content2</p><br>
<p><strong>Precio dispuesto a pagar: </strong> $price</p><br>
<p><strong>Categoría: </strong> $pclass</p><br>
<p><strong>Subcategoría: </strong> $specificclass</p><br>
";
}
}
}
解决方案
get_id 的代码中存在拼写错误。
$get_posts = "select * from posts where post_id='get_id'";
应该
$get_posts = "select * from posts where post_id='$get_id'";
关于安全性,请清理您的 $_GET 值...
如果它始终是数字格式,请使用
is_numeric
检查它....
例如..
if (isset($_GET['post_id'])) {
$get_id = sanitized($_GET['post_id']); // sanitize with your created code for it....
if(is_numeric($get_id)){
global $con;
// $get_id = $_GET['post_id']; no need of it as we havev declared it above
$get_posts = "select * from posts where post_id='$get_id'";
//..... rest code.....
}else{
// show error regarding get id.. like not found etc..
}
推荐阅读
- python - 使用 datetime.strptime,为什么当我将年份输入为“20”时,它会显示年份为 2020 而不是 1990?
- reactjs - 我正在尝试删除一个项目,但每次单击按钮时都会返回错误“调度不是函数”
- c# - 在 XAML 中使用值转换器有什么好处?
- google-chrome - Chrome 开发工具:选择一个元素来检查它 -> 无法选择一个在它自己和子元素之间没有任何空格的元素
- java - 如何通过 apache poi 查找和写入一行 .doc 文件?
- virtualenv - 在 python 3.6 上安装 opencv 3.2 的问题
- ios - appendingPathComponent 有什么问题,它是可选的吗?
- typescript - 在打字稿中使用reduce,错误“元素隐式具有'any'类型,因为类型的表达式不能用于索引类型'{}'”
- php - 具有相同路由问题的 Laravel 分组中间件
- amazon-web-services - Native AWS 中是否有用于任务管理的服务或框架?