Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从mysql数据库下载Mp3文件并增加每次单击的下载次数_Php_Mysqli - Fatal编程技术网

Php 从mysql数据库下载Mp3文件并增加每次单击的下载次数

Php 从mysql数据库下载Mp3文件并增加每次单击的下载次数,php,mysqli,Php,Mysqli,我想设计一个e-music php网络应用程序,我想实现以下目标: 1.点击下载按钮时,应下载文件 2.该文件的下载次数应增加1 下面的代码显示了音乐列表,即新版本和热门下载 <?php $hostname_conn = "localhost"; $database_conn = "e-music"; $username_conn = "root"; $password_conn = ""; $conn = mysqli_connect($hostname_conn, $userna

我想设计一个e-music php网络应用程序,我想实现以下目标: 1.点击下载按钮时,应下载文件 2.该文件的下载次数应增加1 下面的代码显示了音乐列表,即新版本和热门下载

<?php 
  $hostname_conn = "localhost";
$database_conn = "e-music";
$username_conn = "root";
$password_conn = "";
$conn = mysqli_connect($hostname_conn, $username_conn, $password_conn, $database_conn) or trigger_error(mysqli_error()); 

  $sql="SELECT * from music INNER JOIN artist on music.a_id=artist.a_id INNER JOIN category on music.cat_id=category.cat_id order by upload_date Desc limit 0,3";
  $Result= mysqli_query($conn,$sql) or die('Cannot Retrive Record' . mysqli_error());
  $sql1="SELECT * from music INNER JOIN artist on music.a_id=artist.a_id INNER JOIN category on music.cat_id=category.cat_id order by downloads Desc limit 0,3";
 $Result1= mysqli_query($conn,$sql1) or die('Cannot Retrive Record' . mysqli_error());
 ?>    
<table align="center" class="table table-striped table-hover ">
        <thead>
            <tr>
                <th colspan="3" align="center"> <h3>New Releases </h3></th>
            </tr>
        </thead>
        <tbody>
        <?php  while ($row=mysqli_fetch_assoc($Result)) { ?>
            <tr>


                <td class="col-md-6">  <div class="col-md-2"><img src="uploads/<?php echo $row['artist_image']; ?>" class="img-responsive ims"  alt="Image" >
                </div>
                  <label> Artist: <?php echo $row['artist_name'] ?></label> <br/> 
                <label> Title: <?php echo $row['m_title'] ?></label> <br/>
                <label>Category: Wakoin <?php echo $row['cat_name'] ?></label>
                </td>
                <td>

              <a  class="btn btn-xs btn-primary" href="download.php?did=<?php echo $row['mid'];?>"> Downloader</a>


                </td>
                <td class="col-md-2">
                    Format:<?php echo $row['m_format']; ?>
                </td>
                <td class="col-md-2">
                    File Size: <?php echo round($row['size']/1048576,2); ?> MB
                </td>
            </tr>
            <?php 
            }
             ?>
        </tbody>

    </table>
    <table align="center" class="table table-striped table-hover">
        <thead>
            <tr>
                <th colspan="3" align="center"> <h3>Top Downloads </h3></th>
            </tr>
        </thead>
        <tbody>
        <?php  while ($row=mysqli_fetch_assoc($Result1)) { ?>
            <tr>
                <td class="col-md-6">  <div class="col-md-2"><img src="uploads/<?php echo $row['artist_image']; ?>" class="img-responsive ims"  alt="Image" >
                </div>
                  <label> Artist: <?php echo $row['artist_name'] ?></label> <br/> 
                <label> Title: <?php echo $row['m_title'] ?></label> <br/>
                <label>Category: Wakoin <?php echo $row['cat_name'] ?></label>
                </td>

                <td class="col-md-2">
                   <a  class="btn btn-xs btn-primary" href="download.php?did=<?php echo $row['mid'];?>"> Downloader</a>


                </td>
                <td class="col-md-2">
                    Format: <?php echo $row['m_format']; ?>
                </td>
                <td>
                    File Size :<?php echo round($row['size']/1048576,2); ?> MB
                </td>
            </tr>
            <?php 
            }
             ?>
        </tbody>

    </table>

新版本
“class=”img responsive ims“alt=”Image“>
艺术家:
标题:
类别:Wakoin 格式: 文件大小:MB 热门下载 “class=”img responsive ims“alt=”Image“> 艺术家:
标题:
类别:Wakoin 格式: 文件大小:MB
我在点击“downloader”按钮(重定向到download.php文件)时尝试下载的代码如下:

$hostname_conn = "localhost";
$database_conn = "e-music";
$username_conn = "root";
$password_conn = "";
$conn = mysqli_connect($hostname_conn, $username_conn, $password_conn, $database_conn) or trigger_error(mysqli_error()); 

    $did=$_GET['did'];

$sql="SELECT target from music  where mid='$did'";
  $Result= mysqli_query($conn,$sql) or die('Cannot Retrive Record' . mysqli_error());
 $row = mysqli_fetch_assoc($Result);
// the target filed contains the directory and file name of the file to be downloaded e.g uploads/Akon_Sorry Blame.mp3
 echo "<a href=". $row['target'] ."  > download</a>";
 $sql1="update music set download=download+1 where mid='$did";
$Result= mysqli_query($conn,$sql1) or die('Cannot Increment number of downloads' . mysqli_error());
$hostname\u conn=“localhost”;
$database_conn=“电子音乐”;
$username\u conn=“root”;
$password_conn=“”;
$conn=mysqli_connect($hostname_conn、$username_conn、$password_conn、$database_conn)或trigger_error(mysqli_error());
$did=$_GET['did'];
$sql=“从音乐中选择目标,其中mid='$did'”;
$Result=mysqli_query($conn,$sql)或die('cannotretrieve Record'.mysqli_error());
$row=mysqli\u fetch\u assoc($Result);
//目标文件包含要下载的文件的目录和文件名,例如uploads/Akon_.mp3
回声“;
$sql1=“更新音乐集下载=下载+1,其中mid='$did”;
$Result=mysqli_query($conn,$sql1)或die('无法增加下载次数'.mysqli_error());

通过对我的项目的研究,我能够使用ajax解决我的问题。下面是我用来实现目标的代码

//index page
<!DOCTYPE html>
<html>
<head>
<title>E-music</title>
<link href="includes/Style.css" rel="stylesheet" type="text/css"/>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="bootstrap-3.3.7-dist/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
  <script src="jQuery/jquery-1.11.1.min.js" type="text/javascript"></script>
  <script src="bootstrap-3.3.7-dist/js/bootstrap.min.js" type="text/javascript"></script>
  <script src="bootstrap-3.3.7-dist/js/site.js" type="text/javascript"></script>


<?php 
  require_once("includes/connection.php");
  $sql="SELECT * from music INNER JOIN artist on music.a_id=artist.a_id INNER JOIN category on music.cat_id=category.cat_id order by upload_date Desc limit 0,3";
  $Result= mysqli_query($conn,$sql) or die('Cannot Retrive Record' . mysqli_error());
  $sql1="SELECT * from music INNER JOIN artist on music.a_id=artist.a_id INNER JOIN category on music.cat_id=category.cat_id order by downloads Desc limit 0,3";
 $Result1= mysqli_query($conn,$sql1) or die('Cannot Retrive Record' . mysqli_error());
 ?>


<div id="nav">
<a href="SearchMusic.php" class="btn btn-primary">Search For Music Audio</a>
<p>Wakokin Hausa Na Gargajiya
 </p>
</div>

<div id="section">
<table align="center" class="table table-striped table-hover ">
    <thead>
        <tr>
            <th colspan="3" align="center"> <h3>New Releases </h3></th>
        </tr>
    </thead>
    <tbody>
    <?php  while ($row=mysqli_fetch_assoc($Result)) { ?>
        <tr>
            <td class="col-md-6">  <div class="col-md-2"><img src="uploads/<?php echo $row['artist_image']; ?>" class="img-responsive ims"  alt="Image" >
            </div>
              <label> Artist: <?php echo $row['artist_name'] ?></label> <br/> 
            <label> Title: <?php echo $row['m_title'] ?></label> <br/>
            <label>Category: Wakoin <?php echo $row['cat_name'] ?></label>
            </td>
            <td>
               <a href="<?php echo $row['target'] ?>"  d="<?php echo $row['mid'] ?>" class="dload btn btn-xs btn-primary"  download>Download</a>

            </td>
            <td class="col-md-2">
                Format:<?php echo $row['m_format']; ?> <br>

                Total Download: <?php echo $row['downloads']; ?>
            </td>
            <td class="col-md-2">
                File Size: <?php echo round($row['size']/1048576,2); ?> MB
            </td>
        </tr>
        <?php 
        }
         ?>
    </tbody>

</table>
<table align="center" class="table table-striped table-hover">
    <thead>
        <tr>
            <th colspan="3" align="center"> <h3>Top Downloads </h3></th>
        </tr>
    </thead>
    <tbody>
    <?php  while ($row=mysqli_fetch_assoc($Result1)) { ?>
        <tr>
            <td class="col-md-6">  <div class="col-md-2"><img src="uploads/<?php echo $row['artist_image']; ?>" class="img-responsive ims"  alt="Image" >
            </div>
              <label> Artist: <?php echo $row['artist_name'] ?></label> <br/> 
            <label> Title: <?php echo $row['m_title'] ?></label> <br/>
            <label>Category: Wakoin <?php echo $row['cat_name'] ?></label>
            </td>

            <td class="col-md-2">
               <a href="<?php echo $row['target'] ?>" d="<?php echo $row['mid'] ?>"  class="dload btn btn-xs btn-primary" download>Download</a>

            </td>
            <td class="col-md-2">
                Format: <?php echo $row['m_format']; ?>
                Total Download: <?php echo $row['downloads']; ?>
            </td>
            <td>
                File Size :<?php echo round($row['size']/1048576,2); ?> MB
            </td>
        </tr>
        <?php 
        }
         ?>
    </tbody>

</table>
</div>
//php来处理数据库更新

<?php
require_once("includes/connection.php");

    $id = $_POST["id"];

    $Result= mysqli_query($conn,"SELECT downloads as total FROM music WHERE mid='$id'");
    $Res = mysqli_fetch_assoc($Result);
    $re = $Res['total'] + 1;
    $updateCount = mysqli_query($conn, "UPDATE music SET downloads= '$re' WHERE mid=$id ");

?>

查询
updte
应该是
update
我在这里犯了一个错误。。。我的文件没有下载。出于一些原因,您需要一个
WHERE
子句,其中
受影响的行()
。而
mysqli\u error()
需要一个db连接作为参数。Thaks alot。。我已经改正了。。我的主要问题是,我选择下载的文件没有下载
<?php
require_once("includes/connection.php");

    $id = $_POST["id"];

    $Result= mysqli_query($conn,"SELECT downloads as total FROM music WHERE mid='$id'");
    $Res = mysqli_fetch_assoc($Result);
    $re = $Res['total'] + 1;
    $updateCount = mysqli_query($conn, "UPDATE music SET downloads= '$re' WHERE mid=$id ");

?>