Forum Moderators: coopster

Message Too Old, No Replies

PHP and MySQL help

linking MySQL fields

         

bassata

7:43 pm on Sep 10, 2008 (gmt 0)

10+ Year Member



I have a .php page that query's mysql database, and links the job_description to the job_name. The problem I'm having is that when I click on the job name hyperlink, it passes the data from the job_description field to the url address bar in a browser. This is a stupid question, but I'm fairly new to php......how can I get the link to pass the data to another .php page?

cameraman

8:07 pm on Sep 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When you have a url like:
http://www.example.com/somepage.php?filter=all&id=7
the stuff to the right of the question mark is called the query string. In PHP, that information is available to you through something called the $_GET [us2.php.net] superglobal.
In this case, the somepage.php script would be able to see:
$_GET['filter']
and
$_GET['id']

bassata

8:14 pm on Sep 10, 2008 (gmt 0)

10+ Year Member



O.k....that's understood.....I have a page called job.php that looks like:
<?PHP

$job_description = $_GET['job_description'];

?>

now....my original page that I'm having trouble with is this:
<table border="1">
<tr>
<th>Location</th>
<th>Job Name</th>
<th>Effective Date</th>
</tr>
<?php
//Include our login information
include('db_login.php');
// Connect
$connection = mysql_connect("mydatabase", "user", "pass");
if (!$connection){
die("Could not connect to the database:
<br />". mysql_error());
}
// Select the database
$db_select = mysql_select_db("orchardweb"); if (!$db_select){
die ("Could not select the database:
<br />". mysql_error());
}
// Assign the query
$query = "SELECT * FROM hr";
// Execute the query
$result = mysql_query($query);
if (!$result){
die ("Could not query the database:
<br />". mysql_error());
}

// Fetch and display the results
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
$location = $row["location"];
//HERE'S THE PROBLEM $job_name = '<a href=job.php"' . $row['job_description'] . '">' . $row['job_name'] . '</a><br />';
$effective_date = $row["effective_date"];
echo "<tr>";
echo "<td>$location</td>";
echo "<td>$job_name</td>";
echo "<td>$effective_date</td>";
echo "</tr>";
}
// Close the connection
mysql_close($connection);
?>
</table>
</body>
</html>

bassata

8:29 pm on Sep 10, 2008 (gmt 0)

10+ Year Member



BTW....thanks for getting back so quickly.

cameraman

8:50 pm on Sep 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



//HERE'S THE PROBLEM $job_name = '<a href=job.php?"' . $row['job_description'] . '">' . $row['job_name'] . '</a><br />';

Unless the job description starts with a question mark you need the one above. You may also need to urlencode [us3.php.net] $row['job_description']. Personally I would use a shorter identifier, like a unique record id for the table, instead of [what I would guess to be] a longer text string as a parameter.
Also, although you can technically do it that way, you want to set it up as a variable=value sort of thing, like
<a href=job.php?detail=' . $row['job_....
Otherwise it's a bit trickier to get it back out on the other end.