在使用回声行时尝试将发送者和接收者与内部联合表分开

Trying to separate sender and receiver from inner joint tables while using echo row

我已经从数据库中检索了整个 table 并拥有所有值,但是由于发送方和接收方在同一个 table 上,我在使用 echo 时遇到了问题在我的 html 页面 table 上显示它们

我试过用

<?php
echo echo  $row["u.username"]
?>

<?php
echo echo  $row["us.username"]
?>

像我在查询中所做的那样将发送者和接收者分开 但我得到这个错误 注意:未定义索引:us.username in C:\xampp\htdocs\otkoth\status.php 如您所见,我必须将我的运输详细信息 id 更改为 sid 才能在 table 中显示它 这是当前输出

<!DOCTYPE html>
<html>

<!--
** Author - Steve Nginyo 
** Project - Courier Services
** Section - Workflow
** Description - Acquiring on transit objects for display from the database
                Product origin and destination are viewed
                Product sender and recipient are also displayed
                Viewing current status of the product is available here
                Products are manually dispatched when placed in cars
                At each checkpoint the product is marked that it has passed
                The last checkpoint is the destination
                The product is the marked arrived when it reaches the destination
-->

<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Speedy Courier Status</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" media="screen" href="status.css" />
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>

<!--
    * Importation of bootstrap online classes is done here
    * links to local css and javascript file is also done here
    * bootstrap helps in styling of the web page content
-->

</head>

<?php

//creation of connection to the database

$conn = mysqli_connect("localhost:9090","root","","courier-services");

/** 
* Check connection or link to the database is done or available display a message
* A message is displayed when the database connection is successful
* An error message is also displayed if the database connection is not successful
*/

if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
  else{
      echo "Worked";
  }

/**
 * Database tables are queried
 * the table contains foreign keys
 * the tables are therefore joined
 * this helps query several databases at the same time
 * the result is then saved in a variable result
 */

  $sql = " SELECT * from shippingdetails s 
  inner join parcel p on s.parcelid = p.id
  inner join offices o on s.officeid = o.id
  inner join offices of on s.destinationid = of.id
  inner join users u on s.senderid = u.id
  inner join users us on s.recepientid = us.id
  inner join vehicle v on s.vehicleid = v.id ";

  $result = $conn->query($sql);

/**
 * This commented code confirms the execution of the query 
 * in case the syntax is correct
 * there may be no syntax error 
 * but the query may not have executed
 */

  /* CODE TO CHECK QUERY EXECUTION
    mysqli_query($conn, $sql);
    $result1 = $conn->query($sql1);
    if ($result){
    echo "no";
    }else{
    echo "yes";
    }
  */
?>

