Jump to content

Pulling a variable from URL to new page


fat creative

Recommended Posts

I have a form that pulls records from a database based on selections from a drop down list.  On the results page, I created a link called View Details which runs another script "showdetails.php". The link sends the field ID in the URL. I did an echo statement on the showdetails.php so I can see that it is passing the right ID, however, I can't seem to display the information from the table. I created a select statement using this passed through ID, but I must have something wrong. I've spent days on this. I am so new to PHP and am happy with what I have working so far. I just need this one last piece to finish my project.If ANYONE can help, I would appreciate it.  I've been searching the boards and tutorials.  Not sure if I NEED to do a session???? But I did try and I couldn't get that to work either so I deleted it.  The form starts at www.FatCreative.com/test.html.  This is the code from my showdetails.php script:

$ID = $_GET['id'];

 

echo "id equals $ID <br />"; // to see if its passing the right variable

 

$query = "SELECT * FROM phoneList

  WHERE ID= $ID ";

 

$Firstname = $_GET['Firstname'];

 

echo "My firstname is $Firstname";

 

and this is code from the first script:

# GRAB THE VARIABLES FROM THE FORM

$Lastname = mysql_real_escape_string($_POST['Lastname']);

$Firstname = mysql_real_escape_string($_POST['Firstname']);

 

//this should select based on form selections

$query = "SELECT * FROM phoneList WHERE ID > 0";

 

if (strlen($Firstname) > 0) {

  $query .= " AND Firstname='$Firstname'";

}

         

if (strlen($Lastname) > 0) {

  $query .= " AND Lastname='$Lastname'";

}

 

$result = mysql_query($query)

    or die ("There was an error:<br />".mysql_error()."<br />Query: $query");

 

if(!mysql_num_rows($result)) //if none!!!

  echo "There were no rows returned from the database";

}

else //otherwise draw the table

{

  //put info on new lines

  echo "<table cellspacing=\"15\">\n";

  while($row = mysql_fetch_assoc($result))

  {

      extract($row);

      echo "<tr><td>";

      echo "$Firstname $Lastname<br />";

      echo "$Email<br />";

      echo "$Phone<br />";

      echo "<a href=\"showdetails.php?id=$ID\">View Details</a>";

      echo "</td></tr>\n";

  }

}

echo "</table>\n";

 

Thanks in advance for any suggestions!

Link to comment
https://forums.phpfreaks.com/topic/105783-pulling-a-variable-from-url-to-new-page/
Share on other sites

this seems to be unnecessary

$ID = $_GET['id'];

echo "id equals $ID
"; // to see if its passing the right variable

$query = "SELECT * FROM phoneList
           WHERE ID= $ID ";

$Firstname = $_GET['Firstname'];

echo "My firstname is $Firstname";

I see no Firstname passed through the url so $_GET['Firstname'] would return nothing. As far as the echo statement, you need to do:

echo "My First Name is" . $Firstname . "!";

That goes for all echo statements. You can't echo variables with quotes around them.

 

Also, it doesn't look like you're accessing the database. This is a correct database call:

mysql_query($query, $link) or die(mysql_error());

 

where $link:

$link = mysql_connect("localhost","username","password")
  or die(mysql_error());

 

Lets conquer these problems then we'll see what's left.

Since you are only passing the id you need to query the database to get the rest of the info

$ID = $_GET['id'];

echo "id equals $ID
"; // to see if its passing the right variable

$query = "SELECT * FROM phoneList
           WHERE ID= $ID ";
$res = mysql_query($query) or die (mysql_error());
$r = mysql_fetch_assoc($res);  // no need for loop since you only returning one row
$Firstname = $r['Firstname'];

echo "My firstname is $Firstname";

 

Ray

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.