PHP-从1个表中的2个不同数据库输出

时间:2016-12-21 08:25:04

标签: javascript php database html-table output

我目前正在尝试添加搜索功能。 一切都很好。我遇到的唯一问题是我想要搜索一些隐藏的值,这些值位于不同的数据库中。我已经选择了它,但输出不正确(参见下面的截图)

所以有一个客户数据库,它有他们的名字,姓氏和生日。我从合同中选择“EK-Number”,EK-Number是一个唯一的ID Witch告诉我什么车辆他买的。使用那个EK-Number,我进入车辆列表并让他选择特定的车辆。我唯一的问题是他没有从客户那里选择特定的车辆。

Screenshot

文:
Kunden - 客户
Fahrzeugverkauf - 车辆数据库
Kaufverträge合同

我的PHP代码(请不要哭,我知道它的第一点很糟糕,但它仅用于测试):

<table id="table" class="table table-striped">
<?php


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

$sql = "SELECT * FROM `Kunden`";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
     echo "<tr><th>Nachname</th><th>Vorname</th><th>Geburtsdatum</th><th>Straße</th><th>PLZ</th><th>ORT</th><th>E-Mail</th><th>Tel-NR</th><th>Bearbeiten</th></tr>";
     // output data of each row
     while($row = $result->fetch_assoc()) {

            $vorname = $row["Vorname"];
          $nachname = $row["Nachname"];
      $geb = $row["GebDatum"];

         echo "<tr><td>" . $row["Nachname"]. "</td> <td>" . $row["Vorname"]. "</td><td>" . $row["GebDatum"]. "</td><td>" . $row["Strasse"]. "</td><td>" . $row["PLZ"]. "</td><td>" . $row["Ort"]. "</td><td>" . $row["EMail"]. "</td><td>" . $row["TelNr"]. "</td><td><a href='kunden_bearbeiten.php?id=$row[KdNr]&nachname=$row[Nachname]&vorname=$row[Vorname]&geb=$row[GebDatum]&strasse=$row[Strasse]&plz=$row[PLZ]&ort=$row[Ort]&mail=$row[EMail]&Notizen=$row[Notizen]&telnr=$row[TelNr]&vk=$row[VKNR]'>Bearbeiten</a></td></tr>";

     }
     echo "</table>";
} else {
     echo "0 results";
}
?>
    </center>

    <?php


        $conn = new mysqli($servername, $username, $password, $dbname);
        $sql = "SELECT * FROM `kaufvertraege` WHERE Vorname = '$vorname' and Nachname = '$nachname' and Geb = '$geb'";
        $result = $conn->query($sql);

        if ($result->num_rows > 0) {

            // output data of each row
            while($row = $result->fetch_assoc()) {
                $eknr = $row["EkNr"];
            }

        }
?>
    <table class="table table-striped">
            <?php
            $conn = new mysqli($servername, $username, $password, $dbname);
            $sql = "SELECT * FROM `Fahrzeugverkauf` WHERE EKNR = $eknr";
            $result = $conn->query($sql);

            if ($result->num_rows > 0) {
                echo "<tr><th>EK-NR</th><th>VK-Nummer</th><th>EK-Datum</th><th>RG-Nummer</th><th>Marke</th><th>Modell</th><th>Farbe</th><th>EZ</th><th>Motor-Nummer</th><th>FIN</th><th>KM</th><th>VK</th><th>EK-Brutto</th><th>EK-Netto</th><th>VK-Datum</th><th>Bearbeiten</th></tr>";
                // output data of each row
                while($row = $result->fetch_assoc()) {
                    echo "<tr><td>" .$row["EKNR"].  "</td><td>" .$row["VKNummer"].  "</td><td>" . $row["EKDatum"]. "</td> <td>" . $row["RGNummer"]. "</td><td>" . $row["Marke"]. "</td><td>" . $row["Modell"]. "</td><td>" . $row["Farbe"]. "</td><td>" . $row["EZ"]. "</td><td>" . $row["MotorNr"]. "</td><td>" . $row["FIN"]. "</td><td>" . $row["KM"]. "</td><td>" . $row["VK"]. "</td><td>" . $row["EKBrutto"]. "</td><td>" . $row["EKNetto"]. "</td><td>" . $row["VKDatum"]."</td><td><a href='fahrzeuge_bearbeiten.php?id=$row[EKNR]'>link</a></td></tr>";
                    $sql = "SELECT * FROM `Kunden`";
                    $result = $conn->query($sql);

                    if ($result->num_rows > 0) {
                        echo "<tr><th>Nachname</th><th>Vorname</th><th>Geburtsdatum</th><th>Straße</th><th>PLZ</th><th>ORT</th><th>E-Mail</th><th>Tel-NR</th><th>Bearbeiten</th></tr>";
                        // output data of each row
                        while($vk = $result->fetch_assoc()) {

                            $vorname = $row["Vorname"];
                            $nachname = $row["Nachname"];
                            $geb = $row["GebDatum"];

                            echo "<tr><td>" . $vk["Nachname"]. "</td> <td>" . $vk["Vorname"]. "</td><td>" . $vk["GebDatum"]. "</td><td>" . $row["Marke"]. "</td><td>" . $vk["PLZ"]. "</td><td>" . $vk["Ort"]. "</td><td>" . $row["EMail"]. "</td><td>" . $row["TelNr"]. "</td><td><a href='kunden_bearbeiten.php?id=$row[KdNr]&nachname=$row[Nachname]&vorname=$row[Vorname]&geb=$row[GebDatum]&strasse=$row[Strasse]&plz=$row[PLZ]&ort=$row[Ort]&mail=$row[EMail]&Notizen=$row[Notizen]&telnr=$row[TelNr]&vk=$row[VKNR]'>Bearbeiten</a></td></tr>";

                        }
                        echo "</table>";
                    } else {
                        echo "0 results";
                    }
                }

            }

1 个答案:

答案 0 :(得分:0)

是的,因为您是第二次循环遍历Kunden表,但是您引用的是$row['Marke'],其中一些来自Fahrzeugverkauf表。我认为您需要更正变量($row - &gt; $vk)或使用JOIN构建更好的MySQLi查询