Page 1 of 1

I'm a bit stuck with a script here.

Posted: Sat Dec 13, 2008 2:18 pm
by JKM
Hi there!

I'm a bit stuck here. This script works fine where it's both active AND expired bans, but not if there is only expired bans. So is there anyone that got some tips for how I can solve this?
ebid = expired ban id
bid = ban id

Code: Select all

if(preg_match("^user_\d{1,10}$^",trim(strtoupper($_GET["s"])))) {
    mysql_connect('xxx','xxx','xxx');
    @mysql_select_db('xxx') or die("Unable to select database");
    $query = mysql_query("SELECT * from lol_old WHERE user_id='".$_GET['s']."'") or die(mysql_error());
    $query1 = mysql_query("SELECT * from lol_oldbans WHERE user_id='".$_GET['s']."'") or die(mysql_error());
   
    if(mysql_num_rows($query)) {
        while($myrow = mysql_fetch_array($query)) {
            if($myrow['ban_length'] == 0) {
                $length = 'perm';
            }
            else {
                $length = $myrow['ban_length'].'min';
            }
            $result = ' <p>'.$myrow['ban_reason'].' ('.$length.') - <a target="_blank" href="/?bid='.$myrow['bid'].'">?bid='.$myrow['bid'].'</a> (ACTIVE)</p>'."\r\n";
        }
    }
    elseif(mysql_num_rows($query1)) {
    while($myrow1 = mysql_fetch_array($query1)) {
            if($myrow1['ban_length'] == 0) {
                $length = 'perm';
            }
            else {
                $length = $myrow1['ban_length'].'min';
            }
            $oldres .= '    <p>'.$myrow1['ban_reason'].' ('.$length.') - <a target="_blank" href="/ebid='.$myrow1['ebid'].'">?ebid='.$myrow1['ebid'].'</a></p>'."\r\n";
        }
    }
    else {
        $result = " <p>Not banned.</p>"."\r\n";
    }
}
else {
    $result = " <p>Error! The search has to start with 'user_'</p>"."\r\n";
}
echo $result;
echo $oldres;
Thanks for any help! :-)

Re: I'm a bit stuck with a script here.

Posted: Sat Dec 13, 2008 3:00 pm
by cavemaneca
Try moving the else statement at line 29 to line 18 and changing the elseif to just if.
Right now it seems to go like this
if ban in lol_old
display ban
if not in lol_old if in lol_oldban
display ban
if not in either display no ban

It should be like this
if active ban
display ban
else not ban
if old ban
display old ban
else never banned

Re: I'm a bit stuck with a script here.

Posted: Sat Dec 13, 2008 3:55 pm
by JKM
Thanks for the help! But there is one little problem. Actually, it's a quite odd problem, I'd say.

- If there is an active ban, but no expired bans, the result cames up like this: "No expired bans" (nothing with "No active bans" or info about the active ban I searched for).
- If I'm searching for a user id where it's both active and expired bans, only the expired bans is shown (and no "No active bans").
- BUT if I'm searcing for an expired bans where there is no active bans, the result cames up like this: "No active bans" "expired ban1, expired ban2 (and so on)".

Code: Select all

if(preg_match("^user_\d{1,10}$^",trim(strtoupper($_GET["s"])))) {
    mysql_connect('xxx','xxx','xxx');
    @mysql_select_db('xxx') or die("Unable to select database");
    $query = mysql_query("SELECT * from lol_bans WHERE user_id='".$_GET['s']."'") or die(mysql_error());
    $query1 = mysql_query("SELECT * from lol_oldbans WHERE user_id='".$_GET['s']."'") or die(mysql_error());
   
    if(mysql_num_rows($query)) {
        while($myrow = mysql_fetch_array($query)) {
            if($myrow['ban_length'] == 0) {
                $length = 'perm';
            }
            else {
                $length = $myrow['ban_length'].'min';
            }
            $result = ' <p>'.$myrow['ban_reason'].' ('.$length.') - <a target="_blank" href="/?bid='.$myrow['bid'].'">?bid='.$myrow['bid'].'</a> (ACTIVE)</p>'."\r\n";
        }
    }
    else {
        $reuslt = 'No active bans'."\r\n";;
    }
    
    if(mysql_num_rows($query1)) {
        while($myrow1 = mysql_fetch_array($query1)) {
            if($myrow1['ban_length'] == 0) {
                $length = 'perm';
            }
            else {
                $length = $myrow1['ban_length'].'min';
            }
            $oldres .= '    <p>'.$myrow1['ban_reason'].' ('.$length.') - <a target="_blank" href="/ebid='.$myrow1['ebid'].'">?ebid='.$myrow1['ebid'].'</a></p>'."\r\n";
        }
    }
    else {
        $oldres = "No expired bans"."\r\n";
    }
}
echo $result;
echo $oldres;

