2014-11-03 20 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

相關問題