0

I want to edit an admin which has username, password and power. The last one is made like this:

<?php $showQuery = mysqli_query($db, "SELECT * FROM admins WHERE id = {$id}"); $show = mysqli_fetch_assoc($showQuery); ?> <p>Power:&nbsp; <select name = "power"> <option value <?php if ($show['power']=='superadmin') {echo " selected";} ?> = "superadmin">Superadmin</option> <option value <?php if ($show['power']=='admin') {echo " selected";} ?> = "admin">Admin</option> <option value <?php if ($show['power']=='moderator') {echo " selected";} ?> = "moderator">Moderator</option> </select><br /><br /> 

Then I want to catch these values:

<?php if (isset($_POST['editAdmin'])) { $username = $_POST['username']; if (!$_POST['password'] == "") { $password = $_POST['password']; } $power = $_POST['power']; $editQuery = "UPDATE admins SET "; $editQuery .= "username = '{$username}', "; if (isset($password)) { $editQuery .= "hashed_pwd = '{$password}', "; } $editQuery .= "power = '{$power}' "; $editQuery .= "WHERE id = {$id} LIMIT 1"; $editAdmin = mysqli_query($db, $editQuery); 

Now, if I don't want to change the power of the admin (leave the option unchanged) and hit Edit, the power becomes blank. If, however, I change the power (upgrade or downgrade) it get set OK. What am I doing wrong?

1
  • First of all your code is invalid. <option value <?php if ($show['power']=='superadmin') {echo " selected";} ?> = "superadmin">Superadmin</option> Needs to be <option value= "superadmin"<?php if ($show['power']=='superadmin') {echo " selected";} ?>>Superadmin</option> (similar for the other options) Commented Nov 16, 2013 at 11:53

2 Answers 2

1

You are wrongly constructing your select, you have the options set like this:

<option value <?php if ($show['power']=='admin') {echo " selected";} ?> = "admin">Admin</option> 

where they should be like this:

<option value="admin" <?php if ($show['power']=='admin') {echo " selected";} ?> >Admin</option> 
Sign up to request clarification or add additional context in comments.

Comments

1

Change option outputing to

<option value="superadmin" <?php if ($show['power']=='superadmin') {echo "selected";} ?>>Superadmin</option> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.