更新 PHP 和 JS 播放器

问题描述 投票:0回答:1

)我正在为广播电台的网站编写代码分配,并且页面上的播放器出现问题。问题是我不知道如何更新“现在”和“下一步”块,它们对数据库运行查询。您需要确保它们在曲目结束时得到更新。我提供代码:

    <?php
        include "connect.php";

        function currentSong($time) {
            global $mysqli;
            global $way;
            global $time_next;
        
            $result = $mysqli-> query("SELECT * FROM `music` WHERE `time` = '$time'");
    
            $now;
            foreach($result as $row) {
                $image = "image";
                $time = "time";
                $image = $row["image"];
                $time_now = substr($row["time"], 0, -3);
               
                $now = <<<HTML
                    <div class="block_now_next">
                        <img class="swiper_img_music" src="{$image}">
                        <div>
                            <p class="text_40_swiper">{$row["name"]}</p>
                            <p class="text_36_swiper">{$row["author"]}</p>
                            <p class="text_36_swiper">Слушайте в {$time_now}</p>
                        </div>
                    </div>
                HTML;
                
                $way = $row["way"];
                $time_next = $row["time_next"];
                global $time;
                $time = $row["time_next"];
            }
            return $now;
        }
        function nextSong($time_next) {
            global $mysqli;
            global $way;
            global $time_next;
    
            $result = $mysqli-> query("SELECT * FROM `music` WHERE `time` = '$time'");

            $next;
            foreach($result as $row) {
                $image = "image";
                $time = "time";
                $image = $row["image"];
                $time_now = substr($row["time"], 0, -3);
           
                $next = <<<HTML
                    <div class="block_now_next">
                        <img class="swiper_img_music" src="{$image}">
                        <div>
                            <p class="text_40_swiper">{$row["name"]}</p>
                            <p class="text_36_swiper">{$row["author"]}</p>
                            <p class="text_36_swiper">Слушайте в {$time_now}</p>
                        </div>
                    </div>
                HTML;
            }
            return $next;
        }
    ?>

    <fifth_screen>
        <div class="text_now_next">
            <p class="name_now_next">Сейчас</p>
            <p class="name_now_next">Далее</p>
        </div>
        <div class="now_next">
            <?php

                $time = '00:00:00';
                $time_next = '00:00:00';
                $way;
                echo currentSong($time);
                echo nextSong($time_next);

            ?>
        </div>

        <div class="player_center">
            <div id="player"></div>
        
            <script>
                let player = new Playerjs({id:"player", file:"<?php echo $way ?>"});
                let count = 1;
                document.getElementById('player').addEventListener('end', () => {
                
                    console.log(`<?php echo $time ?>`)
                })
            </script>
        </div>
    </fifth_screen>

我将非常感谢您的帮助!)

javascript php
1个回答
0
投票

这是已解决且防SQL注入的版本

  1. 使用准备好的语句来防止SQL注入漏洞。
  2. 确保动态 SQL 参数的正确转义。
  3. 为了提高可读性进行了小幅清理。
<?php
include "connect.php";

function currentSong($time) {
    global $mysqli;
    global $way;
    global $time_next;

    // Prepare and bind the statement to prevent SQL injection
    $stmt = $mysqli->prepare("SELECT * FROM `music` WHERE `time` = ?");
    $stmt->bind_param("s", $time);
    $stmt->execute();
    $result = $stmt->get_result();

    $now = '';
    foreach($result as $row) {
        $image = $row["image"];
        $time_now = substr($row["time"], 0, -3);
       
        $now .= <<<HTML
            <div class="block_now_next">
                <img class="swiper_img_music" src="{$image}">
                <div>
                    <p class="text_40_swiper">{$row["name"]}</p>
                    <p class="text_36_swiper">{$row["author"]}</p>
                    <p class="text_36_swiper">Слушайте в {$time_now}</p>
                </div>
            </div>
        HTML;
        
        $way = $row["way"];
        $time_next = $row["time_next"];
    }
    $stmt->close();
    return $now;
}

function nextSong($time_next) {
    global $mysqli;
    global $way;

    // Prepare and bind the statement to prevent SQL injection
    $stmt = $mysqli->prepare("SELECT * FROM `music` WHERE `time` = ?");
    $stmt->bind_param("s", $time_next);
    $stmt->execute();
    $result = $stmt->get_result();

    $next = '';
    foreach($result as $row) {
        $image = $row["image"];
        $time_now = substr($row["time"], 0, -3);
   
        $next .= <<<HTML
            <div class="block_now_next">
                <img class="swiper_img_music" src="{$image}">
                <div>
                    <p class="text_40_swiper">{$row["name"]}</p>
                    <p class="text_36_swiper">{$row["author"]}</p>
                    <p class="text_36_swiper">Слушайте в {$time_now}</p>
                </div>
            </div>
        HTML;
    }
    $stmt->close();
    return $next;
}
?>

<fifth_screen>
    <div class="text_now_next">
        <p class="name_now_next">Сейчас</p>
        <p class="name_now_next">Далее</p>
    </div>
    <div class="now_next">
        <?php
            $time = '00:00:00';
            $way = '';
            echo currentSong($time);
            echo nextSong($time_next);
        ?>
    </div>

    <div class="player_center">
        <div id="player"></div>
    
        <script>
            let player = new Playerjs({id:"player", file:"<?php echo $way ?>"});
            let count = 1;
            document.getElementById('player').addEventListener('end', () => {
                console.log(`<?php echo $time ?>`)
            })
        </script>
    </div>
</fifth_screen>
© www.soinside.com 2019 - 2024. All rights reserved.