-4

为什么以下 php 脚本不显示图像。我检查了数据库连接正常,Mysql Query 也正常。

php代码:

$upload_directory = "uploaded"; //set upload directory

$getimages = mysql_query("SELECT * FROM property_step3 WHERE propertyid =  
'$propertyid' AND active =1 AND uname = '$uname'"); 

$re2 = mysql_fetch_array($getimages)
$images = mysql_real_escape_string(htmlspecialchars(trim($re2['imgname'])));


echo '<img src="$upload_directory/$images" width="98" height="68"  /></a>'; 

也许我错了..

我不明白为什么有些人投票给我 DOWN ??!!

更新:

<?php
 $uname = $_SESSION['uname'];
 $check = mysql_query("SELECT * FROM property_step1 WHERE uname = '$uname' AND active 
 = 1");
 $num = mysql_num_rows($check);

if($num == 0)
{

echo "<h3>You didn't upload any property. Click <a href='publishadvert.php'>here</a> 
to publish your property</h3>";

}
else
{
    echo "<h3>You have $num published property.</h3>";

    echo "<table width='1020' cellpadding='0' cellspacing='0'>";
        echo "<tr>";
    echo "<td class='tabletr3'><b>Property Title</b></td>";
    echo "<td class='tabletr3'><b>Area</b></td>";
    echo "<td class='tabletr3'><b>State</b></td>";
        echo "<td class='tabletr3'><b>City</b></td>";
    echo "<td class='tabletr3'><b>Country</b></td>";            
    echo "<td class='tabletr3'><b>Images</b></td>";         
    echo "<td class='tabletr3'><b>Action</b></td>";         
    echo "</tr>";

    while($re = mysql_fetch_array($check))
    {
$propertyid = (int) $re['propertyid'];  
$country = mysql_real_escape_string(htmlspecialchars(trim($re['pro_country'])));
$state = mysql_real_escape_string(htmlspecialchars(trim($re['pro_state'])));
$area = mysql_real_escape_string(htmlspecialchars(trim($re['pro_area'])));
$city = mysql_real_escape_string(htmlspecialchars(trim($re['pro_city'])));
$title = mysql_real_escape_string(htmlspecialchars(trim($re['pro_title'])));

 $getimages = mysql_query("SELECT * FROM property_step3 WHERE propertyid = 
 '$propertyid' AND active =1 AND uname = '$uname'");

    $upload_directory = dirname(__file__) . '/uploaded/'; //set upload directory    
while($re2 = mysql_fetch_array($getimages))
{
$images = mysql_real_escape_string(htmlspecialchars(trim($re2['imgname'])));
}



        echo "<tr>";
            echo "<td class='tabletr2'>$title</td>";
            echo "<td class='tabletr2'>$country</td>";
            echo "<td class='tabletr2'>$state</td>";
            echo "<td class='tabletr2'>$city</td>";
            echo "<td class='tabletr2'>$area</td>";     
                  echo '<img src="'.$upload_directory.'/'.$images.'" width="98" 
 height="68"  /></a>';      

echo "<td class='tabletr2'><img src='uploaded/$images' 
/></td>";                               
echo "<td class='tabletr2'><a href='editproperty.php?propertyid=$propertyid&
uname=$uname'>Edit</a> | <a href='deleteproperty.php?propertyid=$propertyid'>Delete</a>
</td>";
    echo "</tr>";
    }  
    echo "</table>";

     }
 ?>
4

4 回答 4

1

错误 1:Waqar Alamgir 所说的,但更明确。尝试在单引号中使用变量。

echo '<img src="$upload_directory/$images" width="98" height="68"  /></a>'; 

将输出

<img src="$upload_directory/$images" width="98" height="68"  /></a>

然而

echo "<img src=\"$upload_directory/$images\" width=\"98\" height=\"68\"  /></a>"; 

或者

echo '<img src="'. $upload_directory . '/' . $images .'" width="98" height="68"  /></a>'; 

或者

echo '<img src="', $upload_directory, '/', $images, '" width="98" height="68"  /></a>'; 

将输出

<img src="uploaded/theimagename.jpg" width="98" height="68"  /></a>

哪里$images等于theimagename.jpg

错误 2:(由 Pankaj Khairnar 发现)

$re2 = mysql_fetch_array($getimages)

缺少一个分号。这会破坏你的脚本。

$re2 = mysql_fetch_array($getimages);

错误 3 (c/o Michael Berkowski)

不要在输出数据上调用 mysql_real_escape_string()

于 2012-11-25T15:17:59.823 回答
0

像这样使用

$upload_directory = "uploaded"; //set upload directory
$getimages = mysql_query("SELECT * FROM property_step3 WHERE propertyid =  
'$propertyid' AND active =1 AND uname = '$uname'"); 

$re2 = mysql_fetch_array($getimages);
$images = trim($re2['imgname']);
echo '<img src="'.$upload_directory.'/' .$images. '" width="98" height="68"  /></a>'; 

还 ; 最后在您的声明中丢失

$re2 = mysql_fetch_array($getimages)

应该是这样的

$re2 = mysql_fetch_array($getimages);
于 2012-11-25T15:00:27.240 回答
0

引号问题您不能在单个 ' 中使用变量

$upload_directory = "uploaded";
$getimages = mysql_query("SELECT imgname FROM property_step3 WHERE propertyid =  
'$propertyid' AND active =1 AND uname = '$uname'"); 
$re2 = mysql_fetch_assoc($getimages)
$images = $re2['imgname'];
echo 'Debug image: ',$images;

echo '<a href="#"><img src="',$upload_directory,'/',$images,'" width="98" height="68"  /></a>'; 
于 2012-11-25T15:06:09.847 回答
0

也许您需要完整的网址来显示图像,例如添加

http://localhost/.../uploaded/50b11aefaad43font-8.jpg

我不确定您机器上的目录层次结构。

我假设“上传”文件夹位于您的 htdocs 根目录中。

于 2012-11-25T15:10:45.220 回答