如何从数据库中取出与用户选择的值相关的值?

How can I take out values from database that are related to the value the user picks?

我知道很多 HTML 和 CSS,但仍在学习 PHP。这就是我想出的。我需要用户提交 code#,它会在数据库中找到一个值。这是我的代码:

$invitecode = $_GET['invitecode'];
$isattend = $_GET['attend'];
$isphone = $_GET['phone'];
$isemail = $_GET['email'];

$sql = "SELECT firstname, lastname FROM guests WHERE code = $code";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $firstname = $row["firstname"];
        $lastname = $row["lastname"];
}}

现在我可以 echo $firstname;

我需要它来查找与我刚刚提取的字段具有相同 "relate" 字段的其他值。因此,如果我添加与我获得的信息相关的内容。

$sql = "SELECT firstname, lastname, relate FROM guests WHERE code = $code";
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
    $firstname = $row["firstname"];
    $lastname = $row["lastname"];
    $relate = $row["relate"];
}}

然后我启动另一个数据库搜索:

$sql = "SELECT firstname, lastname, code FROM guests WHERE relate = $relate";
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
    $first = $row["firstname"];
    $last = $row["lastname"];
    $code = $row["code"];
    echo " ?><input type="checkbox" name="add" value="<?php echo $code; ?>"><?php You are " . $first . " " . $last . "<br>";
}
}

我需要用户能够 select 这些值。这就是为什么我也试图通过新值添加复选标记。我怎样才能使这项工作正常进行?

您有语法错误。最后一个块应该是:

# This is a great way to get hacked.
# http://php.net/manual/en/security.database.sql-injection.php
$sql = "SELECT firstname, lastname, code FROM guests WHERE relate = $relate";
$result = mysqli_query($conn, $sql);
if ( mysqli_num_rows($result) > 0 ) {
    // output data of each row
    while ( $row = mysqli_fetch_assoc($result) ) {
        $first = $row["firstname"];
        $last = $row["lastname"];
        $code = $row["code"];
        ?>
        <input type="checkbox" name="add" value="<?php echo $code ?>"> You are "<?php echo $first ?>" "<?php echo $last ?><br>
        <?php
    }
}

此外,我怀疑您真的希望它成为收音机而不是复选框。复选框允许多项选择。