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 10 of 10
  1. #1
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts

    How to empty a query field?

    Hi, I want to make a field in the database empty before I submit the new query. So it doesn't stack information.

    This is an example query.

    PHP Code:
    $update mysql_query("UPDATE users SET permissions='' WHERE id='$id'"); 
    But it completely ignors that query. I need it so it empties permissions, and then a few lines down another query runs to put information back in. Or unless both can be done on the same query.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    SQL will never stack.
    Using:
    Code:
    UPDATE table SET x = 'new value' WHERE y = 'value'
    will always completely overwrite the value originally held by x. This is different than attempting to append values with a SET x = x + 'value', or SET x = x & 17.

    If the value is incorrect, than there is something wrong with the final query being run, somewhere in there it is appending values that sound like they don't belong.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    Well it keeps adding onto with my query.

    PHP Code:
    $userpermit explode(","$row["fortleadper"]);
       
    array_push($userpermit$use1);
    $new_userpermit implode(","$userpermit);

    $update mysql_query("UPDATE users SET fortleadper='$new_userpermit' WHERE id='" $row["id"] . "'); 
    When I execute that it will be like

    ,,3,,,6,7

    But if I run it again it looks like this.

    ,,3,,,6,7,1,2,,,5,,7

    It doesn't keep only 7 characters long.

  • #4
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    Well, from the looks of the code, you are first reading in the value in $row['fortleaderper'] (presumably from your db), then appending the value in $use1 and re-writing the combined data back into fortleadper in your db. So, it's not MySQL that's doing it, it's your logic.

    I might add that although I don't know what fortleadper is, I suspect that you should not be storing the data as csv. Instead, it should be a separate table linked to users by a foreign key. Then the fortleadper table can contain one value per row. This would be a more 'normalized' form for your data structure.
    John

  • #5
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    It stands for Fort Leader Permissions. So there is 7 permissions, if you have a 1, you can access blah, if you have a 2 you can access blah. Or else you can't access it.

    So how do I get it to clear or overwrite the last query?

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Aside from the normalized route, you should instead consider using a bitwise value for these.
    This works like so, each bitwise signifies a high order bit. So, these values are 1, 2, 4, 8, 16, 32, 64.... 2147483648. You're limited to 32 possible bitwise values in PHP, since integers are stored in a signed format.
    Then you associate each number to a single permission value. For example, can read would be 1, can write would be 2, can edit would be 4. Then, you assign the value of the permissions using an OR command. So, someone who has read, write and edit permissions would have a value of 7 (1 | 2 | 4), someone who can read and edit but not write would have a value of 5 (1 | 5). As you've probably noticed, the maximum value you can actually assign is equal to the current highest value << 2. So, the next permission in my example would be valued at 8 (since the previous was 4), while all the permissions before it are equal to 7 (1 + 2 + 4).
    Then you check with a bitwise & operation.
    PHP Code:
    $aPermissions = array(
        
    => 'Can Read',
        
    => 'Can Write',
        
    => 'Can Edit'
    );

    // Maybe we'll promote this to a defined value.
    reset($aPermissions);
    while (list(
    $key$value) = each($aPermissions))
    {
        
    define(strtoupper(str_replace(' ''_'$value)), $key);
    }

    // Remember, boolean in PHP is defined as FALSE and !FALSE
    // $userpermissionvalue is a single integer value, like 7
    if ($somethingtoindicateread && ($userpermissionvalue CAN_READ)) // Should be 0 or the CAN_READ value
    {
        
    // Do the read stuff, since they have privilege

    Does that make sense?

    Edit:
    Oh yeah completely missed this lol.
    Looks like to do what you're asking is to simply use the $use1 variable instead of the $userpermit. This will get interesting when you're altering it though, since you will need to explode it, check to see if it already exists, add if it doesn't remove where you don't need, then implode it again. This is why the normalized route is the best one, and then the bitwise for hardcoded permissions.
    Last edited by Fou-Lu; 03-04-2009 at 11:02 PM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #7
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    EEK! I don't know half of those code tags. I know like 5% of the total PHP tags available. What's the easiest and most beginner way of doing it? The functions work perfectly when it checks if you have access to it or not.

  • #8
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    *bump*

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    For the bitwise handling, the easiest way to handle it is like I posted - hard coded permissions.
    PHP Code:
    // Permissions.php
    // I'd actually use an object instead, to be more flexible in the future, but thats ok.
    $aPermissions = array(
        
    => 'Can Read',
        
    => 'Can Write',
        
    => 'Can Edit',
        
    => 'Can Delete',
        
    // And so on, *2 each value on
    );

    reset($aPermissions);
    while (list(
    $key$val) = each($aPermissions))
    {
        
    define(strtoupper(str_replace(' ''_'$val)), $key);
    }
    // This has just set up our definitions, letting us use CAN_READ, CAN_WRITE, and so forth.
    ?> 
    PHP Code:
    // Index
    <?php
    require_once 'permissions.php';

    // Do what you gotta do to get the user permissions

    if (!isset($_GET['do']))
    {
        
    $_GET['do'] = 'read';
    }

    $func null;
    $perm 0;

    switch (
    $_GET['do'])
    {
        case 
    'read':
            
    $func 'doRead';
            
    $perm CAN_READ;
            break;
        case 
    'write':
            
    $func 'doWrite';
            
    $perm CAN_WRITE;
            break;
        case 
    'edit':
            
    $func 'doEdit';
            
    $perm CAN_EDIT;
            break;
        case 
    'delete':
            
    $func 'doDelete';
            
    $perm CAN_DELETE;
            break;
    }

    if (
    $userpermission $perm// Eg: userpermission 5 & 1 = 1
    {
        
    call_user_func($func);
    }

    ?>
    Something like this. That help?

    The best part is how easy it is to generate the permissions involved. A simple form with a set of checkboxes, and an array sum. Super sweet:
    PHP Code:
    <form method="post" action="assignPermissions.php">
    <fieldset>
        <legend>Assign Permissions</legend>
    <?php

    foreach ($aPermissions AS $permKey => $permName)
    {
        
    printf("<label for=\"chk_%d\">%s</label>\n"$permKey$permName);
        
    printf("<input type=\"checkbox\" name=\"perm[]\" id=\"chk_%d\" value=\"%d\" />\n"$permKey$permKey);
    }

    ?>
    </fieldset>
    <input type="submit" name="submit" value="Change Permissions" />
    </form>
    $_POST['perm'] can be array_sum'd.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #10
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    That isn't capable with my website because it uses page arrays instead of echo and print.


  •  

    Posting Permissions

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