1

I have a PHP Crud system. One of the options when creating a new record is a selection box with two options. Netman and Administrator. When I update the record I see the option, but it duplicates and it has the other option, like thisDuplicate selection. I cant see how I can show the record from the DB and the other option. Is this possible?

    <?php
    include "config.php";
    include "header.php";
    if(isset($_GET['u'])):
    if(isset($_POST['bts'])):
    $stmt = $mysqli->prepare("UPDATE passwordtable SET school=?,usertype=?, password=?, notes=? WHERE id_personal=?");
    $stmt->bind_param('sssss', $nm, $gd, $tl, $ar, $id);

    $nm = $_POST['nm'];
    $gd = $_POST['gd'];
    $tl = $_POST['tl'];
    $ar = $_POST['ar'];
    $id = $_POST['id'];

    if($stmt->execute()):
    echo "<script>location.href='index.php'</script>";
    else:
    echo "<script>alert('".$stmt->error."')</script>";
    endif;
    endif;
    $res = $mysqli->query("SELECT * FROM passwordtable WHERE id_personal=".$_GET['u']);
    $row = $res->fetch_assoc();
    ?>

And the selection box code us

    <label for="gd">User Type</label>
    <select class="form-control" id="gd" name="gd">
    <option><?php echo $row['usertype'] ?></option>
    <option>Administrator</option>
    <option>NetMan</option>
    </select>
Jay
  • 31
  • 5

1 Answers1

1

If you still not find answer please have look at below code

 <?php
    $usertypes = array('Administrator' => 'Administrator', 'NetMan' => 'NetMan');
    $selcted_usertype = !empty($row['usertype']) ? $row['usertype'] : "";
    ?>
    <label for="gd">User Type</label>
    <select class="form-control" id="gd" name="gd">
        <?php
        foreach ($usertypes as $user_type_val => $usertype_txt) {
            $selected = ($user_type_val == $selcted_usertype) ? "selected='selected'" : "";
            ?>
            <option <?php echo $selected; ?> value="<?php echo $user_type_val; ?>"><?php echo $usertype_txt; ?></option>
        <?php }
        ?>

    </select>

You will need to value attribute for option.

Yogesh
  • 434
  • 4
  • 12