0

我在 PHP 中创建了一个带有组合框的 html 表,用于检索 mysql 数据库表的值。

这是我的代码:

<?php
    include_once 'indeling/header.php';

print '<a href="overzicht_notass.php" class="button">Overzicht nota\'s</a> <br /><br />';

$sql = "
SELECT
    notas.id AS notaid
    , notas.klantid AS klantid
    , notas.bedrag AS bedrag
    , notas.datum AS datum
    , contacten.bedrijf AS bedrijf
    , contacten.adres AS adres
    , contacten.woonplaats AS woonplaats
FROM
    notas
    LEFT JOIN contacten 
        ON (notas.klantid = contacten.id);
";

function bedrijven($mysqli) {

$sqlbedrijven = "
SELECT 
    id
,   bedrijf
,   adres
,   woonplaats
FROM
    contacten
    ORDER BY bedrijf ASC
";

    $resultbedrijven = $mysqli->query($sqlbedrijven); 
    if (!$resultbedrijven) {
        echo "something went wrong: (" . $mysqli->error .")";
    }
        echo "<select name = klantid>\n";
        while ($row = $resultbedrijven->fetch_assoc()) {
echo <<<opt
<option value="{$row['id']}"> {$row['bedrijf']} - {$row['adres']} - {$row['woonplaats']} </option>

opt;
        }
        echo "</select>\n";
}


$result = $mysqli->query($sql); 
    if (!$result) {
        echo "Oeps hier gaat iets fout: (" . $mysqli->error .")";
    }
    else {
        printf("Er zijn momenteel %d nota's.<br />", $result->num_rows);

echo "
<table>
    <tr>
        <th>notanummer.</th>
        <th>bedrijf</th>
        <th>bedrag</th>
        <th>datum</th>
        <th>bewerken</th>
    </tr>
";

    while ($row = $result->fetch_assoc()) {
        echo '<tr> <form action="overzicht_relaties_bewerken.php" method="post">
        <td> <input type="text" class="short" name="notaid" value="' . $row['notaid'] . '"></td>
        <td> ' . bedrijven($mysqli) . ' </td> 
        <td> <input type="text" name="bedrag" value="' . $row['bedrag'] . '"></td>
        <td> <input type="date" name="datum" value="' . $row['datum'] . '"></td>
        <td> <input type="submit" name="update" value="aanpassen" class="button">' . '<br />
        <input type="submit" name="delete" value="verwijderen" class="button"' . '"></td>
        </tr></form>
        ';
    }
        echo "</table>";
}

?>

一切正常,但我看到表格上方的组合框,而不是第二个 TD bedrijven($mysqli)

这是网页的 mij html 源代码的转储:

<a href="overzicht_notass.php" class="button">Overzicht nota's</a> <br /><br />Er zijn momenteel 2 nota's.<br />
<table>
    <tr>
        <th>notanummer.</th>
        <th>bedrijf</th>
        <th>bedrag</th>
        <th>datum</th>
        <th>bewerken</th>
    </tr>
<select name = klantid>
<option value="37"> afsdf - fasdf12 - Klarenbeek - (Gelderland) </option>
<option value="36"> afsdf - fasdf12 - Klarenbeek - (Gelderland) </option>
<option value="38"> afsdf2 - fdas - Klarenbeek </option>

现在我们有一堆更多的选项值,然后:

</select>
<tr> <form action="overzicht_relaties_bewerken.php" method="post">
        <td> <input type="text" class="short" name="notaid" value="2"></td>
        <td>  </td> 
        <td> <input type="text" name="bedrag" value="125.50"></td>
        <td> <input type="date" name="datum" value="2013-06-04"></td>

tabledata id 下的表数据为空...?

因此,我在正确的位置(第二个 TD)调用该函数,但在其他地方显示组合框。

知道这怎么可能吗?

4

1 回答 1

1

我不擅长 php 但我认为你的select标签不在table!看看我是如何在 html 中做到这一点的:

<table>
<tr>
    <th>notanummer.</th>
    <th>bedrijf</th>
    <th>bedrag</th>
    <th>datum</th>
    <th>bewerken</th>
</tr>
<tr>
    <td colspan='5'>
        <select name = klantid>
        <option value="37"> afsdf - fasdf12 - Klarenbeek - (Gelderland) </option>
        <option value="36"> afsdf - fasdf12 - Klarenbeek - (Gelderland) </option>
        <option value="38"> afsdf2 - fdas - Klarenbeek </option>
        </select>
    </td>
</tr>

<tr> <form action="overzicht_relaties_bewerken.php" method="post">
    <td> <input type="text" class="short" name="notaid" value="2"></td>
    <td>  </td> 
    <td> <input type="text" name="bedrag" value="125.50"></td>
    <td> <input type="date" name="datum" value="2013-06-04"></td>

所以改变你的PHP代码:

    echo "<select name = klantid>\n";
    while ($row = $resultbedrijven->fetch_assoc()) {
    echo <<<opt
    <option value="{$row['id']}"> {$row['bedrijf']} - {$row['adres']} -       {$row['woonplaats']} </option>

    opt;
    }
    echo "</select>\n";

至:

    echo "<tr><td colspan='5'><select name = klantid>\n";
    while ($row = $resultbedrijven->fetch_assoc()) {
    echo <<<opt
    <option value="{$row['id']}"> {$row['bedrijf']} - {$row['adres']} -       {$row['woonplaats']} </option>

    opt;
    }
    echo "</select></td></tr>\n";
于 2013-08-10T07:57:13.723 回答