gpt4 book ai didi

php - 检查帖子是否存在

转载 作者:行者123 更新时间:2023-11-29 00:22:07 25 4
gpt4 key购买 nike

所以我想检查数据库中是否存在帖子,但我在重定向方面遇到了一些问题。这是我到目前为止的工作:

echo '<br>';//the $row part tells the DB what post his looking on
echo '<a href="comments.php?post_id='. $row['id'].'">View comments</a>';

这是显示评论按钮,可转到您查看帖子评论的部分。


<?php
require_once ('checkp.php');

我让它要求一次后检查脚本。


<?php
include ('variables.php');
//connects to DB
$dbc=mysql_connect($host,$user,$pass);


if ($dbc) {


} else {

echo ('Failed to connect to MySql; '. mysql_error());

}


//selects db from MySQl
$sqldb=mysql_select_db('a2318052_blog');

$pid=$_GET['post_id'];

$query1="SELECT * FROM posts_b WHERE id='$pid'";
$sql=mysql_query($query);

if ($sql) {


} else {

echo "cant run query";

}


if (mysql_num_rows($sql) > 0) {

echo "that post does not exist!";

} else {

header ("location: comments.php?post_id='. $pid.'");

}

?>

这是检查空结果然后重定向回来的脚本。我相信它与这里的重定向有关(标题(“位置:comments.php?post_id ='。$ pid。'”);)

最佳答案

您在重定向中混用了引号:

"location: comments.php?post_id='. $pid.'"

应该是

"location: comments.php?post_id=". $pid

php 中的点用于连接字符串。 Bu 在那里你用 "打开字符串并用 ' 关闭它。

编辑:还有其他人已经注意到您使用的是查询而不是查询 1。我也想而不是:

if (mysql_num_rows($sql) > 0) {

echo "that post does not exist!";

你想要别的东西:

if (mysql_num_rows($sql) == 0) {

echo "that post does not exist!";

关于php - 检查帖子是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20683399/

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