2014-11-03 56 views
-6

当我尝试搜索形式上的东西它让我看到了所有的变量错误...下面的代码是给别人打工,但不适合我

<form action="search.php" method="get"> 
    <input type="text" size="25" name="search" placeholder="search this site"> 
    <input type="submit" name="submit" value="search"></form> 
    <?php 
    include("includes/connect.php"); 
     if(isset($_GET['submit'])){ 
     $search_id = $_GET['search']; 
     $query = "select * from posts where post_title like '%search_id%'"; 
     $run = mysql_query($query); 
     while ($row=mysql_fetch_array($run)){ 
     $post_id = $row['post_id']; 
     $post_title = $row['post_title']; 
     $post_image = $row['post_image']; 
     $post_content = $row['post_content']; 
    } 
    ?> 
     <h2> 
     <a href="pages.php?id=<?php echo $post_id; ?>"> 
     <?php echo $post_title; ?></a> 
     </h2> 
    <center><img src="images/<?php echo $post_image; ?>" width="100" height="100"></center> 
     <p align="justify"><?php echo $post_content; ?></p> 
    <?php } ?> 

我认为这个问题是有循环的功能。但不知道如何解决这个...

+0

在您的问题中显示错误消息 – 2014-11-03 16:09:22

+0

运行此代码时出现错误? – 2014-11-03 16:11:12

+0

也可以推荐使用'mysqli'或'PDO'作为'mysql'已折旧并且不会再更新。 – chriz 2014-11-03 16:17:39

回答

4

你在发言中忘了$ -sign:

$query = "SELECT * FROM `posts` WHERE `post_title` like '%$search_id%'"; 

顺便说一句:请不要使用的mysql_query了,其弃用。改用mysqli或PDO。 :)

你也很容易mysql注入。尝试跳过$_GET值:mysql_real_escape_string($ _ GET ['搜索'])

此外,你正在覆盖你的while循环变量,因为变量的调用是在while循环之外。将这些变量放在while循环中,如下所示:

while ($row=mysql_fetch_array($run)){ 
    $post_id = $row['post_id']; 
    $post_title = $row['post_title']; 
    $post_image = $row['post_image']; 
    $post_content = $row['post_content']; 
?> 
<h2> 
    <a href="pages.php?id=<?php echo $post_id; ?>"><?php echo $post_title; ?></a> 
</h2> 
<center> 
    <img src="images/<?php echo $post_image; ?>" width="100" height="100"> 
</center> 
<p align="justify"> 
    <?php echo $post_content; ?> 
</p> 
<?php 
    }//<--- this one closes the while loop 
    }//<--- this one closes the if statement 
?> 
+0

@John它说未定​​义变量$ post_id未定义变量$ post_title未定义变量$ post_image未定义变量$ post_content – 684425 2014-11-03 17:03:53

+0

我是新来编码,不知道很多事情。但我想学习,这是我在php中的第一个项目:) – 684425 2014-11-03 17:05:48

+0

@ 684425检查你的查询是否好:用'run = mysql_query($ query)替换'$ run = mysql_query($ query) mysql_error());' – Refilon 2014-11-04 08:30:25

相关问题