2

I have this form on my site to change activated statuses of users in the users list, so the admin has the ability to ban people from the admin area. The form is meant to show what the activation status is, then show either 0, or 1 as an option. If you drop down and change to the opposite choice, it will change the activation status number in the database under the specific username in which you've changed the activation number. I am having a problem because when you change the number, the page refreshes as something is happening. But it seems to not send the information or change the database at all? Has anyone got any help here? Thanks.

$outputList .= '<tr><td>'. $id .'</td><td>'. $firstname .'</td>
    <td>'. $lastname .'</td>
    <td><form name="activationsend" action="admin.php?page=2" method="POST">
      <input type="hidden"/>
      <select name="act" onchange="this.form.submit();">
        <option style="display:none">'. $activated .'</option>
        <option name="0" value="0" >0</option>
        <option name="1" value="1" >1</option>
      </select>
    </td>
    <td><a href="'. $username .'">'. $username .'</a></td>
    </form>
  </tr>';
}

$outputList .= '</tbody>
            </table>';

if (isset($_POST['activationsend'])){
$activated = $_POST['act'];
$username = $_POST['$username'];

$exists = mysql_query ("SELECT * FROM users WHERE username='$username'") or die ("not found");
    if (mysql_num_rows($exists) != 0){
        //update the info in database
        mysql_query ("UPDATE users SET activated='$activated' WHERE username='$username'") or die ("update didn't work");
        echo "worked";
} else echo "did not work";
}
3
  • $username = $_POST['$username']; also username is never posted, there's no such field Commented Mar 18, 2014 at 10:32
  • So how can I make username a field depending on which username is being clicked on? Bare in mind that its a list of usernames with the form on each separate one? Commented Mar 18, 2014 at 10:34
  • well, make a drop down before the one that submits Commented Mar 18, 2014 at 10:36

2 Answers 2

1

Try this condition

if (isset($_POST['act'])){
// ...
}

I'm not sure, that activationsend send to server. So, you can also try var_dump($_POST) function to show your POST data after request:

$outputList .= '</tbody>
            </table>';

var_dump($_POST);

It will return POST-data or NULL.

UPD.

You also need to pass username data. Try to change

<a href="'. $username .'">'. $username .'</a>

into

<a href="'. $username .'">'. $username .'</a>
<input type="hidden" name="username" value="'.$username.'" />
Sign up to request clarification or add additional context in comments.

8 Comments

The form is NOW being initiated but it's echoing "did not work" no idea why?
With the Var_dump on send its saying "array(1) { ["act"]=> string(1) "1" } did not work "
oh.you have an error: $username = $_POST['$username']; change to $username = $_POST['username'];
You don't need to write $ as array index: $username = $_POST['username']; // 'username' is a name of your form field, but not '$username'
array(1) { ["act"]=> string(1) "1" } did not work, still giving me that.
|
1

First of all, username is not posted to the server and over that you are using $username variable instead of username in $_POST.

It should be like this :

if (isset($_POST['activationsend'])){
  $activated = $_POST['act'];
  $username = isset($_POST['username'])? $_POST['username'] : '';
  // Check if $username is empty or not
  if($username != ""){
    $exists = mysql_query ("SELECT * FROM users WHERE username='$username'") or die ("not found");
    if (mysql_num_rows($exists) != 0){
        //update the info in database
        mysql_query ("UPDATE users SET activated='$activated' WHERE username='$username'") or die ("update didn't work");
        echo "worked";
    } else echo "did not work";
  }
}

If there is no record in database for that username, it'll show "did not work"

Hope,it'll solve your problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.