0

I just created a search function in PHP that searches my database table and returns results. Now how can i make the search result a button to download an mp3 file in my database? The file path is stored in a table while the file is uploaded on a folder.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Search results</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<?php
    $query = $_GET['query']; 
    // gets value sent over search form

    $min_length = 3;
    // you can set minimum length of the query if you want

    if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then

        $query = htmlspecialchars($query); 
        // changes characters used in html to their equivalents, for example: < to &gt;

        $query = mysql_real_escape_string($query);
        // makes sure nobody uses SQL injection

        $raw_results = mysql_query("SELECT * FROM tbl_uploads
            WHERE (`file` LIKE '%".$query."%') OR (`file` LIKE '%".$query."%')") or die(mysql_error());


        if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following

            while($results = mysql_fetch_array($raw_results)){
            // $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop

                echo "<p><h3>".$results['file']."</h3>".$results['file']."</p>";

            }

        }
        else{ // if there is no matching rows do following
            echo "No results";
        }

    }
    else{ // if query length is less than minimum
        echo "Minimum length is ".$min_length;
    }
?>
</body>
</html>
  • are you looking for this http://stackoverflow.com/questions/20956434/create-download-link-for-music-or-video – Doktor OSwaldo Mar 24 '16 at 11:34
  • no sir. i am using a code in my .htacess to download. i created a search query that searches the contents of my database table. but it shows the entire mp3 file name as plain text and not as a downloadable link. – Onwu Bishop Gideon Mar 24 '16 at 12:00
  • yeah of course, as far as i see, you echo it as a

    But a

    is plain text. You must at least put it in a

    – Doktor OSwaldo Mar 24 '16 at 12:06

2 Answers2

1

If you are trying to get PHP to retrieve the the file and present it as a download please see here

Community
  • 1
  • 1
DevilCode
  • 1,054
  • 3
  • 35
  • 61
0

Replace this:

echo "<p><h3>".$results['file']."</h3>".$results['file']."</p>";

With this

echo "<p><a href=Uploads/".$results['file'].">".$results['file']."</a></p>";

This should get you one step further

But you should consider looking at this answer: Create download link for music or video

Community
  • 1
  • 1
Doktor OSwaldo
  • 5,732
  • 20
  • 41