Creating a hyperlink to a specific record
Creating a hyperlink to a specific record
am 09.01.2006 20:05:43 von Bob Sanderson
I have a php page which contains the code:
echo "
";
echo "$JobNumber | ";
echo "$Description | ";
echo "$NSN | ";
echo "$Manufacturer | ";
echo "$PartNumber | ";
echo "
";
This produces a web page listing all records in my database. I would now
like to be able to click on a job number in that page and open a different
output form (results.php) displaying only that record. I already have a
search form which accepts a Job Number and then transfers that to
results.php using the variable 'searchterm'. What I want is to create a
hyperlink for each record that will open results.php and display only the
job number I selected.
Any help will be greatly appreciated.
Re: Creating a hyperlink to a specific record
am 09.01.2006 23:25:13 von Jonathan
Bob Sanderson wrote:
> I have a php page which contains the code:
>
> echo "
";
> echo "$JobNumber | ";
> echo "$Description | ";
> echo "$NSN | ";
> echo "$Manufacturer | ";
> echo "$PartNumber | ";
> echo "
";
>
> This produces a web page listing all records in my database. I would now
> like to be able to click on a job number in that page and open a different
> output form (results.php) displaying only that record. I already have a
> search form which accepts a Job Number and then transfers that to
> results.php using the variable 'searchterm'. What I want is to create a
> hyperlink for each record that will open results.php and display only the
> job number I selected.
>
> Any help will be greatly appreciated.
Generate a hyperlink with the job number as parameter in the first page.
If you now access $_GET['jobnumber'] it shoudl hold the value from the
lionk you clickjed on and present the job details with a select query
from the database.
Jonathan
Re: Creating a hyperlink to a specific record
am 10.01.2006 14:03:57 von Bob Sanderson
Jonathan wrote in
news:43c2e2cb$0$10084$ba620dc5@text.nova.planet.nl:
> If you now access $_GET['jobnumber'] it should hold the value from the
> lionk you clickjed on and present the job details with a select query
> from the database.
Thanks for the reply. I'm fairly new to PHP. The link makes sense, but I
don't understand the statement above. Can you please elaborate on how I
would implement that?
Re: Creating a hyperlink to a specific record
am 10.01.2006 19:32:57 von Robert Billing
Among the wreckage we found a fragment on which Bob Sanderson had
scratched:
> Thanks for the reply. I'm fairly new to PHP. The link makes sense, but I
> don't understand the statement above. Can you please elaborate on how I
> would implement that?
If it's any consolation so am I. I was poring over a hot computer until
about 11 last night trying to learn PHP very quickly from the manual for a
one-off job.
In the end I produced these two scripts. They present my address book as
two web pages. The first is a simple list of all the names and addresses
in the book, which is a single table. The salutation (Mr., Mrs., etc.) is
a clickable link which brings up a full version of the address on another
page.
The first script (hello.php) puts up the summary page:
echo "\n" ;
$Db=mysql_connect("localhost","unclebob");
mysql_select_db("addressbook",$Db);
$Res=mysql_query("select * from main order by Last", $Db);
while ($Row=mysql_fetch_row($Res)) {
printf ( " | %s | %s | %s | %s | %s | %s | %s |
\n",
$Row[0], $Row[4], $Row[1], $Row[2], $Row[10], $Row[11], $Row[12], $Row[13], $Row[14] ) ;
}
?>
The second script is full.php which puts up the full details for one
record:
$Id = $_GET['tag'] ;
$Db=mysql_connect("localhost","unclebob");
mysql_select_db("addressbook",$Db);
$Res=mysql_query("select * from main where Id=$Id", $Db);
printf ( "
%s %s %s
\n", mysql_result ( $Res, 0, 4 ), mysql_result ( $Res, 0, 1 ), mysql_result ( $Res, 0, 2 ) ) ;
printf ( "
%s\n", mysql_result ( $Res, 0, 10 ) ) ;
printf ( "
%s\n", mysql_result ( $Res, 0, 11 ) ) ;
printf ( "
%s\n", mysql_result ( $Res, 0, 12 ) ) ;
printf ( "
%s\n", mysql_result ( $Res, 0, 13 ) ) ;
printf ( "
%s\n", mysql_result ( $Res, 0, 14 ) ) ;
printf ( "
\n" ) ;
printf ( "
Work: %s\n", mysql_result ( $Res, 0, 6 ) ) ;
printf ( "
Home: %s\n", mysql_result ( $Res, 0, 7 ) ) ;
printf ( "
Fax: %s\n", mysql_result ( $Res, 0, 8 ) ) ;
printf ( "
Mobile: %s\n", mysql_result ( $Res, 0, 9 ) ) ;
echo "
" ;
?>
These are exceedingly crude, in fact they are my first sortie into PHP.
However they do illustrate how to make the magic work. The first script
generates hyperlinks of the form href="full.php?tag=123" where 123 is in
fact the primary key of the database. The second is called with this as
the URL, and the array _GET is filled with the parameters. In this case
there is exactly one parameter which has the name "tag" and the value 123.
I'm NOT advocating this as good PHP, in fact it's pretty rough coding.
However it does work and should illustrate the particular spell you are
trying to make work.
Re: Creating a hyperlink to a specific record
am 10.01.2006 19:50:21 von Jonathan
Bob Sanderson wrote:
> Jonathan wrote in
> news:43c2e2cb$0$10084$ba620dc5@text.nova.planet.nl:
>
>
>>If you now access $_GET['jobnumber'] it should hold the value from the
>>lionk you clickjed on and present the job details with a select query
>>from the database.
>
>
> Thanks for the reply. I'm fairly new to PHP. The link makes sense, but I
> don't understand the statement above. Can you please elaborate on how I
> would implement that?
Suppose we have a hyperlink that has jobnumber 123. I would look like this:
If you clikc on this hyperlink it will open the results.php page. In
this page you can access the value for the jobnumber you transmitted
with the hyperlink.
All values that are transmitted in the URL are transmitted as a GET
request you can access these values using the super global $_GET. This
is a special variabele that is an array of all the values passed to the
page.
You can now query the database with the specified number and then
display the result of the query, like with the code below.
// Check whether the value for jobnumber is transmitted
if (isset($_GET['jobnumber'])) {
// Put the value in a separate variable
$jobnumber = $_GET['jobnumber'];
// Query the database for the details of the chosen jobnumber
$result = mysql_query("select jobnumber, description, NSN,
manufacturer, partnumber from table where jobnumber = $jobnumber");
// Check result
// This shows the actual query sent to MySQL, and the error. Useful
for debugging.
if (!$result) {
$message = "Invalid query: " . mysql_error() . "\n";
$message .= "Whole query: " . $query;
die($message);
}
// Use result
// Attempting to print $result won't allow access to information in
the resource
// One of the mysql result functions must be used
// See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(),
etc.
while ($row = mysql_fetch_assoc($result)) {
echo $row['jobnumber'] . "\n";
echo $row['description'] . "\n";
echo $row['NSN'] . "\n";
echo $row['manufacturer'] . "\n";
echo $row['partnumber'] . "\n";
}
} else {
die("No valid jobnumber specified!");
}
?>
I hope this might help you a little further.
Jonathan
Re: Creating a hyperlink to a specific record
am 10.01.2006 19:58:13 von Bob Sanderson
Jonathan wrote in news:43c401f1$0$10080$ba620dc5
@text.nova.planet.nl:
> I hope this might help you a little further.
Sure did, thanks again.
Re: Creating a hyperlink to a specific record
am 10.01.2006 20:59:14 von gordonb.1htar
>Suppose we have a hyperlink that has jobnumber 123. I would look like this:
>
>
>
>If you clikc on this hyperlink it will open the results.php page. In
>this page you can access the value for the jobnumber you transmitted
>with the hyperlink.
>
>All values that are transmitted in the URL are transmitted as a GET
>request you can access these values using the super global $_GET. This
>is a special variabele that is an array of all the values passed to the
>page.
>
>You can now query the database with the specified number and then
>display the result of the query, like with the code below.
>
>
> // Check whether the value for jobnumber is transmitted
> if (isset($_GET['jobnumber'])) {
>
> // Put the value in a separate variable
> $jobnumber = $_GET['jobnumber'];
SQL injection attack alert!
I think you want something like:
$jobnumber = addslashes($_GET['jobnumber']);
OR
$jobnumber = mysql_real_escape_string($_GET['jobnumber']);
or you can verify that it is all digits before running the query.
>
> // Query the database for the details of the chosen jobnumber
> $result = mysql_query("select jobnumber, description, NSN,
>manufacturer, partnumber from table where jobnumber = '$jobnumber'");
Gordon L. Burditt
Re: Creating a hyperlink to a specific record
am 10.01.2006 23:30:09 von Jonathan
Gordon Burditt wrote:
>
>
> SQL injection attack alert!
>
> I think you want something like:
> $jobnumber = addslashes($_GET['jobnumber']);
> OR
> $jobnumber = mysql_real_escape_string($_GET['jobnumber']);
>
> or you can verify that it is all digits before running the query.
>
>
>
>
>
> Gordon L. Burditt
You are completely right... forgot about it! Most of the times I use
PEAR::HTML_QuickForm like classes and check the entries before after
submit but before committing the entries to the database.
Jonathan