Jump to content


Photo

$_GET help please FIXED


  • Please log in to reply
10 replies to this topic

#1 maxic0

maxic0
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 31 July 2006 - 02:18 AM

Hi, i haven't been doing PHP for to long, so i don't know about some thing at all. I have never quite got my head around the $_GET thing
My friend was kind enout to make this little script just to show how it works.
<?php
$sentence = 'Hello, ' . $_GET['name'] . ', and welcome to my little page.';
echo $sentence;
?>
<br /> <br />
<a href="indexx.php?name=Simon">Simon</a><br />
<a href="indexx.php?name=John">John</a><br />
<a href="indexx.php?name=Max">Max</a><br />

This code is easy to understand, but in my site i want to create a page where the information is stored in a database and then the information is collected from there. So for example if a had a table called "people " and then a little information about Simon, John and Max was stored stored in there in a field called "info" how would i echo the information about them when their name is clicked.

Any help would be very appreciated.
Thanks
maxic0

#2 pixy

pixy
  • Members
  • PipPipPip
  • Advanced Member
  • 295 posts

Posted 31 July 2006 - 02:24 AM

1. You will always want to escape $_GET variables so that people do not enter malicious content onto your site.
2. Here's what I think you want:
<?php
if (isset($_GET['name'])) {
  $name = $_GET['name'];
  $query = "SELECT * FROM users WHERE name='$name'";
  $result = mysql_query($result) or die(mysql_error());
  if ($result) {
    while ($row = mysql_fetch_array($result)) {
      echo $row['name']; // Output all information.
    }
  }
  else {
    echo 'Error with query: '.$query.'<br>'.mysql_error();
  }
}
else {
  echo 'You must specify a name.';
}
?>

This is a .44 Caliber Loveletter straight through my heart.

Tabulas + Threadless + Hire Me!


#3 maxic0

maxic0
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 31 July 2006 - 02:34 AM

Thats perfect pixy, Thank you very much!

#4 maxic0

maxic0
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 31 July 2006 - 02:40 AM

Oh no sorry, there is a small problem :-[.
I get the output "Query was empty" even if there is something in the field.
I changed echo $row['name']; to the names of the different rows, but i get the same thing everytime. =/
Can you help me please.

#5 hostfreak

hostfreak
  • Members
  • PipPipPip
  • Advanced Member
  • 581 posts

Posted 31 July 2006 - 02:56 AM

You also changed the variable right? "$name" = $_GET['name']; ?

#6 maxic0

maxic0
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 31 July 2006 - 03:05 AM

Yeah that part of it is right, but it just Says "Query was empty".
I don't understand.

#7 hostfreak

hostfreak
  • Members
  • PipPipPip
  • Advanced Member
  • 581 posts

Posted 31 July 2006 - 06:45 AM

Try:

<?php
if (isset($_GET['name'])) {
  $name = $_GET['name'];
  $query = "SELECT * FROM users WHERE name='$name'";
  $result = mysql_query($result) or die(mysql_error());
  if ($result) {
    while ($row = mysql_fetch_array($result)) {
      $name = $row["name"];
      echo $row['name']; // Output all information.
    }
  }
  else {
    echo 'Error with query: '.$query.'<br>'.mysql_error();
  }
}
else {
  echo 'You must specify a name.';
}
?>


#8 maxic0

maxic0
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 31 July 2006 - 06:36 PM

No it still just says that the query is empty. :(

#9 maxic0

maxic0
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 31 July 2006 - 07:55 PM

Please Reply. Someone must know the answer.

#10 maxic0

maxic0
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 31 July 2006 - 08:24 PM

For the record this was the problem.

This line was wrong..
$result = mysql_query($result) or die(mysql_error());

It should be..
$result = mysql_query($query) or die(mysql_error());

Thanks for your help everyone. :D


#11 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 31 July 2006 - 08:55 PM

Change
<?php $result = mysql_query($result) or die(mysql_error()); ?>
to
<?php $result = mysql_query($query) or die("Problem with the query: $query<br>" . mysql_error());
You are trying to use the contents of the variable $result instead of $query.

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users