Log in

View Full Version : Getting Variable Error



Titan85
07-10-2007, 12:19 AM
Hello, I am trying to make a user ban script that you can either enter the username or IP and it will ban the user. When baning by username, I want it get the ip of the user out of the user list and thats where I run into problems. All the data goes through but for some reason, it never gets the value of $ip. Here is my code:
<?php

########## If Ban Hit ##########
if ($_POST['ban']) {
$reason = mysql_real_escape_string($_POST['reason']);
$ban = $_POST['ban'];
$date = date('n/j/y');

// If ban by username
if ($_POST['type'] == 'user') {
$get = mysql_query("SELECT ip FROM `users` WHERE user = '$ban'") or die ("Error Getting IP! \n<br />\n" .mysql_error());
$u = mysql_fetch_array($get); $ip = $u['ip'];
} else { $ip = mysql_real_escape_string($_POST['ip']); }
//

$insert = mysql_query("INSERT INTO `ban` (id, reason, ip, date) VALUES ('', '$reason', '$ip', '$date')") or die ("Error Banning User! \n<br />\n" .mysql_error());
echo('<meta http-equiv="refresh" content="2;URL=index.php" /> <div class="message">'.$ip.' The user has been banned.</div>');
}


########## If Ban Not Hit ##########
if (!$_POST['ban']) { ?>

<!-- Ban User Form -->
<form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<table width="300" align="center">
<tr>
<td>Ban By:</td>
<td>
<select name="type">
<option selected="selected" value="ip">IP</option>
<option value="user">Username</option>
</select>
</td>
</tr>
<tr>
<td>IP/Username:</td>
<td><input type="text" name="ban" /></td>
</tr>
<tr>
<td>Reason:</td>
<td><input type="text" name="reason" maxlength="80" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="ban" value="Ban" /></td>
</tr>
</table>
</form>
<!-- /Ban User Form -->

<? }

?>I think the query for getting the ip data is returning nothing, but I know that there is indeed an entry for "ip" in the user table for that username. Any ideas? Thanks

alexjewell
07-11-2007, 12:00 AM
I don't see you using $ban in the else statement - instead you're using $_POST['ip']...I may be misunderstanding, but it looks like that should equal $ban?

So:



else { $ip = mysql_real_escape_string($ban); }

thetestingsite
07-11-2007, 12:19 AM
The variable "ban" is being overwritten with the value of the submit button. You should change the name of the text field (the one that you type the username or ip into) to something else.

Hope this helps.

alexjewell
07-11-2007, 01:17 PM
Oh, Testing is right, you gave the submit button the same name as the ban input...



<form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<table width="300" align="center">
<tr>
<td>Ban By:</td>
<td>
<select name="type">
<option selected="selected" value="ip">IP</option>
<option value="user">Username</option>
</select>
</td>
</tr>
<tr>
<td>IP/Username:</td>
<td><input type="text" name="ban" /></td>
</tr>
<tr>
<td>Reason:</td>
<td><input type="text" name="reason" maxlength="80" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="ban_submit" value="Ban" /></td>
</tr>
</table>
</form>


But I still see the other issue I saw before...
Maybe I'm wrong, but it looks like you're still banning the value of the selected option "ip"...not the actual ip, that would be in the ban input...

thetestingsite
07-11-2007, 03:00 PM
Yes, alex is correct on this. It should be $ban instead of $_POST['ip'] (beings that there is no form field called ip).

Titan85
07-11-2007, 03:07 PM
Thanks guys, both problems were correct and once fixed, it worked fine. Thanks for the help.