-1

我在 PHP 中使用搜索页面。但这是一个错误。请告诉我我的错误。

错误是:

注意:未定义变量:第 22 行 I:\xampp\htdocs\tsooj\includes\search_page.php 中的 post_title

注意:未定义变量:第 26 行 I:\xampp\htdocs\tsooj\includes\search_page.php 中的 post_content

源代码在这里:

<div id="content-main">
    <?php
    include("includes/connect.php");
    
    if(isset($_GET['search'])){
    
    $search_id = $_GET['value'];
    
    $search_query = "select * from posts where post_keywords like '%$search_id%'";
    
    $run_query = mysql_query($search_query);
    
    while ($search_row = mysql_fetch_array($run_query)){
    
    $post_title = $search_row['post_title'];
    $post_image = $search_row['post_image'];
    $post_content = substr($search_row['post_content'],0,150);
    }
    ?>
<h1>Your Search Result is here:<h1>

<h2><?php echo $post_title; ?></h2>

<img src="images/<?php echo $post_image; ?>">;

<p><?php echo $post_content; ?></p>

<?php } ?>
    
</div>
4

6 回答 6

1

变量$post_content;是在if语句内部定义的......它只能在if语句内部使用......首先在外部声明它,if然后在if和/或其他任何地方(即echo部件中)调用它。

于 2013-09-07T08:52:58.790 回答
1

这是因为您的 mysql_query() 函数无法运行您的查询!我会重写你的代码:

<div id="content-main">
    <?php
    include("includes/connect.php");
    if(isset($_GET['search']) && isset($_GET['value'])){
    if(mysql_set_charset('utf-8')){
     $search_id = mysql_real_escape_string($_GET['value']);
     $search_query = "SELECT * FROM post WHERE post_keywords LIKE '%".$search_id."%'";
     $run_query = mysql_query($search_query);
     if($run_query){
         while ($search_row = mysql_fetch_array($run_query)){
         $post_title = $search_row['post_title'];
         $post_image = $search_row['post_image'];
         $post_content = substr($search_row['post_content'],0,150);
         echo "<h1>Your Search Result is here:<h1>
                    <h2>$post_title</h2>
                    <img src=\"images/$post_image\">;
                    <p>$post_content</p>
                    </div>";
    }
    else{
         echo 'Mysql error: '.mysql_error(); exit();
   }
  }
   else{
          echo 'Mysql couldn\'t set charset'; exit();
   }
}        
?>

更新:mysql_real_escape_string() 函数清除用户输入以防止 SQL 注入。

于 2013-09-07T08:52:59.720 回答
1

你只需要在使用它之前定义你的变量。请参阅下面的代码。

//define your variable here
$post_title = "";
$post_content = "";

if(isset($_GET['search'])){
于 2013-09-07T08:33:24.173 回答
0

我怀疑这是因为当您的页面加载时$_GET['search']未设置变量,因此未运行 if 语句。这意味着没有对$post_title.

做这个:

<h2><?php if (isset($post_title)){ echo $post_title;} ?></h2>
于 2013-09-07T08:56:30.220 回答
0

尝试 var_dump($run_query) 看看它到底持有你的结果是什么?并验证是否有'post_title'

于 2013-09-07T08:38:36.673 回答
0
if(isset($_GET['search']) &&  $_GET['search']!='')
{

 $search_query = "select * from posts where post_keywords like '%" .$search_id. "%' ";

$run_query = mysql_query($search_query);

    while ($search_row = mysql_fetch_array($run_query)){

    $post_title = $search_row['post_title'];
    $post_image = $search_row['post_image'];
    $post_content = substr($search_row['post_content'],0,150);
    }
}
于 2013-09-07T08:38:47.407 回答