首页 > 解决方案 > 将数据插入弹出窗口(需要帮助)

问题描述

我做了一个弹出窗口,假设连接到 MySQL,它确实连接了,但我有 2 个按钮,它假设为它自己(个人)插入数据。目前,当我按下 1 个按钮时,一切正常,但当我按下第二个按钮时,它会显示第一个按钮的数据。我试图在互联网上查找,但找不到任何可以帮助我的东西。我的代码:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "megadrop";

// Create connection
$conne = new mysqli($servername, $username, $password, $dbname);
//check connection
if ($conne->connect_error) {
die("Connection failed: " . $conne->connect_error);
}

$sql = "SELECT * FROM uslugi";
$result = $conne->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($rowe = $result->fetch_assoc()) {
echo "<!-- The Modal -->";
echo "<div id='myModal' class='modal'>";

  echo "<!-- Modal content -->";
  echo "<div style='width:700px;height:300px;margin-top: 100px' class='modal-content'>";
    echo "<div class='modal-header'>";
      echo "<span class='close'>&times;</span>";
        
         echo "<h2>".$rowe['nazwa']."<h2>";
    echo "</div>";
    echo "<div class='modal-body'>";
        echo "<p>".$rowe['nazwa']."<p>";
   echo "</div>";
  echo "</div>";

echo "</div>";
    }
}
$conne->close();
?>

        <script>
        // Get the modal
        var modal = document.getElementById('myModal');

        // Get the button that opens the modal
        var btns = document.getElementsByClassName("myBtn");

        // Get the <span> element that closes the modal
        var span = document.getElementsByClassName("close")[0];

        // When the user clicks the button, open the modal 
        for (var i = 0; i < btns.length; i++) {
          btns[i].onclick = function() {
            modal.style.display = "block";
          }
        }

        // When the user clicks on <span> (x), close the modal
        span.onclick = function() {
          modal.style.display = "none";
        }

        // When the user clicks anywhere outside of the modal, close it
        window.onclick = function(event) {
          if (event.target == modal) {
            modal.style.display = "none";
          }
        }
    </script>
            <?php
            if(isset($_SESSION['zalogowany']))
                echo "<p id='bczalogowany'>Jestes Zalogowany Do Panelu Gracza!</p>";
            $servername = "localhost";
            $username = "root";
            $password = "";
            $dbname = "megadrop";

            // Create connection
            $conn = new mysqli($servername, $username, $password, $dbname);
            // Check connection
            if ($conn->connect_error) {
              die("Connection failed: " . $conn->connect_error);
            }

            $sql = "SELECT id, nazwa, opis, cena, komenda, img, zakupiono FROM uslugi";
            $result = $conn->query($sql);

            if ($result->num_rows > 0) {
              // output data of each row
              while($usg = $result->fetch_assoc()) {
                echo "<div class='usg'>";
                    echo "<p class='usg1'><img src='{$usg['img']}' width='75px; height: 75px;'</img></p> <br><br><br>";
                    echo "<p class='usg2'>".$usg['nazwa']."</p>";
                    echo "<p class='usg3'>".$usg['opis']."</p>";
                    echo "<p class='usg4'>".$usg['cena']."</p>";
                    echo "<button class='myBtn'>KUP ".$usg['nazwa']."</button>";
                echo "</div>";
                    }
                }
                $conn->close();
        
        ?>

标签: javascriptphpmysqldatabase

解决方案


推荐阅读