Hi All,
Ive got a page called user.php and when I link to there I get there by sending a URL variable called id.... so basically like this...
user.php?id=6
user.php?id=3 etc etc...
Then in my main code I have a line like this:
For some reason this doesnt work though (i.e. it doesnt display the 'name' field for the given 'id' field).Code:<?php $result = mysql_query("SELECT * FROM sp_table WHERE id=$_GET['id']"); while($row = mysql_fetch_array($result)) { echo $row['name']; echo "<br />"; } ?>
I know that the problem part of the code is the $result line because when I replace a plain number instead of the $_GET function it displays the 'name' field ok... i.e. this...:
...would display the correct 'name' field.Code:$result = mysql_query("SELECT * FROM sp_table WHERE id='1'");
Any ideas what is wrong with my $GET['id'] field?
Thanks for all your help!
Ive just figured it out... yay!!!
I should have added the GET into a variable, and echo the variable, not the funcion...
i.e.
hope this helps somebody else!Code:$idvar = $_GET['id'];
I think if you load it directly into the mysql thing you just need to take out the ' character for it to work like this:
However you should make it look a bit nicer and filter GET data to prevent injectionCode:$result = mysql_query("SELECT * FROM sp_table WHERE id=$_GET[id]");
This filters out everything except numbers so that you cant get an injection.Code:$id = ereg_replace('[^0-9]', '', $_GET['id']);
$result = mysql_query("SELECT * FROM `sp_table` WHERE `id`=$id");
or, the built in mysql function for preventing injection
Code:$result = mysql_query("SELECT * FROM `sp_table` WHERE `id`='".mysql_real_escape_string($_GET['id'])."'");
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall
mysql_real_escape_string() is imo in only-numeric values needless...
Well, you can try is_numerical or similar methods first, and throw an error upon that, but the very best advice from me is to always enclose values within the sql statement with apostrophes (') and always escape them at some point.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks