如何通过另一个查询在bind_result中插入变量?

时间:2018-09-11 20:11:41

标签: php html sql

我正在PHP和SQL之间建立此连接以编辑表的记录。我正在下拉列表以进行最佳化(旅行目的地),但是我坚持使用这段代码,而且我不知道如何实现下面的两个代码并将它们相互混合。

我希望用于最佳化的SQL代码(数字2)位于所有ID的下拉列表中。现在,它仅显示所选行的ID。我想用查询代码(第2个)分开。

这是代码1:

// make sure the 'id' value is valid
if (is_numeric($_GET['id']) && $_GET['id'] > 0) {
    // get 'id' from URL
    $id = $_GET['id'];
    // get the recod from the database
    if($stmt = $mysqli->prepare("    
            SELECT
            k.naam 
            ,k.adres 
            ,r.bestemming 
            ,b.klasse 
            ,b.prijs_in_euro 
            ,b.datum 
            FROM klant k INNER JOIN bestelling b ON k.klant_code = b.klant_code 
            INNER JOIN reis r ON b.reis_code = r.reis_code AND k.klant_code=?")) {
        $stmt->bind_param("i", $id);
        $stmt->execute();
        $stmt->bind_result($name, $adres, $bestemming, $klasse, $prijs, $datum);
        $stmt->fetch();
        // show the form
        renderForm($datum,$name,$adres,$bestemming,$klasse,$prijs,NULL,$id);

        $stmt->close();
    }

这是代码2:

if($stmt = $mysqli->query("select bestemming from reis")) {
    $stmt->bind_param("i", $id);
    $stmt->execute();
    $stmt->bind_result($bestemming);
    $stmt->fetch();
    // show the form
    renderForm($datum,$name,$adres,$bestemming,$klasse,$prijs,NULL,$id);
    $stmt->close();
}

这是renderForm函数代码:

<?php
/*
Allows the user to both create new records and edit existing records
*/

// connect to the database
include("connect-db.php");

// creates the new/edit record form
// since this form is used multiple times in this file, I have made it a function that is easily reusable
function renderForm($d = '', $n = '', $a ='', $b = '', $k ='', $p ='', $error = '', $id = '')
{ ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <title>
            <?php if ($id != '') { echo "Bewerk Record"; } else { echo "Nieuw Record"; } ?>
        </title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    </head>
    <body>
        <h1><?php if ($id != '') { echo "Bewerk Record"; } else { echo "Nieuw Record"; } ?></h1>
        <?php if ($error != '') {
        echo "<div style='padding:4px; border:1px solid red; color:red'>" . $error . "</div>";
        } ?>
        <form action="" method="post">
            <div>
                <?php if ($id != '') { ?>
                    <input type="hidden" name="id" value="<?php echo $id; ?>" />
                    <p>ID: <?php echo $id; ?></p>
                <?php } ?>
                <strong>Datum: *</strong> <input type="text" name="datum" value="<?php echo $d; ?>"/><br/>
                <strong>Volledige naam: *</strong> <input type="text" name="name" value="<?php echo $n; ?>"/><br/>
                <strong>Woonplaats: *</strong> <input type="text" name="adres" value="<?php echo $a; ?>"/><br/>
                <strong>Bestemming: *</strong> <select><option name="bestemming"> <?php echo $b; ?> </option></select><br/>
                <select>
                    <option>-- Selecteer bestemming --</option>
                        <?php foreach ($resultBestemming as $output) {?>
                            <option><?php echo $output["bestemming"];?></option>
                        <?php }?>
                </select>
                <strong>Klasse: *</strong> <input type="text" name="klasse" value="<?php echo $k; ?>"/><br/>
                <strong>Prijs in euro: *</strong> <input type="text" name="prijs" value="<?php echo $p; ?>"/>
                <p>* Verplicht</p>
                <input style="padding:10px;margin-left:60px;" type="submit" name="submit" value="Verzend" />
            </div>
        </form>
    </body>
</html>

<?php }

0 个答案:

没有答案