Looking for ideas on this code...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Goat
Forum Newbie
Posts: 11
Joined: Mon Apr 29, 2002 11:12 pm
Contact:

Looking for ideas on this code...

Post by Goat »

I've recently just tried programming in PHP and being completely new to it I've run into a problem here and there but nothing I couldn't work through. Now I've got this problem, I don't know whether it is through coding, or through my approach and I would appreciate any ideas/comments.

Here's my situation, I've been working on a site where members log in and then are assigned a userlevel either 1-4, 1 being the user with the most access to things. What I've been working on now is a script to promote users and demote users. I created a page that calls all my users from a database and then displays them in a table each user in a different row with each row having the option to promote/demote. I wrote the following script, thinking it would work properly:

Code: Select all

<FORM ACTION="<?PHP_SELF?>" METHOD="Post"> 
<INPUT TYPE="submit" name="promote" VALUE="Promote"> 
</FORM> 
			
<?
$puserlevel = $tuserlevel - 1;
if($promote) 
&#123; 
$connection = mysql_connect($DBhost, $DBuser, $DBpass) or die ("Unable to connect!"); 
mysql_select_db($DBName) or die ("Unable to select database!"); 
$query = "UPDATE auth_user SET userlevel = '$puserlevel' WHERE username ='$tusername'"; 
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error()); 
&#125; 
?>
My dilemma is that when I try to promote one user, it promotes them all, and vice versa. Is it my code, or do I need to try a different approach? Thanks for your time and any comments.
phpfreak
Forum Commoner
Posts: 30
Joined: Fri Mar 21, 2003 10:28 am
Location: New Jersey,USA
Contact:

login php mysql connection to the database

Post by phpfreak »

hi there,
Where are you defining youre $tusername.i.e., in which place are you telling php to update a specific user to promote. I dont see any checkboxes or any textbox where admin can select or enter the name of the user to promote.

It would be great if you could give the whole code.

regards
srinivas
fractalvibes
Forum Contributor
Posts: 335
Joined: Thu Sep 26, 2002 6:14 pm
Location: Waco, Texas

Post by fractalvibes »

Number One thing to do always, always, always is:

You have:
$query = "UPDATE auth_user SET userlevel = '$puserlevel' WHERE username ='$tusername'";

Immediately afterward do this:

echo "sql is: " . $query . "<br>";

To see exactly what you are actually trying to execute...sounds like
$tusername might be blank at this point?!

Phil J.
Jim
Forum Contributor
Posts: 238
Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas

Post by Jim »

Hmm... You might be right, fractal, but he says that ALL user names get promoted, which means there is some sort of transfer of information going on here.

I notice that you don't have $promote adequately defined in the code. Any programmer here will surely give you this advice... always remember to properly call your variables. In this case, this is the way to go:

Code: Select all

$promote = $_POST['promote'];
I don't know what the rest of your code looks like, but I assume you're using a loop with mysql_fetch_(array or assoc) to get your user information from the database.

If this is the case, chances are all the information that comes up in that loop is being put in to this code. That would explain all of your users being updated at once.

I also don't see your users being specified in the form you've created. All I see is a submit action. You need to separate the usernames using some method. This is how I do things like this. Just an example, not the only way to do it. I use query strings :)

Here's my exemplo:

Code: Select all

//All code below is assuming you're already connected to the database

     //Select usernames and current ranks from db

    $sql = "select user, rank, from user_table";
    $act = mysql_query($sql);

            while ($info = mysql_fetch_array($act)) {
  
                $user = $info['user'];
                $rank = $info['rank'];

            
            echo "<b>$user</b>&nbsp;&nbsp;"; //This prints the user name in bold
            echo "<a href='whateverpage.php?action=promote&user=$user&rank=$rank'> Promote </a> &nbsp;&nbsp;"; //Clicking this link will promote the user

            echo "<a href='whateverpage.php?action=demote&user=$user&rank=$rank'> Demote </a>"; //Clicking this link will demote the user

            }


Then I'd get the proper variables, and start the rest of the process.

Code: Select all

$action = $_GET['action'];  //This is either the promote or demote action
$rank = $_GET['rank'];
$user = $_GET['user'];

if ($action == 'promote') {

$newrank = $rank + 1;

$sql = "update user_table set rank = '$newrank' where user = '$user'";
$act = mysql_query($sql);

}


if ($action == 'demote') {

$newrank = $rank - 1;

$sql = "update user_table set rank = '$newrank' where user = '$user'";
$act = mysql_query($sql);


}
Just my two cents though. :P Hope it gives you some ideas!
Goat
Forum Newbie
Posts: 11
Joined: Mon Apr 29, 2002 11:12 pm
Contact:

Post by Goat »

Jim, you are exactly right about using the loop to fetch the information from the database. Once the information was gathered it was then placed into a table with the promote option being the last column of the table at the end of each row.

I'm going to take a new crack at this script using some of the options you gave me. Thanks for your help.
Post Reply