Jump to content

[SOLVED] Dynamic link to open mysql query in a new page


Stoosh

Recommended Posts

Hello,

 

I've started teaching myself PHP heavily after a big learning curve with design, html & css.

 

If you have a look at http://www.aftersunset.com.au/enquiry/test.php, its just a basic query from a database, what I'm looking to do is add an extra table column with a link such as

 

http://www.aftersunset.com.au/enquiry/test.php?id=1 which opens to a page which displays the comments or info that the user submitted which is also stored in the database (row is "comments")

 

<?php require_once('dbconnect.php');?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<head>
</head>
<body>

<?php

// query
$query = "SELECT * FROM enquiry";

// execute
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

// check for rows
if (mysql_num_rows($result) > 0) {
    // yes
    // print, repeat
    echo "<table cellpadding=10 border=1>";
    while($row = mysql_fetch_assoc($result)) {
        echo "<tr>";
	        echo "<td>".$row['id']."</td>";
        echo "<td>".$row['name']."</td>";
        echo "<td>".$row['company']."</td>";
        echo "<td>".$row['email']."</td>";
	echo "<td>".$row['phone']."</td>";
        echo "</tr>";
    }
    echo "</table>";
}
else {
    // no
    // error page
    echo "No rows found!";
}

// free result set memory
mysql_free_result($result);

// close connection
mysql_close($connect);

?>

</body>
</html>

 

This is my first PHP script handwritten so it may be a little sloppy  ::)

Link to comment
Share on other sites

add this line where you want it to display in your table

 

echo '<td><a href="

http://www.aftersunset.com.au/enquiry/test.php?id=' . $id . '">' . $id . '</a></td>';

 

will create the links

 

then look up the php command $GET  to understand how to retrive the data

 

 

heres a quick example

 

$Name = mysql_escape_string(trim(htmlentities($_GET['Name'])));

$query = "SELECT * FROM business  WHERE Name = '$Name'";

 

of the get method  to run a query

Link to comment
Share on other sites

Thanks very much!!

 

It's calling the data but the table is still shown, how do I remove the table when the ID link is clicked?

 

<?php
// create query
$query = "SELECT * FROM enquiry";

// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

// see if any rows were returned
if (mysql_num_rows($result) > 0) {
    // yes
    // print them one after another
    echo "<table cellpadding=10 border=1>";
    while($row = mysql_fetch_assoc($result)) {
        echo "<tr>";
	        echo "<td>".$row['id']."</td>";
        echo "<td>".$row['name']."</td>";
        echo "<td>".$row['company']."</td>";
        echo "<td>".$row['email']."</td>";
	echo "<td>".$row['phone']."</td>";
	echo '<td><a href="http://www.aftersunset.com.au/enquiry/test.php?id=' . $row['id'] . '">View Comments</a></td>';
        echo "</tr>";
    }
    echo "</table>";
}
else {
    // no
    // print status message
    echo "No rows found!";
}

$sql = "SELECT * FROM `enquiry` WHERE `id` = " . mysql_real_escape_string ( $_GET['id'] );
if ( mysql_query ( $sql ) )
{
	$query = mysql_query ( $sql );
	$row = mysql_fetch_assoc ( $query );

	echo $row['comments'];
}
else {
	die ( mysql_error () );
}

// free result set memory
mysql_free_result($result);

// close connection
mysql_close($connect);

?>

Link to comment
Share on other sites

the table is still shown because you are calling to the same page :)

 

try linking to from test.php to test2.php?id='$id'

 

then on test2.php you can re-query data WHERE id='$id'; (because you just passed that value in your URL) and format how you like.

 

 

OR

 

you could maybe add a clause if (isset($id)){ and do your formatting there....

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.