Results 1 to 6 of 6

Thread: Getting Variable Error

  1. #1
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Getting Variable Error

    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 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
    Thanks DD, you saved me countless times

  2. #2
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    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:

    PHP Code:
    else { $ip mysql_real_escape_string($ban); } 
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  3. #3
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    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.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  4. #4
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    Oh, Testing is right, you gave the submit button the same name as the ban input...

    HTML Code:
                    <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...
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  5. #5
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Yes, alex is correct on this. It should be $ban instead of $_POST['ip'] (beings that there is no form field called ip).
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  6. #6
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks guys, both problems were correct and once fixed, it worked fine. Thanks for the help.
    Thanks DD, you saved me countless times

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •