获取基于另一个表中的Table_Id的预订ID当天SQL查询接收按日期和表号唯一的订单

时间:2019-01-23 15:48:46

标签: php sql database html5 forms

如果我运行以下查询:

//The statement I get with an Empty reservation_ID
$sql = "SELECT O.Res_Datum, O.Res_ID, O.Tafel_Id,O.ReceiptID, SUM(MI.ItemPrice) AS TotalReceiptPrice
FROM Orders AS O INNER JOIN MenuItem AS MI ON O.MenuItemID = MI.MenuItemID
GROUP BY O.Res_Datum, O.Res_ID, O.Tafel_Id  ";

我必须manually input the reservation_id in my orders table如何从预定表到订单表中获取与表和预定日期相关的reservation_id?

所以我只需要输入表号,日期和menuItem,我的查询就知道what is related to(reserveringen.Tafel_Id)的table number {1}}到on that date

我尝试运行related查询,但是没有用。 您可以在代码left join中找到它。 并在文件中:commented out

我希望我很清楚,我发现很难对此进行解释和重新编辑。我希望当您运行文件时,您会明白我的意思。 如果我的英语不是最好,也请原谅。我不是英语母语人士。谢谢。

这是我的数据库:

bestelling.php

Bestelling.php:

MenuItem: 
    MenuItemID  int(11)         
    ItemName    varchar(255)        
    ItemPrice   double

orders:
 OrderID    int(11)         
 MenuItemID int(11)         
 ReceiptID  int(11)         
 Res_Datum  date            
 Tafel_Id   int(11)         
 Res_ID     int(11) 


receipt:
ReceiptID       int(11)         
ReceiptPrice    double

reserveringen:  
    Reservering_Id  int(11)         
    Tafel_Id        int(11)         
    VoorNaam        varchar(255)            
    AchterNaam      varchar(255)            
    TelefoonNummer  varchar(255)        
    Email           varchar(255)        
    Res_Datum       date

tafels:
    Tafel_Id        int(11)         
    tafel_Nummer    int(11)         
    Aantal_Personen int(11) 

Maak_bestelling.php

<form action="/restaurant/maak_bestelling.php" method="POST">
      <h2>Enter Order</h2>

      Table Number:<br>
      <input type="text" name="tafelnummer" value=""><br><br>
      Receipt Id:<br>
      <input type="text" name="receiptid"   value=""><br><br>
      Menu_Item:<br>
      <input type="text" name="menu_item"   value=""><br><br>
      Date: <br>
      <input type="date" name="date"        value=""><br><br>

      <input type="submit" value="Submit">
    </form>

<h2>Pending Orders:</h2>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "restaurant";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

//MY Query i am trying to run
// $sql = "SELECT O.Res_Datum, R.Reservering_Id, O.Tafel_Id,O.ReceiptID, SUM(MI.ItemPrice) AS TotalReceiptPrice
// FROM Orders, Reserveringen AS O INNER JOIN MenuItem AS MI ON O.MenuItemID = MI.MenuItemID
// AS O LEFT JOIN Reservering_Id AS R on O.Reservering_Id = R.reservering_Id
// GROUP BY O.Res_Datum, O.Res_ID, O.Tafel_Id  ";

//The statement I get with an Empty reservation_ID
$sql = "SELECT O.Res_Datum, O.Res_ID, O.Tafel_Id,O.ReceiptID, SUM(MI.ItemPrice) AS TotalReceiptPrice
FROM Orders AS O INNER JOIN MenuItem AS MI ON O.MenuItemID = MI.MenuItemID
GROUP BY O.Res_Datum, O.Res_ID, O.Tafel_Id  ";


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

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        echo "Res_datum: ". $row["Res_Datum"]. " ReservationID : " . $row["Res_ID"]. " - Table_Number: " . $row["Tafel_Id"]. " Total Order Price: " . $row["TotalReceiptPrice"]." ". "<br>";
    }
} else {
    echo "0 results";
}

mysqli_close($conn);
?>


</div>

0 个答案:

没有答案