Re: I'm a bit stuck with a script here.

Posted: Sat Dec 13, 2008 4:48 pm
by cavemaneca
Is the from for $query = mysql_query("SELECT * from l.ol_old WHERE user_id='".$_GET['s']."'") or die(mysql_error()); at line 4 supposed to be l.ol_old or l.ol_bans? becuase $query1 is l.ol_oldbans
(sorry for .'s if not it would show smilies)

Re: I'm a bit stuck with a script here.

Posted: Sat Dec 13, 2008 6:14 pm
by JKM
Oh, that's just a mistype - everything in the query is correct.

Re: I'm a bit stuck with a script here.

Posted: Sat Dec 13, 2008 10:46 pm
by cavemaneca
Best I can think of is trying it like this. It's possible you are getting only the results of your second query, so I set it up to only call the query right before it's needed.
Another thing, I set mysql_num_row to ask for > 0 instead of true, and although I didn't put it in, I prefer to use _assoc instead of _array in mysql_fetch

Code: Select all

if(preg_match("^user_\d{1,10}$^",trim(strtoupper($_GET["s"])))) {
    mysql_connect('xxx','xxx','xxx');
    @mysql_select_db('xxx') or die("Unable to select database");
    $query = "SELECT * from lol_bans WHERE user_id='".$_GET['s']."'";
    $query1 = "SELECT * from lol_oldbans WHERE user_id='".$_GET['s']."'";
   
    $result = mysql_query($query) or die(mysql_error());
    if(mysql_num_rows($result) > 0) {
        while($myrow = mysql_fetch_array($result)) {
            if($myrow['ban_length'] == 0) {
                $length = 'perm';
            }
            else {
                $length = $myrow['ban_length'].'min';
            }
            $result = ' <p>'.$myrow['ban_reason'].' ('.$length.') - <a target="_blank" href="/?bid='.$myrow['bid'].'">?bid='.$myrow['bid'].'</a> (ACTIVE)</p>'."\r\n";
        }
    }
    else {
        $reuslt = 'No active bans'."\r\n";;
    }
    
    $result = mysql_query($query1) or die(mysql_error());
    if(mysql_num_rows($result) > 0) {
        while($myrow1 = mysql_fetch_array($result)) {
            if($myrow1['ban_length'] == 0) {
                $length = 'perm';
            }
            else {
                $length = $myrow1['ban_length'].'min';
            }
            $oldres .= '    <p>'.$myrow1['ban_reason'].' ('.$length.') - <a target="_blank" href="/ebid='.$myrow1['ebid'].'">?ebid='.$myrow1['ebid'].'</a></p>'."\r\n";
        }
    }
    else {
        $oldres = "No expired bans"."\r\n";
    }
}
echo $result;
echo $oldres;
EDIT: I agree completely with the below comment as well. You shouldn't be using any of these scripts until you are sure that you have taken at least the minimal measures to avoid people screwing with your database no matter what, even in password protected areas.

Re: I'm a bit stuck with a script here.

Posted: Sun Dec 14, 2008 12:53 am
by toasty2
Not to unnecessarily add to your problems, but you should also sanitize user inputs (the user-supplied data you're sticking into your sql queries).