SQL PHP HTML ASP JavaScript articles and free scripts to download
PHP PHP & Database Query

All details of a single record of MySQL table in one page

We have seen how to display records of a mysql table here. Now we will learn how to display one record in a single page. This is required where full details of a record are to be shown in a page. Usually the records unique id is used to collect the details from the table. So same php page is used and the value of unique id of the record is taken as a variable. Before that links to different records are displayed and on click the full details are shown. So we will fist start with displaying a group of records with link to individual records.

Displaying Links to individual records

We will use our student table where we have added two more columns ( fields ) to store address and image. As we have more records so to restrict number of records we will display records of a particular class. The query is here.

select * from student where class='Four' order by id

Using the above query we will display the records by keeping them inside an html table. At the top of the script we will connect to MySQL database.
Here is the code.

<html>
<head>
<title></title>
</head>
<body >

<?Php
require "config.php"; // connection string is here

////////Query & Data Display is here/////////

$q="select * from student where class='Four' order by id ";
echo "<table>";
foreach ($dbo->query($sql) as $nt) {
echo "<tr><td><a href=details.php?id=$nt[id]>$nt[name]</a></td><td>$nt[class]</td></tr>";
}
echo "</table>";
/////////////////////////////////////
?>
</body>
</html>

You can see we have used the name field to display a hyper link and by clicking that the individual record can be displayed in details.php page. We have formatted the hyper link to carry the student id in query string.

<a href=details.php?id=$nt[id]>$nt[name]</a>

The output of above code is here
Krish StarFour
John MikeFour
Alex JohnFour
Big JohnFour
Tade RowFour
GimmyFour
Babby JohnFour
Marry ToeeyFour

Displaying individual records in page

By clicking the hyper link the address bar will carry the student id to details.php page to use inside our query. But before using this student id we have to sanitize this data as it can be changed to carry out injection attack. There are different ways to do this but we will check if the data is numeric or not and exit the page execution if we found non-numeric value is stored in id variable. Here is the code to this part.

$id=$_GET['id']; // Collecting data from query string
if(!is_numeric($id)){ // Checking data it is a number or not
echo "Data Error";
exit; }

Now we can use this id value inside our query.

select * from student where id=:id

In above query we have collected the data for the individual record. Now we will display all the fields of the record like this.
NameKrish Star
ClassFour
Mark60
AddressKrish Star_address
Image4.jpg
Here is the code for this.

<html>
<head>
<title></title>

</head>
<body >

<?Php
require "config.php"; // database connection is here

//////Displaying Data/////////////
$id=$_GET['id']; // Collecting data from query string
if(!is_numeric($id)){ // Checking data it is a number or not
echo "Data Error";
exit;
}

$count=$dbo->prepare(select * from student where id=:id ");
$count->bindParam(":id",$id,PDO::PARAM_INT,3);

if($count->execute()){
echo " Success ";
$row = $count->fetch(PDO::FETCH_OBJ);
}

echo "<table>";
echo "
<tr bgcolor='#f1f1f1'><td><b>Name</b></td><td>$row->name</td></tr>
<tr><td><b>Class</b></td><td>$row->class</td></tr>
<tr bgcolor='#f1f1f1'><td><b>Mark</b></td><td>$row->mark</td></tr>
<tr><td><b>Address</b></td><td>$row->address</td></tr>
<tr bgcolor='#f1f1f1'><td><b>Image</b></td><td>$row->img</td></tr>
";
echo "</table>";

////////////////////
?>
</body>
</html>

Displaying Image stored in MySQL table

Usually images are stored in any folder or directory but the name of the image file is stored in a field so to display the photo we will format the image file name by using html img tag. Here is an example to display the photo in place of file name ( replace $row->img with this code ).

<img src=dir_name/$row->img>

To know more about how to store file name in table and manage images along with thumbnail using MySQL database , read photo gallery script

Photo gallery script using PHP MySQL



Here is the dump of the student table

Number of User Comments : 4


Google+
Eagertolearn19-03-2013
Great tutorial.

I was banging my head for more than 2 weeks on how to accomplish this task. Thanks for sharing. Well explained.

I would like to go 1 step further. How can I get the database result opens in a new page.



Thanks



effefef19-07-2013
Simple and Good.
Stanley28-09-2013
Hey man I was looking for this, you have safe my life, thank you very much...
John14-10-2013
Hi how can I fix the problem with Notice: Undefined index: id.

I have tested with $id = isset($_GET['id']) ? $_GET['id'] : ''; but send a Data Error; but everything is OK what am I doing wrong?
Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked



HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us

©2000-2014 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer