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
https://forums.phpfreaks.com/topic/234696-storing-variables-in-while-functions/
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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.