php - for循环仅获取mysql结果中的第一个值

时间:2018-06-15 14:11:44

标签: php html css mysql

在这段代码中,我尝试创建与查询结果行数一样多的模态框。这有效!然后我想用数据库中的数字$row['seats_no']填充这些模态框。这里我们有一个问题..在每个模态框中,它创建与第一个相同数量的复选框。我的意思是$row['seats_no']具有每行第一行的数字。

<?php
    session_start();
    if(!isset($_SESSION['name']) && !isset($_SESSION['email']) && !isset($_SESSION['id']) && !isset($_SESSION['cash'])){
        header("location: Start.php");
    }
    $name=trim($_SESSION['name']);
    $email=trim($_SESSION['email']);
    $dbc = mysqli_connect("localhost","root","","my_db");
    // Check connection
    if (mysqli_connect_errno()){
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    mysqli_query($dbc,"SET NAMES 'utf8'");
    mysqli_query($dbc,"SET CHARACTER SET 'utf8'");

    if (isset($_POST['buy']) && isset($_POST['consert_id'])){
        $q = "SELECT ticket_id, section, cost, seats_no FROM tickets INNER JOIN concert ON tickets.consert_id=concert.concert_id WHERE concert.concert_id=".$_POST['consert_id'];
        //ticket_id, concert_id, section, cost, seats_no, concert_id,concert_name, date, place, category, description
        $r = mysqli_query($dbc,$q);
        $num= mysqli_num_rows($r);
        $q1 = "SELECT * FROM concert WHERE concert_id=".$_POST['consert_id'];
        $r1 = mysqli_query($dbc,$q1);
        $first = mysqli_fetch_assoc($r1)
?>
<!DOCTYPE html>
<html>
<title>Seats</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link type="text/css" rel="stylesheet" href="seats_style.css">
<body>
<div style="z-index:1;position:fixed;">
<ul>
    <li><a href="Concerts.php" class="active">E-Viva</a></li>
    <li><a href="cash.php">Ανανεωση υπολοιπου</a></li>
</ul>
</div>
<div class="background">
    <div class="transbox">
        <div class="w3-container">
            <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/199011/concert.png">
            <h1><?=$first['concert_name']?></h1>
            <div style="left:0;"><?=$first['date']?></div>
            <div style="right:0;"><?=$first['place']?></div>
            <p><?=$first['description']?></p>
          <?php
            while ($row = mysqli_fetch_array($r)) {
            ?>
            <div style="margin-left:5em;padding:5px;"><?=$row['section']?></div>
            <div style="margin-left:5em;padding:5px;">Τιμή <?=$row['cost']?>€</div>
            <div style="margin-left:5em;padding:5px;"><button onclick="document.getElementById('id01').style.display='block'" class="w3-button w3-black">Κράτηση</button></div>

          <div id="id01" class="w3-modal" background="black">
            <div class="w3-modal-content">
              <div class="w3-container">
                <span onclick="document.getElementById('id01').style.display='none'" class="w3-button w3-display-topright">&times;</span>
                <p><h2>Διαλεξε τις θεσεις που θες</h2></p>
                <form action="buy.php" method="post">
                <table>
          <?php
          $num=0;
            for($j=1; $j<=10; $j++){
                echo "<tr>";
                for($i=1; $i<= $row['seats_no']/10; $i++){
                    $num++;
                    echo "<td><input class='seat' type=\"checkbox\" name=\"s[]\" id=\"".$num."\" value=\"".$num."\"><label for=\"".$num."\">".$num."</label><input type='hidden' name='ticket_id' value='".$row['ticket_id']."'><input type='hidden' name='cost' value='".$row['cost']."'></td>";
                }
                echo "</tr>";
            }
            echo "</table><input class=\"ok\" type=\"submit\" name=\"ok\" value=\"Κράτηση\"></form></div></div></div><br>";
            }
            }
            mysqli_free_result($r1);
            mysqli_free_result($r);
            mysqli_close($dbc);
        ?>
        </div>
    </div>
</div>
</body>
</html>

here are the results i get from executing my query at phpMyAdmin

Here are the buttons I create to open the modals

And here is the modal from the last button i clicked that in the database shows it should be 60 checkboxes but it shows 180

1 个答案:

答案 0 :(得分:0)

Κατερίνα, 尝试使用while ($row = mysqli_fetch_array($r)) {...}循环更改foreach
foreach( mysqli_fetch_array($r) as $row ) {....}一样 只是一个想法 此外,您过早关闭一个括号,在第一个mysqli_free_result($r1)之前删除一个,并在文件末尾的结束html标记</html>后关闭它。
另外,正如@tadman所说,做一些研究并使用参数化查询。你的代码非常容易受到MySQL注入,你将面临几个问题。据我所知,该页面与付款或预付款有关。

无论如何,我希望我能帮助你 Καλήεπιτυχία;)