AidanPT
AidanPT

Reputation: 185

PHP HTML Form not posting data to database?

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";
}

Upvotes: 2

Views: 294

Answers (2)

Kirill Arutyunov
Kirill Arutyunov

Reputation: 66

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.'" />

Upvotes: 1

Nishu Tayal
Nishu Tayal

Reputation: 20820

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.

Upvotes: 1

Related Questions