PHP/MySQL - 带有空格的图像 URL 不起作用



我试图从MYSQL数据库中获取图像url以显示在网页表上,但大多数链接由于文件名中的空间而无法工作。我已经尝试过urlencode和rawurlencode,它适用于一些文件,但不是大多数。我真的不知道为什么有些有用,有些不行。也许这里有人能帮忙。

输出:

不工作:

<img class="thumb" src="http://www.example.com/images/thumbnails/Brown" thrasher.jpg="">

工作:

<img class="thumb" src="http://www.example.com/images/thumbnails/Growing Ideas-Drew Lawson-1.jpg">

由于某种原因,它把引号放在第一个空格后面,而不是把整个字符串括起来。然而,它在第二个例子中工作。

下面是问题代码:

<td><img class='thumb' src=".$fetch[urlencode(image_link_1)]." /></td>

其中image_link_1是MySQL数据库中图像的url。

下面是整个php文件:https://pastebin.com/P6epznu1

<?php
require_once '/var/www/html/includes/artsurvey-con.php';
if(ISSET($_POST['search'])){
$search = urlencode($_POST['search']);
$search1 = urldecode($search);
$search2 = preg_replace("/[^a-zA-Z0-9,. ]/",'',$search1);
$query = $conn->query("SELECT * FROM `art_collection_records`
WHERE (`department` LIKE '%".$search2."%')
OR (`building` LIKE '%".$_POST['search']."%')
OR (`room_number` LIKE '%".$_POST['search']."%')
OR (`contact_person` LIKE '%".$_POST['search']."%')
OR (`category` LIKE '%".$_POST['search']."%')
OR (`painting` LIKE '%".$_POST['search']."%')
OR (`drawing` LIKE '%".$_POST['search']."%')
OR (`mixed` LIKE '%".$_POST['search']."%')
OR (`print` LIKE '%".$_POST['search']."%')
OR (`sculpture` LIKE '%".$_POST['search']."%')
OR (`craft` LIKE '%".$_POST['search']."%')
OR (`title` LIKE '%".$_POST['search']."%')
OR (`artist` LIKE '%".$_POST['search']."%')
OR (`how_acquired` LIKE '%".$_POST['search']."%')
OR (`back_notes` LIKE '%".$_POST['search']."%')
OR (`written_description` LIKE '%".$_POST['search']."%')
ORDER BY title ASC LIMIT 1200");
$row = $query->num_rows;
if($row > 0){
$output = "";
$output .= "
<center>
<h3>Search Results</h3>
</center>
<table class='table table-striped'>
<caption>End of Results</caption>
<thead class='thead-dark'>
<tr>
<th>ID</th>
<th>Department</th>
<th>Building</th>
<th>Room Number</th>
<th>Contact Person</th>
<th>Category</th>
<th>Painting</th>
<th>Drawing</th>
<th>Mixed</th>
<th>Print</th>
<th>Framed</th>
<th>Sculpture</th>
<th>Craft</th>
<th>Base</th>
<th>2D Size</th>
<th>3D Size</th>
<th>Title</th>
<th>Artist</th>
<th>Date Created</th>
<th>Date Acquired</th>
<th>Back Notes</th>
<th>Description</th>
<th>Image 1</th>
<th>Image 2</th>
<th>Image 3</th>
</tr>
</thead>
<tbody>";
while($fetch = $query->fetch_array()){
$output .= "
<tr>
<td>".$fetch['acs_id']."</td>
<td>".$fetch['department']."</td>
<td>".$fetch['building']."</td>
<td>".$fetch['room_number']."</td>
<td>".$fetch['contact_person']."</td>
<td>".$fetch['category']."</td>
<td>".$fetch['painting']."</td>
<td>".$fetch['drawing']."</td>
<td>".$fetch['mixed']."</td>
<td>".$fetch['print']."</td>
<td>".$fetch['framed']."</td>
<td>".$fetch['sculpture']."</td>
<td>".$fetch['craft']."</td>
<td>".$fetch['base']."</td>
<td>".$fetch['two_d_size']."</td>
<td>".$fetch['three_d_size']."</td>
<td>".$fetch['title']."</td>
<td>".$fetch['artist']."</td>
<td>".$fetch['date_created']."</td>
<td>".$fetch['date_acquired']."</td>
<td>".$fetch['back_notes']."</td>
<td>".$fetch['written_description']."</td>
<td><img class='thumb' src=".$fetch[urlencode(image_link_1)]." /></td>
<td><img class='thumb' src=".$fetch[rawurlencode(image_link_2)]." /></td>
<td><img class='thumb' src=".$fetch[rawurlencode(image_link_3)]." /></td>
</tr>";
}
$output .="
</tbody>
</table>
<a class='text-danger' href='#anchor'>Return to Top of Page</a>";
echo $output;
}else{
echo "<center><h4>Search Not Found!</h4></center>";
}
}
?>

任何帮助都是感激的。如果你需要更多的细节,请询问。谢谢!

因为src属性没有引号。因此,代码将以下内容输出到客户端:

<td><img class='thumb' src=http://www.example.com/images/thumbnails/Brown thrasher.jpg /></td>

不出所料,浏览器在该标记中将thrasher.jpg视为自己的属性。(本页的语法高亮显示也是这么看的。)当涉及到结构正确的HTML时,浏览器可以相当宽容,但这个例子表明,有效的HTML确实非常重要。

将属性值用引号括起来,就像代码对class属性所做的那样:

<td><img class='thumb' src='".$fetch[urlencode(image_link_1)]."' /></td>

最新更新