2012-07-10 68 views
0
<?php 
require 'db.php'; 
include_once("header.php"); 
include_once("functions.php"); 
include_once("profile.php"); 

if(isset($_POST['search_term'])){ 
    $search_term = mysql_real_escape_string(htmlentities ($_POST['search_term'])); 

    if(!empty($search_term)){ 

     $search = mysql_query("SELECT `username`,`id` FROM `users` WHERE `username` LIKE '%$search_term%' and `business` <> 'business'"); 


     $result_count = mysql_num_rows($search); 



     $suffix = ($result_count != 1) ? 's' : ''; 


     echo '<div data-theme="a">Your search for <strong>' , $search_term ,'</strong> returned <strong>', $result_count,' </strong> record', $suffix, '</div>'; 



     while($results_row = mysql_fetch_assoc($search)){ 
      echo '<div data-theme="a"><strong>', "<img src='/image/<?php echo $image; ?>' width= 50px height=50px>", $results_row['username'], '</strong></div>'; 



$following = following($_SESSION['userid']); 


    if (in_array($key,$following)){ 
     echo ' <div action= "action.php" method="GET" data-theme="a"> 
     <input type="hidden" name="id" value="$key"/> 
     <input type="submit" name="do" value="follow" data-theme="a"/> 
</div>'; 

    }else{ 
     echo " <div action='action.php' method='GET' data-theme='a'> 
     <input type='hidden' name='id' value='$key'/> 
     <input type='submit' name='do' value='follow' data-theme='a'/> 
     </div>"; 

} 

} 

     } 

} 

?> 

我想要一些帮助把用户图像放入此代码的回声部分。我不确定如何做到这一点,以便它将图像放在搜索的正确线上。任何建议将不胜感激。以下是我所指的代码行。谢谢。如何显示该图像?

而($ results_row = mysql_fetch_assoc($搜索)){

回声“, ””的宽度= 50像素 高度= 50像素>“,$ results_row [ '用户名'],' “;

+0

另外,在构建SQL查询时使用预处理语句。检查这篇文章,看看为什么'mysql_real_escape_string'不会保护您的数据库免受SQL注入 - http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string – 2012-07-10 01:45:58

回答

1

我没有看到$image定义在您的代码的任何地方,所以我会假设图像正在从数据库中拉出。

如果是这样的话,那么你会想要做这样的事情:

while($results_row = mysql_fetch_assoc($search)){ 
    echo '<div data-theme="a"><strong><img src="/image/'.$results_row['image'].'" style='width:50px;height:50px' />'.$results_row['username'].'</strong></div>'; 
} 
0

只是写:

..."<img src='/image/<?php echo $image; ?>' width='50' height='50'>", ... 

但你肯定50×50是合适的?您可能只想设置width=50并让浏览器相应地设置高度。

0
$image = 'someImage.png';  
echo '<div data-theme="a"><strong>', "<img src='/image/{$image}' width= 50px height=50px>", $results_row['username'],  '</strong></div>'; 
0

你会想是这样的:

<?php 
while($results_row = mysql_fetch_assoc($search)){ 
?> 
<div data-theme="a"> 
    <img src="/image/<?php echo $image; ?>" width="50px" height="50px"> 
    <strong> 
    <?php echo $results_row['username']; ?> 
    </strong> 
</div> 

<?php 
} 
?> 

有几件事情来,虽然检查:

  • 不使用标签来包装标签。它们通常仅用于文本
  • 您未在查询中选择任何图像。
  • 不要用echo来输出html的丑陋。
  • 请勿使用GET。
  • 使用预准备语句(PDO或mysqli)来防止mysql注入。
+0

如何你会输出HTML吗?而不是回声? – Steven 2012-07-10 02:04:11