0

I'm new to PHP and have been working on a generic php based search engine in order to search through a MYSQL database. I've stored pictures of buildings on google drive at in goo.gl format. My question is how would I go about creating a hyperlink for the url listed within the database because the links will differ in most cases when being pulled from the database. Either having a clickable link or showing the picture would be great.

<html>
<head>
<title> Buildings</title>
</head>
<center>
<body>


<?php
include "connection.php";

$sql = "SELECT * FROM Building_Loc ";

if (isset($_POST['search'])) {

$search_term = mysql_real_escape_string($_POST['search_box']); 
$sql .= "WHERE Building LIKE '%{$search_term}%' "; 
$sql .= "OR Floor LIKE '%{$search_term}%' "; 
$sql .= "OR Number LIKE '%{$search_term}%' "; 
$sql .= "OR Building_Pictures LIKE '%{$search_term}%' ";
}   

$query = mysql_query($sql) or die(mysql_error());



?>
<form name="search_form" method="POST" action="display_data.php"> 

Search: <input type="text" name="search_box" value="" /> 
<input type="submit" name="search" value="Search">
</form>


<table width="80%" cell padding="60" cellspace="60">

<tr>
   <td><strong>Building</strong></td>            
   <td><strong>Floor</strong></td>
   <td><strong>Number</strong></td>
   <td><strong>Picture</strong></td>

</tr>
<?php while ($row = mysql_fetch_array($query)) { ?>
<tr>
   <td><?php echo $row['Building']; ?></td>              
   <td><?php echo $row['Floor']; ?></td>
   <td><?php echo $row['Number']; ?></td>       
   <td><?php echo $row['Building_Pictures']; ?></td>
</tr>

<?php } ?>

</body>
</center>
</html>
  • Can you show us your table schema? – Jono20201 Jul 04 '14 at 20:53
  • You want to generate links in the listing to urls to your site for displaying details about that particular building? – didierc Jul 04 '14 at 20:56
  • Or are the urls already in the db? – didierc Jul 04 '14 at 21:18
  • The URLs are already located within the database. The database is displaying the urls to the search engine which is exactly how I want it to be but just want those URL's to be clickable and link to wherever they are located on Google Drive. I'm also working to implement a more up to date MYSQL method. – user3806500 Jul 05 '14 at 21:36

1 Answers1

0

Grab the URL string from the database, say we put it into a variable $URL, then

Hyperlink:

<?php echo "<a href =\"{$URL}\">Click to view</a>" ?>

Image:

<?php echo "<img src =\"{$URL}\" />" ?> 

Additionally, your code is vulnerable to SQL injects; refer to How can I prevent SQL injection in PHP?

Community
  • 1
  • 1
mattpk
  • 11
  • 1
  • I'll be updating the SQL in order to prevent that but my real question is lets say each stored URL in the database is different so every building has a different URL that its pointing to, how would I go about pulling each URL into the PHP and making it clickable. The pictures are stored in google drive and the URL's point directly to the pictures. – user3806500 Jul 08 '14 at 13:01