简体   繁体   中英

PHP/MySQL: Displaying data from different tables based on a users ID

I have two tables "personal_trainer" and "training_plan" . PersonalTrainerID is a foreign key in training_plan. I want to display that when a trainer logs in with their email and password, only the training plans that apply to that ID appears.

However, I am having trouble understanding the logic. I have coded it so that all the information from the training_plan table appears, I cannot created it such that only the rows that apply to the ID are visible to the user. I have made this by the simple sql statement "SELECT * from training_plan" . There is a filter textbox to search the table that doesn't effect the code if you're wondering.

I have commented the code to try make it easier to understand. Any help would be greatly appreciated!

           <?php
       if (isset($_POST['search'])) /*This code allows the filter textbox to search the db */
       {
           $valueToSearch = $_POST['ValueToSearch'];
           $query = "select * from training_plan WHERE concat('trainingPlanID', `personalTrainerID`, `clientID`, `trainingType`, `exercise1`, `exercise2`, `exercise3`, `exercise4`, `exercise5`, `exercise6`, 'reps', 'sets', 'description')like'%".$valueToSearch."%'";
           $search_result = filterTable($query);

       }
       else {
           $query = "SELECT * from training_plan WHERE PersonalTrainerID= (SELECT personalTrainerID FROM personal_trainer WHERE email=$_SESSION['user'])"; /*The error that is displayed is 'syntax error, unexpected string after ['*/
           $search_result = filterTable($query);
       }

       function filterTable($query)
       {
           $connect = mysqli_connect("localhost:3308","root","","fypdatabase");
           $filter_Result = mysqli_query($connect, $query);
           return $filter_Result;
       }
       ?>



       <?php /*This displays the data in a table but so far outputs all of the table data */
       while($row = mysqli_fetch_array($search_result))
    {
        ?>
                <tr>
                <td><?php echo $row["trainingPlanID"]; ?></td>
                <td><?php echo $row["personalTrainerID"]; ?></td>
                <td><?php echo $row["clientID"]; ?></td>
                <td><?php echo $row["trainingType"]; ?></td>
                <td><?php echo $row["exercise1"]; ?></td>
                <td><?php echo $row["exercise2"]; ?></td>
                <td><?php echo $row["exercise3"]; ?></td>
                <td><?php echo $row["exercise4"]; ?></td>
                <td><?php echo $row["exercise5"]; ?></td>
                <td><?php echo $row["exercise6"]; ?></td>
                <td><?php echo $row["reps"]; ?></td>
                <td><?php echo $row["sets"]; ?></td>
                <td><?php echo $row["description"]; ?></td>
                <td>
                    <a href="?Delete=<?php echo $row["trainingPlanID"]; ?>" onclick="return confirm('Are you sure?');">Delete</a>
                </td>
                <td>
                    <a href="updateTplan.php?Edit=<?php echo $row["trainingPlanID"]; ?>" onclick="return confirm('Are you sure?');">Update</a>
                </td>
              </tr>
              <?php 

将查询更改为:

$query = "SELECT * from training_plan WHERE PersonalTrainerID = (SELECT personalTrainerID FROM personal_trainer WHERE email='".$_SESSION['user']."')";

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