learningprobs Posted August 5, 2016 Share Posted August 5, 2016 (edited) Hello, I am having a little problem, nothing serious but it is annoying. I have made a CRUD, this page is for my update page, the form values are loaded from mysql, but when there is a POST error, the form may also refresh. I have managed to get my values to load ok and refresh ok, the only problem is that I am getting double values in my dropdown menu(1 from the $_POST and 1 from the database). How to remedy to this please? Here is my prepared statement: <?php if(isset($_GET['id'])){ $id = (int)$_GET['id']; $stmt = $conn->prepare("SELECT u.users_id, u.users_firstname, u.users_lastname, u.users_username, u.users_email, u.users_password, u.users_active, u.users_phone, u.users_countries_id, u.users_role, r.role_name, c.countries_name, a.activated_id, a.activated_name FROM users AS u LEFT JOIN roles AS r ON u.users_role = r.role_id LEFT JOIN countries AS c ON u.users_countries_id = c.countries_id LEFT JOIN activated AS a ON u.users_active = a.activated_id WHERE u.users_id = ?"); $stmt->bind_param('i', $id); $stmt->bind_result( $users_id, $users_firstname, $users_lastname, $users_username, $users_email, $users_password, $users_active, $users_phone, $users_countries_id, $users_role, $role_name, $countries_name, $activated_id, $activated_name); $stmt->execute(); $stmt->store_result(); $stmt->fetch(); $stmt->free_result(); $stmt->close(); } ?> And here one of my dropdown menus: <div class="form-group"> <label for="activated">Activated?</label> <select class="form-control" name="active"> <option value="<?php echo (int)$activated_id; ?>"> <?php echo htmlentities($activated_name); ?> </option> <?php $stmt = $conn->prepare("SELECT activated_id,activated_name FROM activated"); $stmt->bind_result($activated_id,$activated_name); $stmt->execute(); $stmt->store_result(); if($stmt->num_rows() > 0){ while ($stmt->fetch()) { $s = ""; if(isset($_POST['active']) && ($_POST['active'] == $activated_id)) { $s = " selected"; } echo "<option value='".(int)$activated_id."'".$s.">".htmlentities($activated_name)."</option>"; } } $stmt->free_result(); $stmt->close(); ?> </select> </div> Here is what happens: http://screencast.com/t/newQksedwkQ Thank you! Edited August 5, 2016 by learningprobs Quote Link to comment https://forums.phpfreaks.com/topic/301770-how-to-eliminate-double-values-in-dynamic-dropdown-on-update/ Share on other sites More sharing options...
Barand Posted August 5, 2016 Share Posted August 5, 2016 Use "SELECT DISTINCT ... " You seem to have design flaws in the database. If the table contains the id and name then they should be unique. If, on the other hand, it's a table at the "many" end of a one-to-many relationship then the name shouldn't be in the table, just the id. Quote Link to comment https://forums.phpfreaks.com/topic/301770-how-to-eliminate-double-values-in-dynamic-dropdown-on-update/#findComment-1535684 Share on other sites More sharing options...
Jacques1 Posted August 5, 2016 Share Posted August 5, 2016 For some reason, you first create a single <option> element for the selected option and then iterate over all possible options (including the selected one). This necessarily leads to a duplicate option. Quote Link to comment https://forums.phpfreaks.com/topic/301770-how-to-eliminate-double-values-in-dynamic-dropdown-on-update/#findComment-1535685 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.