Jump to content

Storing variables in while functions.


Frezzwar

Recommended Posts

Still working on my project, and i have been learning a lot here! Thank you so much.

But as you may have guessed, i still have problems.

User registration is working, and new users are put in the database with a rank of 0. This means they can't do anything. (can there be trouble with this. I mean, a rank of "0"?)

An admin needs to give access to these accounts, but that is where it becomes difficult.

The following code is showing the new accounts to the admin.

<?php
include("navbar.php");
if ($admin<2) //normal guy or not
{
die ("Du har ikke rettigheder til at se denne side!");
}
else

if(isset($_POST['submit']))
{
//code to make the user able to use stuff

}


{	
$connect = mysql_connect("localhost","root","");
mysql_select_db("eksamen - phoenix");
$query = mysql_query("SELECT * FROM users WHERE rank='0'");

       
	?>
	<form action='admin.php' method='POST'>
		<table>				
			<?php
			while($row = mysql_fetch_assoc($query)) {
			 echo "
				<tr>				
					<td>
					".$row['username']."
					</td>
					<td>
					".$row['email']."
					</td>
					<td>
					".$row['real_name']."
					</td>
					<td>
					<input type=\"checkbox\" name=".$row['username']." value=\"Godkend\"> 
					</td>					
				</tr>
			";} ?>	
				<tr>
					<td>
					<input type='submit' name='submit' value='Register'>
					</td>
				</tr>					
		</table>
	</form>		
	<?php
}
?>

As you may see, there is a lot of turning php on and off. I made it work this way, but i guess there is nothing wrong with it.

The problem is that the username is not stored, so i can connect to the database and change the "rank" value. Changing that value should be easy, but storing the username is as easy as i thought.

Any ideas?

Link to comment
Share on other sites

Admin gives access to the user by changing the "rank" to 1 or whatever greater than 0? You have two options:

 

1. Place a link in each user row with a url variable (file.php?uid=x). You can catch that uid with GET and update his rank.

2. Place a checkbox (as you currently have) that acts as an array and holds user ids.

 

<input type=\"checkbox\" name=\"uid[]\" value=\"" . $row['id'] . "\">

 

With that code you'll have a "uid" POST variable, which is actually an array holding user ids (note the square braces of uid[] in the "name" attribute). The following code will update the rank of the checked users:

<?php
if (isset($_POST['submit'])) {
     $uid = $_POST['uid']; //this will return an array with user ids
     foreach ($uid as $id) {
          $results = mysql_query("UPDATE users SET rank=1 WHERE id=$id");
     }
}
?>

 

Just remember to place mysql_connect() and mysql_select_db() in the beginning of the script, or otherwise you will run the query on submit before being connected to the database.

 

Hope I got your question right and my suggestions help.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.