Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts

    Query doesn't add on

    Hi, I am trying to make a ban list, where you type in a users id to ban them. I wanted to seperate each user id with a comma, the first one records but doesn't keep adding on. It overwrites the first one. Here is the coding I am using.


    Code:
    $id = $_POST["id"];
    $id = intval($id);
    
    $close = $id .= ",";
            $final = $close;
    Code:
    doquery("UPDATE {{table}} SET banlist='$final' WHERE blah blah

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,862
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    What would be the value of $close in
    $close = $id .= ",";
    , at the time of execution?

    BTW, that expression is equivalent to
    PHP Code:
    $id=$id.",";
    $close=$id 
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    $id would be the id that a user types in, so if the user they want banned is ID 100, then $id would become 100.

  • #4
    Regular Coder
    Join Date
    Mar 2007
    Location
    Quebec
    Posts
    261
    Thanks
    6
    Thanked 7 Times in 7 Posts
    I've done something similar in the past and I used explode.
    Here's the code. Maybe it will help.
    PHP Code:
    //Check values for blanks and invalid data
    if(!empty($banUser)){
        
    //Explode usernames to see if there are multiple
        
    $banUser explode(",",$banUser);
        
        
    //Check if username exists
        
    foreach($banUser as $user){
            
    //Check so they don't ban themselves
            
    if($user == $searchName){
                die(
    "You cannot ban your own account.");
            }
            
    userExistsS "SELECT `user_name` FROM `{$prefix}_users` 
            WHERE `user_name`='$user'"
    ;
            
    $userExistsQ mysql_query($userExistsS);
            
    $num mysql_num_rows($userExistsQ); 
            
            if(
    $num == 0){
                
    $errors[] = "The username {$user} was not found";
            }
        }
    }
        
    //Add to DB if no errors are present                
    if(empty($errors)){
        
    //Separate usernames
        
    foreach($banUser as $user){
            
    $banUserSql "INSERT INTO `{$prefix}_banned_users` (`ban_id`,`user_name`,`ban_caption`,`ban_length`)
                       VALUES('NULL','$user','$banCaption','$banLength')"
    ;
                
    $banUsersQ mysql_query($banUserSql) or die(mysql_error());
            if(!
    $banUsersQ){
                die(
    "An error was encountered and the query failed.");
            }else{
                
    header("Location: {$installPath}/admin/index.php");
            }
        }

    I've left out a few odds and ends (mostly variable initialization) but the main parts are there which I think you help you.
    Last edited by fl00d; 07-14-2008 at 05:57 PM.

  • #5
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    I don't quite understand that one. Half of it I do, but some parts I don't.
    Doesn't a period mean to "add on" to a database field?

  • #6
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,862
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Quote Originally Posted by masterofollies View Post
    $id would be the id that a user types in, so if the user they want banned is ID 100, then $id would become 100.
    If you are trying to concatenate the current $id value to the present value in the table, you'd need to use a mysql concat() function, say
    Code:
    update table set field_name=concat(field_name, ',$id') where ...
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    masterofollies (07-15-2008)

  • #7
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    Hmm I will try that, it's suppose to look something like this in the database.

    166,52,305,12,18, etc.

  • #8
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    Works well. Thank you

  • #9
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,862
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Quote Originally Posted by masterofollies View Post
    Works well. Thank you
    Have you ever thought about the initial case (when there is nothing in that field)? Or isn't it OK to have an extra comma at the beginning?
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)


  •  

    Posting Permissions

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