<body>
    <div class="jumbotron">
        <h1 class="display-3">View current status of the product sent</h1>
        <p class="lead">Tracking of the current status by the work flow sections of the project ie. customer care, dispatch, checkpoints and destination</p>
        <hr class="my-2">
        <p>Edit product status on dispatch, arrival and at checkpoints</p>
    </div>
    <div class="container-fluid">
        <?php
        /**
         * Checks whether there is a result set from the database
         * if there is a result set the table is created 
         * the table header is then created here
         * this is before the result set iteration
         */
            if ($result->num_rows > 0) {
        ?>
        <table class="table table-dark table-striped">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Sender</th>
                    <th>Receiver</th>
                    <th>Dispatch</th>
                    <th>Checkpoint</th>
                    <th>Arrival</th>
                    <th>Vehicle</th>
                    <th>Origin</th>
                    <th>Destination</th>
                </tr>
            </thead>
            <tbody>
            <?php
            /**
             * a new iteration without the table header is created
             * the iteration is supposed to display items of the result set
             * the iteration creates a new table row
             * then it inputs the values from the designated table headers
             * into the table columns within the row
             * the iteration happens  for each result in the set creating
             * multiple rows
             */
            // output data of each row
            while($row = $result->fetch_assoc()) {
            ?>          
                <tr>
                    <td><?php echo  $row["sid"]?></td>
                    <td><?php echo  $row["username"]?></td>
                    <td><?php echo  $row["username"]?></td>

                    <td>
                    <?php
                    //checking for the current checkpoint of the product from table
                    if($row["checkpoint"] == 0 ||  $row["checkpoint"] < 0){
                        /**
                         * If the checkpoint is zero it means the product 
                         * has not been dispatched yet
                         */
                        echo "Not dispatched";
                    }else{
                        /**
                         * Otherwise the product has been dispatched
                         * and is enroute to the first checkpoint
                         */
                        echo "Dispatched";
                    }
                    ?>
                    </td>

                    <td><?php echo  $row["checkpoint"]?> of 6</td>

                    <td>
                    <?php
                    //checks for current checkpoint of the product from table
                    if($row["checkpoint"] == 6 ||  $row["checkpoint"] > 6){
                        /**
                         * If the product has reached the sixth checkpoint it means it has arrived
                         * to the final checkpoint
                         * therefore it is marked arrived
                         */
                        echo "Arrived";
                    }else{
                        /**
                         * If the product has yet to arrive at the final checkpoint 
                         * the message of the product not having arrived is displayed
                         */
                        echo "Not yet arrived";
                    }
                    ?>
                    </td>

                    <td><?php echo  $row["platenumber"]?></td>
                    <td><?php echo  $row["cityname"]?></td>
                    <td><?php echo  $row["location"]?></td>
                    <td>
                        <form action="todispatch.php">

                            <!--
                                a hidden input for a form is created with
                                a value of the id of the product from the database
                             -->

                            <input type="hidden" name="dispatch" value="<?php echo  $row["sid"]?>">
                            <?php

                            //checks whether the product has been dispatched

                            if($row["checkpoint"] == 0 ||  $row["checkpoint"] < 0){

                                /**
                                 * if it has yet to be dispatched the button is activated
                                 * the form with the hidden input 
                                 * is redirected to "todispatch.php"
                                 */

                            ?>
                                <button class="btn btn-primary btn-lg" type="submit" method="post">Dispatch</button>
                            <?php
                            }else{

                                /**
                                 * the button is deactivated/disabled if the product has already been dispatched
                                 */

                            ?>
                               <button class="btn btn-primary btn-lg" type="submit" disabled>Dispatch</button>
                            <?php
                            }
                            ?>
                        </form>
                    </td>

                    <td>
                        <form action="tocheckpoint.php">
                            <input type="hidden" name="checkpoint" value="<?php echo  $row["sid"]?>">
                            <?php
                            //checks the current status of the product
                            if($row["checkpoint"] > 0 &&  $row["checkpoint"] < 5){
                                /**
                                 * if the product has not arrived and is dispatched
                                 * the checkpoint button is enabled
                                 * the form redirects to "to checkpoint.php"
                                 */
                            ?>
                                <button class="btn btn-primary btn-lg" type="submit" method="post">Checkpoint</button>
                            <?php
                            }else{
                                /**
                                 * the product is not dispatched 
                                 * therefore cannot go through the checkpoints 
                                 * or the product has arrived
                                 * therefore has gone though all the checkpoints
                                 */
                            ?>
                               <button class="btn btn-primary btn-lg" type="submit" disabled>Checkpoint</button>
                            <?php
                            }
                            ?>
                        </form>
                    </td>

                    <td>
                        <form action="toarrival.php">
                            <input type="hidden" name="arrival" value="<?php echo  $row["sid"]?>">
                            <?php
                            //checks current status of the product
                            if($row["checkpoint"] == 5){
                                /**
                                 * If the product is at the final checkpoint designated
                                 * as 5
                                 * the button for arrival is enabled
                                 * the hidden form with the id of the specific product
                                 * is redirected to "to arrival.php"
                                 */
                            ?>
                                <button class="btn btn-primary btn-lg" type="submit" method="post">Arrival</button>
                            <?php
                            }else{
                                /**
                                 * else the button displayed is disabled
                                 * since the product is yet to arrive to it's
                                 * destination or has already arrived at it's
                                 * destination and marked as so
                                 * checkpoint 6
                                 */
                            ?>
                               <button class="btn btn-primary btn-lg" type="submit" disabled>Arrival</button>
                            <?php
                            }
                            ?>
                        </form>
                    </td>
                </tr>
                <?php
                }
                ?>
            </tbody>
        </table>
        <?php
            } else {
                //in case there are no products within the database
                //this message is displayed instead of the table
            echo "<h3> There are no items currently in transit currently</h3>";
            }
            //the connection to the database is then closed for security purposes
            $conn->close();
        ?>

    </div>
</body>

</html>

我的 table 运输详细信息有来自 4 个不同 table 的 6 个外键 要清楚这个查询确实检索数据 然而,我的问题是将我在加入期间使用的列与在相同 table

中使用的列分开
SELECT * from shippingdetails s 
  inner join parcel p on s.parcelid = p.id
  inner join offices o on s.officeid = o.id
  inner join offices of on s.destinationid = of.id
  inner join users u on s.senderid = u.id
  inner join users us on s.recepientid = us.id
  inner join vehicle v on s.vehicleid = v.id

我希望将买家和接收者分开,然后为起点和终点复制相同的内容,我改为输出城市名称和位置以使其更真实

如果 space 允许,我会这样做作为评论:

你的问题不是很清楚。但是,如果您必须将 table 与其自身连接,则需要在列名上使用别名以确保它们是唯一的。如果我有一个名为 my_table 的 table,其列为 idab 并且我想加入它本身,我可能会这样做:

SELECT t1.a AS a1, t1.b AS b1, t2.a AS a2, t2.b as b2 FROM
my_table t1 JOIN my_table t2 ON t1.id = t2.id
;

以上是一个荒谬的例子,因为我只是为每一行复制相同的值 a1、b1 和 b1 和 b2。但是您可以看到我如何通过使用别名为每一列指定一个唯一的名称。当然,您不必像我那样为两个 table 都使用别名。

这是您想要完成的吗?

根据 Ronald Aaronson 的说法,非常感谢您的反馈 如果它们来自相同的 table,我应该使用 as 重命名我需要的行 它奏效了 这是我必须将查询更改为

$sql = " SELECT sid, p.name, u.username, us.username as ususername, 
            v.platenumber, o.location, 
            of.location as oflocation, 
            checkpoint from shippingdetails as s
  join parcel as p on s.parcelid = p.id
  join offices as o on s.officeid = o.id
  join offices as of on s.destinationid = of.id
  join users as u on s.senderid = u.id
  join users as us on s.recepientid = us.id
  join vehicle as v on s.vehicleid = v.id ";

因此我的 echo 语句必须看起来像

发件人和收件人

<?php echo  $row["username"]?>
<?php echo  $row["ususername"]?>

目的地和起点

<?php echo  $row["location"]?>
<?php echo  $row["oflocation"]?>

如果有人需要更多帮助,他们可以参考此 link 以获得更多见解 https://www.phpknowhow.com/mysql/joins/