简体   繁体   中英

My query won't work (it does in a wrong way) because of a script!? But why? SESSIONS and PHP

So I have to make a webapp for school. U can use it to declare projects costs, names, other properties, login etc and I scripted a way to sort projects by the user in de dropdown menu.

So, in my script, it looks what the value is of the drowpdown menu and puts it in the query and that is why I supposed to see the 'projects' only of that particular user with his/hers unique code (getting it out of the dropdown value!) .

I looked up the value of the dropdown menu with jQuery in the Google Chrome Console ( the jQuery line: $(".li.li-primary-1 option:selected").val(); ) and it gives me the right value.

But whenever I execute my query I always get the wrong one with the code '1007'.

I tried so many different ways wihtout succes. Could someone possibly point out the piece where it is totally wrong. Need some explanation. Thanks in advance! :)

    <img src="assets/logo.png" width="150" style="margin-right: auto; position:static; display:block; margin-top: 0.5%;margin-left: 0.5%;">
    <span class="echo-user"> <?php echo $_SESSION['name'];?>,  <?php  echo $_SESSION['consultantcode']; ?>
        <br><div class="btn-group btn-group-lg">
            <a href="uitloggen.php"><button type="submit" name="dangerbutton" class="btn btn-danger" id="btn-btn-danger-1">Uitloggen</button></a>
        </div>  
    </span>
</div>
<div class="decla">

<h2>Declarations per project</h2>

             <form id="content-project" name="search" method="post">
                <div class="field">
                    <label>Choose project:</label> 
                    <select name="peruser" class="li li-primary-1">
                        <?php 
                        $query = "SELECT * FROM consultant";
                        $q = $db->prepare($query);
                        $q->execute();
                        $qry = $q->fetchAll(PDO::FETCH_ASSOC);

                         foreach ($qry as $qe) {
                        echo "<option value='" .$qe['consultantcode']. "'>".$qe['naam']."</option>";

                        }
                        ?>
                        </select>

                    <br /><input class="btn btn-default" style="width: 20%; margin-left: 20.5%;" type="submit" name="submit" value="Zoeken" />
                    <a href="adminpage.php"><button class="btn btn-primary">Terug</btn></a>
                </div>
             </form>
        </p>

                        <?php
                        if (isset($_POST['submit'])) {
                            ?> 
                            <p>
        <center>
                <table class='declaraties' style='border-collapse: collapse'>
                    <thead>
                        <tr>
                            <th><b>Declaratie code </b></th>
                            <th><b>Project code </b></th>
                            <th><b>Project naam </b></th>
                            <th><b>Omschrijving</b></th>
                            <th><b>Kosten code </b></th>
                            <th><b>Datum</b></th>
                            <th><b>Consultant code </b></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php

                        $cc = $_SESSION['consultantcode'];
                            $query = 'SELECT consultant.consultantcode, consultant.naam,
                            declaratie.declaratiecode, declaratie.kostencode, declaratie.datum,
                            project.projectnaam, project.projectcode, kosten.omschrijving,
                            kosten.kostencode, consultant.naam, consultant.consultantcode
                            FROM declaratie
                            INNER JOIN project
                            ON project.projectcode = declaratie.projectcode
                            INNER JOIN kosten
                            ON kosten.kostencode = declaratie.kostencode
                            INNER JOIN consultant
                            ON consultant.consultantcode = declaratie.consultantcode
                            WHERE declaratie.consultantcode ="'.$qe['consultantcode'] .'"';
                            $data = $db->prepare($query);
                            $data->execute(array());
                            $rows = $data->fetchAll(PDO::FETCH_ASSOC);

                            foreach($rows as $row)
                            {
                                echo "<tr><td>"  
                                . $row['declaratiecode'] . "</td><td>"
                                . $row['projectcode'] . "</td><td>" 
                                . $row['projectnaam'] . "</td><td>" 
                                . $row['omschrijving'] . "</td><td>"
                                . $row['kostencode'] . "</td><td>"
                                . $row['datum'] . "</td><td>" 
                                . $row['consultantcode'] . "</td><td>" 
                                .  "</td></tr>";


                            }
                                // echo "<td style='margin-top:2%;'>" . $qewat. "<br>" . $qewa . "</td>";
                        }
                        ?>
                    </tbody>
                </table>
            </center>
            </p>
</div>

<div class="modal fade declaraties-modal-email" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-lg">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
        <h4 class="modal-title">Mijn declaraties</h4>
      </div>
      <div class="modal-body">
        <p>
        <center>
                <table class='declaraties' style='border-collapse: collapse'>
                    <thead>
                        <tr>
                            <th><b>Declaratie code &nbsp;</b></th>
                            <th><b>Project naam &nbsp;</b></th>
                            <th><b>Project code &nbsp;</b></th>
                            <th><b>Kosten code &nbsp;</b></th>
                            <th><b>Datum</b></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php include_once 'mijndeclaratie.php';?>
                    </tbody>
                </table>
            </center>
            </p>
      </div>
      <!-- <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div> -->
    </div><!-- /.modal-content -->

<script type="text/javascript"> 
    $('.btn.btn-danger.1').on("click", function() {
        alert('U bent uitgelogd!');
    });    
</script>

You are selecting the last result in your option dropdown loop.

WHERE declaratie.consultantcode ="'.$qe['consultantcode'] .'"';

That should be

WHERE declaratie.consultantcode ="'.$_POST['peruser'] .'"';

And it should also be sanitized, but for this question, this is why you are getting the wrong result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM