Jump to content

Recommended Posts

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 by learningprobs

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.