Jump to content

Why will only disable users work in my code?


slotegraafd
 Share

Recommended Posts

So I am trying to create a table where users can be disabled and enabled with the press of a radio button. So for the most part it works but for some reason it only allows me to disable a user it wont let me enable them and I can't seem to figure out why.

I've checked that all the names were correct with the database and I have also tried both single ' quotes and double " quotes on the prepared statements to see if that makes a difference... it did not

These are the functions to disable and enable users

function inactive($userId){
$conn = db_connect();

$disable_user = pg_prepare($conn, 'disable', "UPDATE users SET Enabled = false, Status = 'd' WHERE Id = $userId");

$result = pg_execute($conn, 'disable', array());
}

//Create a function to activate a salesperson
function active($userId){
$conn = db_connect();

$enable_user = pg_prepare($conn, 'enable', "UPDATE users SET Enabled = true, Status = 'a' WHERE Id = $userId");

$result = pg_execute($conn, 'enable', array());
}
$id = (array_keys($_POST['active'])[0]);
      $status = $_POST['active'][$id];

      if($status == "t"){
        active($id);
      } 
      else{
        inactive($id);
      }

And this is where it gets put into action ^^

 

In the database I have a boolean for enabled which is either true or false and then I have a varchar for status which is either 'd' or 'a'

 

Any help would be greatly appreciated

 

Thanks in advance

Link to comment
Share on other sites

11 hours ago, requinix said:

What's the code for your HTML form? Specifically for the radio buttons.

ope sorry

if ($keys[$recordCount] == "logo" && $totalRows[$column] != "") {
          echo '<td class="py-2"><img src="' . $totalRows[$column] . '" alt="Client Logo" class="logo-thumbnail" /></td>';
        
        //Check whether or not the client is active or inactive
        } else if ($keys[$recordCount] == "enabled") {
          $userId = $data[$record]['id'];
          $enabled = $data[$record]['status'];
          
          echo '
            <td class="py-2">'. 
              '<form method="POST" action="salesperson.php">
                <div>
                  <input type="radio" name="active[' . $userId . ']" value="Active" '; 
                  if($enabled == "a") { echo 'checked'; };
                   echo '/>
                  <label for="' . $userId . '-Active">Active</label>
                </div> 
                <div>
                  <input type="radio" name="active[' . $userId . ']" value="Inactive" '; 
                  if($enabled == "d") { echo 'checked'; };
                  echo '/>
                  <label for="' . $userId . '-Inactive">Inactive</label>
                </div> 
                <input type="submit" name="submitType" value="Update" />
              </form> 
              Current Status: '. $totalRows[$column] . 
            "</td>";

(its created as a function)

Link to comment
Share on other sites

Good that you solved it, but next time, it would be nice if you could let us know that the problem was the code thinking the radio button's value is "t" when the form uses "Active". In case someone else comes to this thread hoping for an answer to their own problems.

Link to comment
Share on other sites

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.

 Share

×
×
  • 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.