Jump to content

'Sort' rows in a table


tomhoad

Recommended Posts

Hi, I can't see where this script is failing. It doesn't appear to sort correctly (just randomly).

 

<table width="200" border="1">
<tr>
<td><b>Email <a href="view_members.php?sort=email">[sort]</a></b></td>
<td><b>Name <a href="view_members.php?sort=name">[sort]</a></b></td>
<td><b>Date of Birth <a href="view_members.php?sort=dob">[sort]</a></b></td>
<td><b>Favourite Artist <a href="view_members.php?sort=fav_artist">[sort]</a></b></td>
<td><b>Favourite Genre <a href="view_members.php?sort=fav_genre">[sort]</a></b></td>
</tr>
    
<?php
    
if(isset($_GET['sort']))
{
	$query = "SELECT id, name, email, fav_artist, fav_genre, FROM_UNIXTIME(dob, '%e/%c/%Y') as dob FROM users ORDER BY '{$_GET['sort']}'";

	$result  = mysql_query($query) or die('Error : ' . mysql_error()); 
	while ($row     = mysql_fetch_array($result, MYSQL_ASSOC)) { 

	$id   = $row['id']; 
	$name   = $row['name']; 
	$email   = $row['email']; 
	$dob   = $row['dob'];
	$fav_artist   = $row['fav_artist'];
	$fav_genre   = $row['fav_genre'];
	?>

	<tr>
	<td><?php echo $email;?></td>
	<td><?php echo $name;?></td>
	<td><?php echo $dob;?></td>
	<td><?php echo $fav_artist;?></td>
	<td><?php echo $fav_genre;?></td>
	</tr>

	<?php
	}
}
?>

 

Any help appreciated :)

 

Link to comment
Share on other sites

What is the contents of $_GET['sort'] ? Aside from the obvious security issues with your code, I'm assuming the $_GET['sort'] is supposed to represent a column name, that being the case it shouldn't be enclosed in single quotes.

Link to comment
Share on other sites

Ok great, thanks a lot.

 

I assume by security issues you mean not sanitizing the Get data?

 

Also, the sort now works for all fields except the date of birth, and I'm pretty sure this is because this is stored as a timestamp - can you advise on how to successfully be able to sort that?

 

Thanks!

Link to comment
Share on other sites

try removing the single quotes around $_GET['sort'] in your query:

 

'{$_GET['sort']}'

 

to

 

{$_GET['sort']}

 

also, to address cag's note about security, inside your condition, above your $query = ... you should at the very least do something like this:

$allowed = array('email','name','dob','fav_artist','fav_game');
$_GET['sort'] = (in_array($_GET['sort'],$allowed)) ? $_GET['sort'] : $allowed[0]; // change [0] to whatever element you want as default

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.