将两个会话变量添加到MYSQL查询中

时间:2017-05-23 22:57:25

标签: php forms post mysqli

如何在我的MYSQL查询中添加2个会话变量?

这很有效,因为我只使用变量$stad

session_start();
$_SESSION['stad']   = $_POST['stad'];

//Database connectie
$link = mysqli_connect("*", "*", "*", "entour_nl") or die("Error ". mysqli_error($link));

//Query //
$query = "SELECT * FROM Fissa_test WHERE stad='".$_SESSION['stad']."' AND datum >= CURDATE() ORDER BY datum";

$result = mysqli_query($link, $query);

//loop door de resultaten
while($row = mysqli_fetch_assoc($result)){
?>

<article class="fissa-item">
    <div class="left-side">
        <img src="<?php echo $row['flyer_url'];?>" href="<?php echo $row['flyer_url'];?>" height="100%" width="100%">
    </div>


    <div class="right-side">
        <h3 class="naam-feest" style="margin-top: 1px;"><?php echo $row['naam_feest'];?></h3>
        <h5 class="line-up"><?php echo $row['line_up'];?></h5>
        <h4 class="info"><?php echo $row['plaats'];?> | <?php echo date("d-m-Y", strtotime($row['datum']));?> | <?php echo $row['tijd'];?> | <?php echo $row['leeftijd'];?></h4>
        <a href="<?php echo $row['ticket_url'];?>"><button class="tickets">KOOP TICKETS</button></a>
    </div>

</article>
<?php
}
?>
<div class="footer">
    <button class="filter" onclick="openNav()">FILTER</button>
</div>

<div id="myNav" class="overlay">
  <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
  <div class="overlay-content">
    <form action="lit_stad_leeftijd.php" method="post">
        <button name="leeftijd" value="14+">14+</button>
        <button name="leeftijd" value="15+">15+</button>
        <button name="leeftijd" value="16+">16+</button>
        <button name="leeftijd" value="17+">17+</button>
        <button name="leeftijd" value="18+">18+</button>
        <button name="leeftijd" value="21+">21+</button>
        <button name="leeftijd" value="23+">23+</button>
        <button name="leeftijd" value="*">ALL</button>
    </form>
  </div>
</div>

但是当我加入$leeftijd时。该查询不起作用。

lit_stad_leeftijd.php

session_start();
$_SESSION['leeftijd']   = $_POST['leeftijd'];
$_SESSION['stad']       = $_POST['stad'];

//Database connectie
$link = mysqli_connect("*", "*", "*", "*") or die("Error ". mysqli_error($link));

//Query //
$query = "SELECT * FROM Fissa_test WHERE stad='".$_SESSION['stad']."' AND leeftijd='".$_SESSION['leeftijd']."' AND datum >= CURDATE() ORDER BY datum";

$result = mysqli_query($link, $query);

//loop door de resultaten
while($row = mysqli_fetch_assoc($result)){
    ?>

    <article class="fissa-item">
        <div class="left-side">
            <img src="<?php echo $row['flyer_url'];?>" href="<?php echo $row['flyer_url'];?>" height="100%" width="100%">
        </div>

        <div class="right-side">
            <h3 class="naam-feest" style="margin-top: 1px;"><?php echo $row['naam_feest'];?></h3>
            <h5 class="line-up"><?php echo $row['line_up'];?></h5>
            <h4 class="info"><?php echo $row['plaats'];?> | <?php echo date("d-m-Y", strtotime($row['datum']));?> | <?php echo $row['tijd'];?> | <?php echo $row['leeftijd'];?></h4>
            <a href="<?php echo $row['ticket_url'];?>"><button class="tickets">KOOP TICKETS</button></a>
        </div>
    </article>
    <?php
}
?>
<div class="footer">
    <button class="filter" onclick="openNav()">FILTER</button>
</div>

<div id="myNav" class="overlay">
  <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
  <div class="overlay-content">
    <form action="lit_stad_leeftijd.php" method="post">
        <button name="leeftijd" value="14+">14+</button>
        <button name="leeftijd" value="15+">15+</button>
        <button name="leeftijd" value="16+">16+</button>
        <button name="leeftijd" value="17+">17+</button>
        <button name="leeftijd" value="18+">18+</button>
        <button name="leeftijd" value="21+">21+</button>
        <button name="leeftijd" value="23+">23+</button>
        <button name="leeftijd" value="*">ALL</button>
    </form>
  </div>
</div>

我收到的错误消息:

警告:session_start():无法发送会话cookie - 已经/客户发送的标题(/customers/9/f/7/website.nl/httpd.www/fissa/lit_stad_leeftijd.php:16中的输出)第21行/9/f/7/website.nl/httpd.www/fissa/lit_stad_leeftijd.php警告:session_start():无法发送会话缓存限制器 - 已发送的标头(输出从/ customers / 9 / f / 7开始) /website.nl/httpd.www/fissa/lit_stad_leeftijd.php:16)在/customers/9/f/7/website.nl/httpd.www/fissa/lit_stad_leeftijd.php第21行注意:未定义的索引:stad in第22行/customers/9/f/7/website.nl/httpd.www/fissa/lit_stad_leeftijd.php注意:未定义索引:stad in /comersomers/9/f/7/website.nl/httpd.www/fissa第32行的/lit_stad_leeftijd.php FILTER×

2 个答案:

答案 0 :(得分:0)

为什么要将值设置为$_SESSION变量?你可以做到

$query = "SELECT * 
    FROM Fissa_test 
    WHERE stad='" . $_POST['stad'] . "' 
    AND leeftijd='" . $_POST['leeftijd'] . "' 
    AND datum >= CURDATE() ORDER BY datum";

如果该变量仍未添加到查询中,则问题可能是您没有正确地将数据发布到此页面。

答案 1 :(得分:0)

您发布的错误消息表示您已在数据已发送到输出缓冲区后调用session_start()(因此发送了标头信息)。

PHP documentation for session_start()有一个注释:

  

要使用基于cookie的会话,必须在向浏览器输出任何内容之前调用session_start()。

这意味着必须在任何“HTML输出”之前调用session_start()。这包括空格等。简单的解决方案是简单地将session_start()作为文件中的第一行代码。

<?php
session_start();
?>
<html>
    <body>
        [...]
    </body>
</html>