简体   繁体   中英

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

I know a lot of HTML and CSS, but still learning PHP. This is what I've come up with. I need the user to submit code# and it will find a value in the database. This is my 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"];
}}

now I can just echo $firstname;

what I need it to find other values that have the same "relate" field like the one I just pulled. So if I add relate to the information I'm getting.

$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"];
}}

Then I launch another database search:

$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>";
}
}

I need the user to be able to select the values. That's why I'm also trying to add check marks by the new values. How could I make this work right?

You have syntax errors. That last block should be:

# 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
    }
}

Also, I suspect you really want that to be a radio and not a checkbox. Checkboxes allow multiple selections.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM