How to Assign Variable On_Click and Pass to Another Page

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
jeffimperial
Forum Newbie
Posts: 13
Joined: Sun Feb 01, 2009 9:54 pm

How to Assign Variable On_Click and Pass to Another Page

Post by jeffimperial »

Hello there. I'm very new to this, and I hope to learn more. I've gotten great help from here before, so I was hoping some of you can help me again.

I've got a script that displays a MySQL table in HTML format. The code goes like this:

Code: Select all

 
<?php
// open the MySQL connection
mysql_connect("host_name_here", "username_here", "password_here") or die(mysql_error());
mysql_select_db("database_name_here") or die(mysql_error());
 
// Get all the data from the "table_name_here" table
// ORDER BY id DESC should arrange the entries by the ID Number in reverse order. LIMIT 30 should return only the last 30 entries
$result = mysql_query("SELECT * FROM table_name_here ORDER BY ID DESC LIMIT 30") 
or die(mysql_error());  
 
// Start generating the HTML table on-the-fly
echo "<table width='100%' border='1' cellspacing='1' cellpadding='1'>";
 
// generate the table headers and format in accordance with CSS definitions
// Field7 is the ID field so no need to display here
echo "<tr class='link ind_1' align='center'> <th>Field 1</th> <th> Field 2</th> <th> Field 3</th> <th> Field 4</th> <th> Field 5</th> <th> Field 6</th> </tr>";
 
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
    // Print out the contents of each row into a table except field "ID"
    echo "<tr align='center'><td>"; 
    echo $row['Field1'];
    echo "</td><td>"; 
    echo $row['Field2'];
    echo "</td><td>"; 
    echo $row['Field3'];
    echo "</td><td>"; 
    echo $row['Field4'];
    echo "</td><td>"; 
    echo $row['Field5'];
    echo "</td><td>"; 
    echo $row['Field6'];
    echo "</td></tr>"; 
} 
 
echo "</table>";
?>
 
Now, what I want to do on this page is just display fields 'Field1' and 'Field2' in the same format above. But this time, I want Field2 to be a hyperlink to another page with a different script. Up to this point, I still know what to do.

Here's the part I don't know how to go about. I want this second script to display the complete row. 'Field1' is a Date, and 'Field2' is a name. What I want is when I click on the name, it's going to bring me to that second script which displays everything else for that particular name (or row).

I hope I'm making sense here. Any kind of help will be much appreciated. If you could show me a snippet of some code, or point me to a tutorial of some kind that would be great. As I said, I'm still learning. Thanks!
Last edited by jeffimperial on Fri Mar 06, 2009 3:58 am, edited 1 time in total.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Display single row from on click

Post by Benjamin »

It doesn't sound like you have tried anything. The solution is fairly simple. What have you tried?
jeffimperial
Forum Newbie
Posts: 13
Joined: Sun Feb 01, 2009 9:54 pm

Re: Display single row from on click

Post by jeffimperial »

That's the thing. I'm trying to Google stuff on this but I'm getting no workable results. I've been to these pages and I don't think I know where to go from here...

http://www.filecart.com/forum/showthread.php?t=9
http://www.dreamincode.net/forums/showtopic20102.htm
http://www.webmasterworld.com/databases ... 140240.htm

Ideas?

***

Edit:
jeffimperial
Forum Newbie
Posts: 13
Joined: Sun Feb 01, 2009 9:54 pm

Re: Display single row from on click

Post by jeffimperial »

Ok, I think I have it...

Code: Select all

 
$query = 'SELECT * FROM table_name_here WHERE id_field_here';
 
Question is, how do I assign a variable for "id_field_here" from clicking from the previous page?
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: How to Assign Variable On_Click and Pass to Another Page

Post by papa »

A simple example to get you in the right direction:

Code: Select all

<a href="page.php?id=x">Your ID</a>
Next page

Code: Select all

 
$query = 'SELECT * FROM table_name_here WHERE $_GET["id"]';
 
For security, validate the variable and also use:

Code: Select all

mysq_real_escape_string()
Post Reply