繁体   English   中英

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

[英]PHP/MySQL - Image URL's with Spaces Not Working

我正在尝试从 MYSQL 数据库中获取图像 url 以显示在网页表上,但由于文件名中的空格,大多数链接都不起作用。 我已经尝试过 urlencode 和 rawurlencode 并且它适用于某些文件,但不适用于大多数文件。 我真的不知道为什么有些工作,而另一些不工作。也许这里有人可以提供帮助。

这是 output:

不工作:

<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是 url 到 MySQL 数据库中的图像。

这是整个 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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM