Wednesday, 30 April 2014

How to Insert Edit and Delete in PHP

In this article we will learn how to insert, edit, update and delete records from the database using PHP. Here we have to create five pages such as config.php (to provide the connection), view.php (to display the records from the database), insert.php (to insert records into the database), edit.php (to edit records), and delete.php (to delete records).

Table Structure

-- phpMyAdmin SQL Dump
-- version 2.10.1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: May 21, 2012 at 10:42 AM
-- Server version: 5.0.45
-- PHP Version: 5.2.5

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

--
-- Database: `Work`
--

-- --------------------------------------------------------

--
-- Table structure for table `employee`
--

CREATE TABLE `employee` (
`id` int(12) NOT NULL auto_increment,
`name` varchar(255) NOT NULL,
`address` varchar(255) NOT NULL,
`city` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

--
-- Dumping data for table `employee`
--

INSERT INTO `employee` (`id`, `name`, `address`, `city`) VALUES
(1, 'Raj', '10 street dane', 'Pune'),
(2, 'Ravi', '12907A 53 St NW', 'Mumbai'),
(3, 'Rahul', '3rd Floor, 888 Fort Street', 'Noida'),
(4, 'Harry', 'Sir Frederick W Haultain Building 9811 109 ST NW', 'London'),
(5, 'Ian', 'Suite 303, 13220 St. Albert Trail', 'Sydney'),
(6, 'Shaun', '9700 Jasper Avenue', 'Perth');

Code part

config.php

<?php

/* Database Connection */

$sDbHost = 'localhost';
$sDbName = 'work';
$sDbUser = 'root';
$sDbPwd = '';

$dbConn = mysql_connect ($sDbHost, $sDbUser, $sDbPwd) or die ('MySQL connect failed. ' . mysql_error());
mysql_select_db($sDbName,$dbConn) or die('Cannot select database. ' . mysql_error());

?>

view.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>View Records</title>
</head>
<body>
<?php

include('config.php');

$result = mysql_query("SELECT * FROM employee")
or die(mysql_error());

echo "<table border='1' cellpadding='10'>";
echo "<tr>
<th><font color='Red'>Id</font></th>
<th><font color='Red'>Name</font></th>
<th><font color='Red'>Address</font></th>
<th><font color='Red'>City</font></th>
<th><font color='Red'>Edit</font></th>
<th><font color='Red'>Delete</font></th>
</tr>";

while($row = mysql_fetch_array( $result ))
{

echo "<tr>";
echo '<td><b><font color="#663300">' . $row['id'] . '</font></b></td>';
echo '<td><b><font color="#663300">' . $row['name'] . '</font></b></td>';
echo '<td><b><font color="#663300">' . $row['address'] . '</font></b></td>';
echo '<td><b><font color="#663300">' . $row['city'] . '</font></b></td>';
echo '<td><b><font color="#663300"><a href="edit.php?id=' . $row['id'] . '">Edit</a></font></b></td>';
echo '<td><b><font color="#663300"><a href="delete.php?id=' . $row['id'] . '">Delete</a></font></b></td>';
echo "</tr>";

}

echo "</table>";
?>
<p><a href="insert.php">Insert new record</a></p>
</body>
</html>

insert.php

<?php
function valid($name, $address,$city, $error)
{
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Insert Records</title>
</head>
<body>
<?php

if ($error != '')
{
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
}
?>

<form action="" method="post">
<table border="1">
<tr>
<td colspan="2"><b><font color='Red'>Insert Records </font></b></td>
</tr>
<tr>
<td width="179"><b><font color='#663300'>Name<em>*</em></font></b></td>
<td><label>
<input type="text" name="name" value="<?php echo $name; ?>" />
</label></td>
</tr>

<tr>
<td width="179"><b><font color='#663300'>Address<em>*</em></font></b></td>
<td><label>
<input type="text" name="address" value="<?php echo $address; ?>" />
</label></td>
</tr>

<tr>
<td width="179"><b><font color='#663300'>City<em>*</em></font></b></td>
<td><label>
<input type="text" name="city" value="<?php echo $city; ?>" />
</label></td>
</tr>

<tr align="Right">
<td colspan="2"><label>
<input type="submit" name="submit" value="Insert Records">
</label></td>
</tr>
</table>
</form>
</body>
</html>
<?php
}

include('config.php');

if (isset($_POST['submit']))
{

$name = mysql_real_escape_string(htmlspecialchars($_POST['name']));
$address = mysql_real_escape_string(htmlspecialchars($_POST['address']));
$city = mysql_real_escape_string(htmlspecialchars($_POST['city']));

if ($name == '' || $address == '' || $city == '')
{

$error = 'Please enter the details!';

valid($name, $address, $city,$error);
}
else
{

mysql_query("INSERT employee SET name='$name', address='$address', city='$city'")
or die(mysql_error());

header("Location: view.php");
}
}
else
{
valid('','','','');
}
?>

edit.php

<?php
function valid($id, $name, $address,$city, $error)
{
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Edit Records</title>
</head>
<body>
<?php

if ($error != '')
{
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
}
?>

<form action="" method="post">
<input type="hidden" name="id" value="<?php echo $id; ?>"/>

<table border="1">
<tr>
<td colspan="2"><b><font color='Red'>Edit Records </font></b></td>
</tr>
<tr>
<td width="179"><b><font color='#663300'>Name<em>*</em></font></b></td>
<td><label>
<input type="text" name="name" value="<?php echo $name; ?>" />
</label></td>
</tr>

<tr>
<td width="179"><b><font color='#663300'>Address<em>*</em></font></b></td>
<td><label>
<input type="text" name="address" value="<?php echo $address; ?>" />
</label></td>
</tr>

<tr>
<td width="179"><b><font color='#663300'>City<em>*</em></font></b></td>
<td><label>
<input type="text" name="city" value="<?php echo $city; ?>" />
</label></td>
</tr>

<tr align="Right">
<td colspan="2"><label>
<input type="submit" name="submit" value="Edit Records">
</label></td>
</tr>
</table>
</form>
</body>
</html>
<?php
}

include('config.php');

if (isset($_POST['submit']))
{

if (is_numeric($_POST['id']))
{

$id = $_POST['id'];
$name = mysql_real_escape_string(htmlspecialchars($_POST['name']));
$address = mysql_real_escape_string(htmlspecialchars($_POST['address']));
$city = mysql_real_escape_string(htmlspecialchars($_POST['city']));


if ($name == '' || $address == '' || $city == '')
{

$error = 'ERROR: Please fill in all required fields!';


valid($id, $name, $address,$city, $error);
}
else
{

mysql_query("UPDATE employee SET name='$name', address='$address' ,city='$city' WHERE id='$id'")
or die(mysql_error());

header("Location: view.php");
}
}
else
{

echo 'Error!';
}
}
else

{

if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0)
{

$id = $_GET['id'];
$result = mysql_query("SELECT * FROM employee WHERE id=$id")
or die(mysql_error());
$row = mysql_fetch_array($result);

if($row)
{

$name = $row['name'];
$address = $row['address'];
$city = $row['city'];

valid($id, $name, $address,$city,'');
}
else
{
echo "No results!";
}
}
else

{
echo 'Error!';
}
}
?>

delete.php

<?php
include('config.php');

if (isset($_GET['id']) && is_numeric($_GET['id']))
{
$id = $_GET['id'];

$result = mysql_query("DELETE FROM employee WHERE id=$id")
or die(mysql_error());

header("Location: view.php");
}
else

{
header("Location: view.php");
}
?>

Output

PHP1.jpg

PHP2.jpg

PHP3.jpg

After inserting 6 records
PHP4.jpg
PHP5.jpg


After editing the address of Name (Raj)

PHP6.jpg
 

Sunday, 27 April 2014

Message Board PHP Project

Making the Database: Database: `forum2`



--
-- Table structure for table `languages`
--

CREATE TABLE IF NOT EXISTS `languages` (
  `lang_id` tinyint(3) unsigned NOT NULL AUTO_INCREMENT,
  `lang` varchar(60) NOT NULL,
  `lang_eng` varchar(20) NOT NULL,
  PRIMARY KEY (`lang_id`),
  UNIQUE KEY `lang` (`lang`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;

--
-- Dumping data for table `languages`
--

INSERT INTO `languages` (`lang_id`, `lang`, `lang_eng`) VALUES
(1, 'English', 'English'),
(2, 'Português', 'Portuguese'),
(3, 'Français', 'French'),
(4, 'Norsk', 'Norwegian'),
(5, 'Romanian', 'Romanian'),
(6, '????????', 'Greek'),
(7, 'Deutsch', 'German'),
(8, 'Srpski', 'Serbian'),
(9, '???', 'Japanese'),
(10, 'Nederlands', 'Dutch');

-- --------------------------------------------------------

--
-- Table structure for table `posts`
--

CREATE TABLE IF NOT EXISTS `posts` (
  `post_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `thread_id` int(10) unsigned NOT NULL,
  `user_id` int(10) unsigned NOT NULL,
  `message` text NOT NULL,
  `posted_on` datetime NOT NULL,
  PRIMARY KEY (`post_id`),
  KEY `thread_id` (`thread_id`),
  KEY `user_id` (`user_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=19 ;

--
-- Dumping data for table `posts`
--

INSERT INTO `posts` (`post_id`, `thread_id`, `user_id`, `message`, `posted_on`) VALUES
(1, 1, 3, 'Jeg har nettopp gått over til PHP 5.0 og forsøkte å benytte meg av mine gamle scripts. Dette viste seg å være noe vanskelig ettersom de bare generer feil. Hovedproblemet virker å være at jeg ikke får tilgang til variabler som tidligere var tilgjengelige. Noen som har noen forslag?', '2007-10-29 04:15:52'),
(2, 1, 1, 'Har du sjekket om variablene du prøver å få tilgang på er superglobals? Dette forandret seg fra 4.2 og utover, tror jeg...', '2007-10-29 04:20:52'),
(3, 1, 4, 'Hva er superglobals?', '2007-10-29 04:30:30'),
(4, 1, 1, 'http://no.php.net/variables.predefined', '2007-10-30 06:16:30'),
(5, 1, 5, 'Linken Terje ga er manualsiden, men du kan også ta en titt på http://www.linuxjournal.com/article/6559 for en grundig innføring og forklaring. Lykke til!', '2007-10-29 10:26:57'),
(6, 2, 2, 'Har sett flere sider hvor man må skrive inn noen tall for å kunne laste ned, registrere seg, osv. Er dette PHP? Kan noen hjelpe meg å få til en slik?', '2007-10-29 22:45:57'),
(7, 3, 1, 'Je voudrais afficher simplement une nouvelle page HTML ou PHP dans mon\r\nnavigateur web depuis un bout de programme en PHP.\r\nLancer par exemple http://www.google.fr/ depuis un condition if (a>0)\r\nJe trouve pas de solution sur google ni dans mes bouquins', '2007-10-29 04:42:38'),
(8, 3, 2, 'header("Location: http://www.domaine.com");\r\nAttention, cette fonction doit être utilisé avant toute sortie vers le navigateur... le moindre echo et c''est foutu\r\n', '2007-10-29 05:17:38'),
(9, 4, 3, 'J''utilise PHP List. J''ai un formulaire contact avec une case à cocher\r\npermettant de choisir de s''abonner à une newsletter. Je traite ce\r\nformullaire en PHP.\r\nExiste-t-il un moyen au moment où je traite le formulaire d''ajouter la\r\npersonne dans ma liste de diffusion PHP List ?\r\nJe suppose que le problème n''est pas compliqué mais je n''ai pas encore\r\ntrouvé comment faire...\r\n', '2007-10-29 04:43:28'),
(10, 4, 5, 'Dans ce genre de problématiques le mieux est de :\r\na/ regarder de quelle manière php list gère les abonnés dans la base ( en\r\ngros, regarder la structure de la table ).\r\nb/ créer une fonction qui ajoute manuellement les données de votre\r\nformulaire dans la ou les tables mysql utilisée(s) par php list ( en se\r\nméfiant des doublons : est ce que cette adresse est déjà dans la liste ? )\r\nc/ faire un ou plusieurs tests...\r\n', '2007-10-31 12:06:28'),
(11, 5, 2, 'PHP ?????MySql?????? ?????????????\r\n???????????????????????????', '2007-10-29 04:57:55'),
(12, 5, 3, '??????????????', '2007-10-29 04:57:55'),
(13, 5, 4, '?????????????????', '2007-10-29 04:58:10'),
(14, 7, 1, 'This is the body of the sample thread. This is the body of the sample thread. This is the body of the sample thread. ', '2007-10-29 05:12:02'),
(15, 7, 1, 'I like your thread. It''s simple and sweet.', '2007-10-29 05:44:07'),
(16, 8, 1, 'its new thread for all', '2014-04-27 10:02:49'),
(17, 8, 1, 'whats uppp', '2014-04-27 10:03:03'),
(18, 3, 1, '\r\n?', '2014-04-27 10:06:13');

-- --------------------------------------------------------

--
-- Table structure for table `threads`
--

CREATE TABLE IF NOT EXISTS `threads` (
  `thread_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `lang_id` tinyint(3) unsigned NOT NULL,
  `user_id` int(10) unsigned NOT NULL,
  `subject` varchar(150) NOT NULL,
  PRIMARY KEY (`thread_id`),
  KEY `lang_id` (`lang_id`),
  KEY `user_id` (`user_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;

--
-- Dumping data for table `threads`
--

INSERT INTO `threads` (`thread_id`, `lang_id`, `user_id`, `subject`) VALUES
(1, 4, 1, 'Byttet til PHP 5.0 fra PHP 4.0 - variabler utilgjengelige'),
(2, 4, 2, 'Automatisk bildekontroll'),
(3, 3, 5, 'Lancer une Page HTML en PHP'),
(4, 3, 4, 'Ajouter des adresses a PHP List depuis un formulaire'),
(5, 9, 4, '???????'),
(7, 1, 1, 'Sample Thread'),
(8, 1, 1, 'This is new thread ');

-- --------------------------------------------------------

--
-- Table structure for table `users`
--

CREATE TABLE IF NOT EXISTS `users` (
  `user_id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
  `lang_id` tinyint(3) unsigned NOT NULL,
  `time_zone` varchar(30) NOT NULL,
  `username` varchar(30) NOT NULL,
  `pass` char(40) NOT NULL,
  `email` varchar(60) NOT NULL,
  PRIMARY KEY (`user_id`),
  UNIQUE KEY `username` (`username`),
  UNIQUE KEY `email` (`email`),
  KEY `login` (`username`,`pass`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;

--
-- Dumping data for table `users`
--

INSERT INTO `users` (`user_id`, `lang_id`, `time_zone`, `username`, `pass`, `email`) VALUES
(1, 1, 'America/New_York', 'troutster', '5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8', 'email@example.com'),
(2, 7, 'Europe/Berlin', 'Ute', 'a5a7569327c9925049693dbfda8cd1d0106f4550', 'email1@example.com'),
(3, 4, 'Europe/Oslo', 'Silje', 'e408d64f8bcd85ebb7d84bc13540c5683ce1b6c9', 'email2@example.com'),
(4, 2, 'America/Sao_Paulo', 'João', '2d2553bcdeda4aa9d3b09965e90c6d283a8cfce1', 'email3@example.com'),
(5, 1, 'Pacific/Auckland', 'kiwi', '9c147500fd397d8f90a2e5005524fee515099760', 'kiwi@example.org');

-- --------------------------------------------------------

--
-- Table structure for table `words`
--

CREATE TABLE IF NOT EXISTS `words` (
  `word_id` tinyint(3) unsigned NOT NULL AUTO_INCREMENT,
  `lang_id` tinyint(3) unsigned NOT NULL,
  `title` varchar(80) NOT NULL,
  `intro` tinytext NOT NULL,
  `home` varchar(30) NOT NULL,
  `forum_home` varchar(40) NOT NULL,
  `language` varchar(40) NOT NULL,
  `register` varchar(30) NOT NULL,
  `login` varchar(30) NOT NULL,
  `logout` varchar(30) NOT NULL,
  `new_thread` varchar(40) NOT NULL,
  `subject` varchar(30) NOT NULL,
  `body` varchar(30) NOT NULL,
  `submit` varchar(30) NOT NULL,
  `posted_on` varchar(30) NOT NULL,
  `posted_by` varchar(30) NOT NULL,
  `replies` varchar(30) NOT NULL,
  `latest_reply` varchar(40) NOT NULL,
  `post_a_reply` varchar(40) NOT NULL,
  PRIMARY KEY (`word_id`),
  UNIQUE KEY `lang_id` (`lang_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

--
-- Dumping data for table `words`
--

INSERT INTO `words` (`word_id`, `lang_id`, `title`, `intro`, `home`, `forum_home`, `language`, `register`, `login`, `logout`, `new_thread`, `subject`, `body`, `submit`, `posted_on`, `posted_by`, `replies`, `latest_reply`, `post_a_reply`) VALUES
(1, 1, 'PHP and MySQL for Dynamic Web Sites: The Forum!', '<p>Welcome to our site....please use the links above...blah, blah, blah.</p>\r\n<p>Welcome to our site....please use the links above...blah, blah, blah.</p>', 'Home', 'Forum Home', 'Language', 'Register', 'Login', 'Logout', 'New Thread', 'Subject', 'Body', 'Submit', 'Posted on', 'Posted by', 'Replies', 'Latest Reply', 'Post a Reply'),
(2, 4, 'PHP og MySQL for Dyaniske Websider: Forumet!', '<p>"Velkommen til denne siden. Introduksjonstekst."</p>\r\n<p>"Velkommen til denne siden. Introduksjonstekst."</p>', 'Hjem', 'Forumet Hjem', 'Språk', 'Registrer deg', 'Logg inn', 'Logg ut', 'Ny tråd', 'Emne', 'Kropp', 'SUBMIT', 'Lagt til', 'Lagt til av', 'REPLIES', 'LATEST REPLY', 'POST A REPLY'),
(3, 5, 'Forumul PHP si MySQL pentru site-uri web dinamice', 'Bine ati venit pe acest site. Text introductiv. Bine ati venit pe acest site. Text introductiv. Bine ati venit pe acest site. Text introductiv.', 'Inregistrare', 'Conectare', 'Deconectare', 'Discutie noua', 'Subiect', 'Continut', 'Limba', 'Acasa', 'SUBMIT', 'Afisat pe', 'Afisat de', 'REPLIES', 'LATEST REPLY', 'POST A REPLY', 'Forumul Acasa'),
(4, 3, 'Sites internet dynamiques avec PHP et MySQL : le forum!', 'Bienvenue sur ce site. Texte d''introduction. Bienvenue sur ce site. Texte d''introduction. Bienvenue sur ce site. Texte d''introduction.', 'S''enregistrer', 'Se connecter', 'Déconnexion', 'Nouvelle discussion', 'Sujet', 'Contenu', 'Langue', 'Accueil', 'Soumettez', 'Posté le', 'Posté par', 'Réponses', 'La Plus défunte Réponse', 'Signalez une réponse', 'Le Forum Accueil'),
(5, 7, 'PHP en MYSQL voor Dynamische Websites: Het Forum!', 'Welkom op deze site! Inleidingstekst. Hier vind je alles op het gebied van php!', 'Registreer', 'Uitloggen', 'Inloggen', 'Nieuw onderwerp', 'Onderwerp', 'Body', 'Taal', 'Index', 'SUBMIT', 'Geplaatst op', 'Geplaatst door', 'REPLIES', 'LATEST REPLY', 'POST A REPLY', 'Forum Index'),
(6, 9, 'PHP ?MySql???????????????????????', '???????????????????????????? ????????????????????????????', '???', '???????????', '??', '?????', ' ????', '?????', '??? ???? ', '??', '??', '?????', '???', ' ???', '???', '?????', '?????');









<?php # Script 15.3 - index.php
// This is the main page for the site.

// Include the HTML header:
include ('includes/header.html');

// The content on this page is introductory text
// pulled from the database, based upon the
// selected language:
echo $words['intro'];

// Include the HTML footer file:
include ('includes/footer.html');
?>











<?php # Script 15.4 - forum.php
// This page shows the threads in a forum.
include ('includes/header.html');

// Retrieve all the messages in this forum...

// If the user is logged in and has chosen a time zone,
// use that to convert the dates and times:
if (isset($_SESSION['user_tz'])) {
    $first = "CONVERT_TZ(p.posted_on, 'UTC', '{$_SESSION['user_tz']}')";
    $last = "CONVERT_TZ(p.posted_on, 'UTC', '{$_SESSION['user_tz']}')";
} else {
    $first = 'p.posted_on';
    $last = 'p.posted_on';
}

// The query for retrieving all the threads in this forum, along with the original user,
// when the thread was first posted, when it was last replied to, and how many replies it's had:
$q = "SELECT t.thread_id, t.subject, username, COUNT(post_id) - 1 AS responses, MAX(DATE_FORMAT($last, '%e-%b-%y %l:%i %p')) AS last, MIN(DATE_FORMAT($first, '%e-%b-%y %l:%i %p')) AS first FROM threads AS t INNER JOIN posts AS p USING (thread_id) INNER JOIN users AS u ON t.user_id = u.user_id WHERE t.lang_id = {$_SESSION['lid']} GROUP BY (p.thread_id) ORDER BY last DESC";
$r = mysqli_query($dbc, $q);
if (mysqli_num_rows($r) > 0) {

    // Create a table:
    echo '<table width="100%" border="0" cellspacing="2" cellpadding="2" align="center">
        <tr>
            <td align="left" width="50%"><em>' . $words['subject'] . '</em>:</td>
            <td align="left" width="20%"><em>' . $words['posted_by'] . '</em>:</td>
            <td align="center" width="10%"><em>' . $words['posted_on'] . '</em>:</td>
            <td align="center" width="10%"><em>' . $words['replies'] . '</em>:</td>
            <td align="center" width="10%"><em>' . $words['latest_reply'] . '</em>:</td>
        </tr>';

    // Fetch each thread:
    while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
   
        echo '<tr>
                <td align="left"><a href="read.php?tid=' . $row['thread_id'] . '">' . $row['subject'] . '</a></td>
                <td align="left">' . $row['username'] . '</td>
                <td align="center">' . $row['first'] . '</td>
                <td align="center">' . $row['responses'] . '</td>
                <td align="center">' . $row['last'] . '</td>
            </tr>';
   
    }
   
    echo '</table>'; // Complete the table.
   
} else {
    echo '<p>There are currently no messages in this forum.</p>';
}

// Include the HTML footer file:
include ('includes/footer.html');
?>








<?php # Script 15.8 - search.php
// This page displays and handles a search form.

// Include the HTML header:
include ('includes/header.html');

// Show the search form:
echo '<form action="search.php" method="get" accept-charset="utf-8">
<p><em>' . $words['search'] . '</em>: <input name="terms" type="text" size="30" maxlength="60" ';
   
// Check for existing value:
if (isset($_GET['terms'])) {
    echo 'value="' . htmlspecialchars($_GET['terms']) . '" ';
}

// Complete the form:
echo '/><input name="submit" type="submit" value="' . $words['submit'] . '" /></p></form>';

if (isset($_GET['terms'])) { // Handle the form.

    // Clean the terms:
    $terms = mysqli_real_escape_string($dbc, htmlentities(strip_tags($_GET['terms'])));

    // Run the query...
    $q = "SELECT * FROM languages WHERE lang_id = 100";
    $r = mysqli_query($dbc, $q);
    if (mysqli_num_rows($r) > 0) {
        echo '<h2>Search Results</h2>';
    } else {
        echo '<p>No results found.</p>';
    }

}

// Include the HTML footer file:
include ('includes/footer.html');
?>








<?php # Script 15.7 - post.php
// This page handles the message post.
// It also displays the form if creating a new thread.
include ('includes/header.html');

if (isset($_POST['submitted'])) { // Handle the form.

    // Language ID ($lid) is in the session.
    // Validate thread ID ($tid), which may not be present:
    $tid = FALSE;
    if (isset($_POST['tid']) && is_numeric($_POST['tid']) ) {
        $tid = (int) $_POST['tid'];
        if ($tid <= 0) {
            $tid = FALSE;
        }
    }

    // If there's no thread ID, a subject must be provided:
    if (!$tid && empty($_POST['subject'])) {
        $subject = FALSE;
        echo '<p>Please enter a subject for this post.</p>';
    } elseif (!$tid && !empty($_POST['subject'])) {
        $subject = htmlspecialchars(strip_tags($_POST['subject']));
    } else { // Thread ID, no need for subject.
        $subject = TRUE;
    }
   
    // Validate the body:
    if (!empty($_POST['body'])) {
        $body = htmlentities($_POST['body']);
    } else {
        $body = FALSE;
        echo '<p>Please enter a body for this post.</p>';
    }
   
    if ($subject && $body) { // OK!
   
        // Add the message to the database...
       
        if (!$tid) { // Create a new thread.
            $q = "INSERT INTO threads (lang_id, user_id, subject) VALUES ({$_SESSION['lid']}, {$_SESSION['user_id']}, '" . mysqli_real_escape_string($dbc, $subject) . "')";
            $r = mysqli_query($dbc, $q);
            if (mysqli_affected_rows($dbc) == 1) {
                $tid = mysqli_insert_id($dbc);
            } else {
                echo '<p>Your post could not be handled due to a system error.</p>';
            }

        }
       
        if ($tid) { // Add this to the replies table:

            $q = "INSERT INTO posts (thread_id, user_id, message, posted_on) VALUES ($tid, {$_SESSION['user_id']}, '" . mysqli_real_escape_string($dbc, $body) . "', UTC_TIMESTAMP())";
            $r = mysqli_query($dbc, $q);
            if (mysqli_affected_rows($dbc) == 1) {
                echo '<p>Your post has been entered.</p>';
            } else {
                echo '<p>Your post could not be handled due to a system error.</p>';
            }

        }
   
    } else { // Include the form:
        include ('post_form.php');
    }

} else { // Display the form:
   
    include ('post_form.php');

}

include ('includes/footer.html');
?>







<?php # Script 15.5 - read.php
// This page shows the messages in a thread.
include ('includes/header.html');
// Check for a thread ID...
$tid = FALSE;
if (isset($_GET['tid']) && is_numeric($_GET['tid'])) {
    $tid = (int) $_GET['tid'];
    if ($tid > 0) { // Check against the database...
        // Convert the date if the user is logged in:
        if (isset($_SESSION['user_tz'])) {
            $posted = "CONVERT_TZ(p.posted_on, 'UTC', '{$_SESSION['user_tz']}')";
        } else {
            $posted = 'p.posted_on';
        }
        // Run the query:
        $q = "SELECT t.subject, p.message, username, DATE_FORMAT($posted, '%e-%b-%y %l:%i %p') AS posted FROM threads AS t LEFT JOIN posts AS p USING (thread_id) INNER JOIN users AS u ON p.user_id = u.user_id WHERE t.thread_id = $tid ORDER BY p.posted_on ASC";
        $r = mysqli_query($dbc, $q);
        if (!(mysqli_num_rows($r) > 0)) {
            $tid = FALSE; // Invalid thread ID!
        }
    } // End of ($tid > 0) IF.
} // End of isset($_GET['tid']) IF.
if ($tid) { // Get the messages in this thread...
    $printed = FALSE; // Flag variable.
    // Fetch each:
    while ($messages = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
        // Only need to print the subject once!
        if (!$printed) {
            echo "<h2>{$messages['subject']}</h2>\n";
            $printed = TRUE;
        }
        // Print the message:
        echo "<p>{$messages['username']} ({$messages['posted']})<br />{$messages['message']}</p><br />\n";
    } // End of WHILE loop.
    // Show the form to post a message:
    include ('post_form.php');
} else { // Invalid thread ID!
    echo '<p>This page has been accessed in error.</p>';
}
include ('includes/footer.html');
?>







<?php # Script 15.6 - post_form.php
// This page shows the form for posting messages.
// It's included by other pages, never called directly.
// Redirect if this page is called directly:
if (!isset($words)) {
    header ("Location: http://www.example.com/index.php");
    exit();
}
// Only display this form if the user is logged in:
if (isset($_SESSION['user_id'])) {
    // Display the form:
    echo '<form action="post.php" method="post" accept-charset="utf-8">';
    // If on read.php...
    if (isset($tid) && $tid) {
        // Print a caption:
        echo '<h3>' . $words['post_a_reply'] . '</h3>';
        // Add the thread ID as a hidden input:
        echo '<input name="tid" type="hidden" value="' . $tid . '" />';
    } else { // New thread
        // Print a caption:
        echo '<h3>' . $words['new_thread'] . '</h3>';
        // Create subject input:
        echo '<p><em>' . $words['subject'] . '</em>: <input name="subject" type="text" size="60" maxlength="100" ';
        // Check for existing value:
        if (isset($subject)) {
            echo "value=\"$subject\" ";
        }
        echo '/></p>';
    } // End of $tid IF.
    // Create the body textarea:
    echo '<p><em>' . $words['body'] . '</em>: <textarea name="body" rows="10" cols="60">';
    if (isset($body)) {
        echo $body;
    }
    echo '</textarea></p>';
    // Finish the form:
    echo '<input name="submit" type="submit" value="' . $words['submit'] . '" /><input name="submitted" type="hidden" value="TRUE" />
    </form>';
} else {
    echo '<p>You must be logged in to post messages.</p>';
}
?>






Footer.html
<!-- Script 15.2 - footer.html -->
    </td>
  </tr>

  <tr>
    <td colspan="2" align="center">&copy; 2013 Aamir Javed  &amp; www.E-pagespk.com</td>
  </tr>

</table>
</body>
</html>








<?php # Script 15.1 - header.html
/* This script...
 * - starts the HTML template.
 * - indicates the encoding using header().
 * - starts the session.
 * - gets the language-specific words from the database.
 * - lists the available languages.
 */

// Indicate the encoding:
header ('Content-Type: text/html; charset=UTF-8');

// Start the session:
session_start();

// For testing purposes:
$_SESSION['user_id'] = 1;
$_SESSION['user_tz'] = 'America/New_York';
//$_SESSION = array();

// Need the database connection:
require_once('../mysqli_connect.php');

// The language ID is stored in the session.
// Check for a new language ID...
if (isset($_GET['lid']) && is_numeric($_GET['lid'])) {
    $_SESSION['lid'] = (int) $_GET['lid'];
} elseif (!isset($_SESSION['lid'])) {
    $_SESSION['lid'] = 1; // Default.
}

// Get the words for this language.
$words = FALSE; // Flag variable.
if ($_SESSION['lid'] > 0) {
    $q = "SELECT * FROM words WHERE lang_id = {$_SESSION['lid']}";
    $r = mysqli_query($dbc, $q);
    if (mysqli_num_rows($r) == 1) {
        $words = mysqli_fetch_array($r, MYSQLI_ASSOC);
    }
}

// If we still don't have the words, get the default language:
if (!$words) {
    $_SESSION['lid'] = 1; // Default.
    $q = "SELECT * FROM words WHERE lang_id = {$_SESSION['lid']}";
    $r = mysqli_query($dbc, $q);
    $words = mysqli_fetch_array($r, MYSQLI_ASSOC);
}

mysqli_free_result($r);
?>
<!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" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title><?php echo $words['title']; ?></title>
    <style type="text/css" media="screen">
        body { background-color: #ffffff; }
       
        .content {
            background-color: #f5f5f5;
            padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px;
            margin-top: 10px; margin-right: 10px; margin-bottom: 10px; margin-left: 10px;
        }
       
        a.navlink:link { color: #003366; text-decoration: none; }
        a.navlink:visited { color: #003366; text-decoration: none; }
        a.navlink:hover { color: #cccccc; text-decoration: none; }
               
        .title { 
            font-size: 24px; font-weight: normal; color: #ffffff;
            margin-top: 5px; margin-bottom: 5px; margin-left: 20px;
            padding-top: 5px; padding-bottom: 5px; padding-left: 20px;
        }
    </style>
</head>
<body>

<table width="90%" border="0" cellspacing="10" cellpadding="0" align="center">

  <tr>
    <td colspan="2" bgcolor="#003366" align="center"><p class="title"><?php echo $words['title']; ?></p></td>
  </tr>
 
  <tr>
    <td valign="top" nowrap="nowrap" width="10%">
<b>
<?php // Display links:

// Default links:
echo '<a href="index.php" class="navlink">' . $words['home'] . '</a><br />
<a href="forum.php" class="navlink">' . $words['forum_home'] . '</a><br />';

// Display links based upon login status:
if (isset($_SESSION['user_id'])) {

    // If this is the forum page, add a link for posting new threads:
    if (stripos($_SERVER['PHP_SELF'], 'forum.php')) {
        echo '<a href="post.php" class="navlink">' . $words['new_thread'] . '</a><br />';
    }
   
    // Add the logout link:
    echo '<a href="logout.php" class="navlink">' . $words['logout'] . '</a><br />';
   
} else {

    // Register and login links:
    echo '<a href="register.php" class="navlink">' . $words['register'] . '</a><br />
    <a href="login.php" class="navlink">' . $words['login'] . '</a><br />';
   
}

// For choosing a forum/language:
echo '</b><p><form action="forum.php" method="get">
<select name="lid">
<option value="0">' . $words['language'] . '</option>
';

// Retrieve all the languages...
$q = "SELECT lang_id, lang FROM languages ORDER BY lang_eng ASC";
$r = mysqli_query($dbc, $q);
if (mysqli_num_rows($r) > 0) {
    while ($menu_row = mysqli_fetch_array($r, MYSQLI_NUM)) {
        echo "<option value=\"$menu_row[0]\">$menu_row[1]</option>\n";
    }
}
mysqli_free_result($r);
unset($menu_row);

echo '</select><br />
<input name="submit" type="submit" value="' . $words['submit'] . '" />
</form></p>
    </td>
    
    <td valign="top" class="content">';
?>









Tuesday, 22 April 2014

CMS PHP BASIC PROJECT SESSION ADMIN USER PANEL






 SQL FILE

 CREATE TABLE  `basic_cms`.`menus` (
`m_id` INT( 10 ) NOT NULL AUTO_INCREMENT ,
`m_title` VARCHAR( 50 ) NOT NULL ,
PRIMARY KEY (  `m_id` )
) ENGINE = INNODB


CREATE TABLE  `basic_cms`.`pages` (
`p_id` INT( 10 ) NOT NULL AUTO_INCREMENT ,
`p_title` VARCHAR( 100 ) NOT NULL ,
`p_desc` TEXT NOT NULL ,
PRIMARY KEY (  `p_id` )
) ENGINE = INNODB


CREATE TABLE  `basic_cms`.`admin_login` (
`u_id` INT( 10 ) NOT NULL AUTO_INCREMENT ,
`user_name` VARCHAR( 30 ) NOT NULL ,
`user_pass` VARCHAR( 30 ) NOT NULL ,
PRIMARY KEY (  `u_id` )
) ENGINE = INNODB

INSERT INTO  `basic_cms`.`menus` (
`m_id` ,
`m_title`
)
VALUES (
NULL ,  'News'
), (
NULL ,  'Showbiz'
), (
NULL ,  'Cricket'
), (
NULL ,  'Blogging'
), (
NULL ,  'Seo Tips'
);










/////includes/db.php
<?php

$con = mysql_connect("localhost","root","");
$db = mysql_select_db('basic_cms', $con);

?>

///////////////////////////////
////////index.php



<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="content-type" content="text/html" />
    <meta name="author" content="gencyolcu" />

    <title>Basic CMS</title>
   
    <style type="text/css">
    a:link{
    color: white;
    text-decoration:none;
    }
   
   
    h2{
        color:red;
        margin: 5px;
        padding:5px;
        font-size:28px;
        text-decoration:underline;
        text-align: center;
       
    }
   
    </style>
   
   
   
</head>

<body>
<table width='990' border='2' align='center'>
<!---Header Starts----->
<tr>
<td> <?php include ("includes/header.php"); ?></td>
</tr>

<!---Navigation Starts----->
<tr>
<td >
<table border='0'>

<?php
include ("includes/db.php");

$query = "select * from menus";
$run = mysql_query($query);

while($row = mysql_fetch_array($run)){
   
    $m_title = $row[1];
   
    echo "<td bgcolor='black'width='100'  align='center'>
    <a href='pages.php?pages=$m_title'> $m_title</a> </td>   ";



}



?>


</table>

</td>
</tr>

<!---Main Contant Starts----->
<tr>
<td bgcolor='pink' height='500' valign='top'>
<h2>Welcome to my website</h2>
<p>This website for all users who wants to learn php in advanceThis website for all users who wants to learn php in advanceThis website for all users who wants to learn php in advanceThis website for all users who wants to learn php in advanceThis website for all users who wants to learn php in advanceThis website for all users who wants to learn php in advance </p>

<center><img src="sanam.jpeg"></img></center>


</td>
</tr>
<!---Footer Starts----->
<tr>
<td bgcolor='black' height='60' align='center'>
<h2 style='color:white;'>Created by Aamir Javed</h2>

</td>
</tr>



</table>
</body>
</html>

///////////////////////////////////////////////////////////////////////////////////////////////////////////
//pages.php



<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="content-type" content="text/html" />
    <meta name="author" content="gencyolcu" />

    <title>Basic CMS</title>
   
    <style type="text/css">
    a:link{
    color: white;
    text-decoration:none;
    }
   
   
    h2{
        color:red;
        margin: 5px;
        padding:5px;
        font-size:28px;
        text-decoration:underline;
        text-align: center;
       
    }
   
    </style>
   
   
   
</head>

<body>
<table width='990' border='2' align='center'>
<!---Header Starts----->
<tr>
<td> <?php include ("includes/header.php"); ?></td>
</tr>

<!---Navigation Starts----->
<tr>
<td >
<table border='0'>

<?php
include ("includes/db.php");

$query = "select * from menus";
$run = mysql_query($query);

while($row = mysql_fetch_array($run)){
   
    $m_title = $row[1];
   
    echo "<td bgcolor='black'width='100'  align='center'>
    <a href='pages.php?pages=$m_title'> $m_title</a> </td>   ";



}



?>


</table>

</td>
</tr>

<!---Main Contant Starts----->
<tr>
<td >

<table border='0' width='800' align='center' >

<tr>
<?php
$pages = $_GET['pages'];
$query = "select * from pages where p_title='$pages' ";
$run = mysql_query($query);
while ($row = mysql_fetch_assoc($run)){
   
 echo "<td bgcolor='aqua' >" . "h2" . $row['p_title']. "</h2>". $row['p_desc']."</td>";  
   
   
}




?>

</tr>

</table>

</td>
</tr>
<!---Footer Starts----->
<tr>
<td bgcolor='black' height='60' align='center'>
<h2 style='color:white;'>Created by Aamir Javed</h2>

</td>
</tr>



</table>
</body>
</html>


//////////////////////////////////////////////////////
//admin_panel.php


<?php

 session_start();

if(!$_SESSION['admin_name'] ){
   
    header('location:login.php?error= You are not administrator');
   
}


?>



<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="content-type" content="text/html" />
    <meta name="author" content="gencyolcu" />

    <title>Admin Panel</title>
</head>

<body>

<div style='width:100%; height: 100px; background: blue; text-align: center; font-size:33px; color:white;'>
Welcome to admin panel

</div>
Welcome: <font size='4' color='red'>
<?php

echo $_SESSION['admin_name'];

?>

</font>
<h2><a href='logout.php' style="margin-top: 10px;">Logout!</a></h2>

<h2 align= 'center'><?php echo @$_GET['deleted'] ?></h2>

<h2 align= 'center'><?php echo @$_GET['inserted'] ?></h2>

<h2 align= 'center'><?php echo @$_GET['logged'] ?></h2>

<h2><a href='admin_panel.php?view_page=view page'>View Pages</a></h2>
<h2><a href='insert_page.php'>Insert New Page</a></h2>
<h2><a href='admin_panel.php?view_menu=view menu'>View Menu</a></h2>
<h2><a href='insert_menu.php'>Insert New Menu</a></h2>

<?php


include ("includes/db.php");

if(isset($_GET['view_page']))
{
   
   
   


?>

<table width='1000' border='2' align='center' >

<tr>
<td align='center' bgcolor='yellow' colspan='6'><h2> All pages here !</h2></td></tr>

<tr align='center'>
<th>Page No.</th>
<th>Page Title</th>
<th>Page Content</th>
<th>Delete</th>
</tr>


<tr>
<?php
//$page = $_GET['view_page'];
$query = "select * from pages ";
$run = mysql_query($query);

while($row = mysql_fetch_array($run) )
{
   
    $p_id = $row['p_id'];
    $p_title = $row['1'];
    $p_desc = substr($row['2'],0,100);


?>


<td><?php echo $p_id;   ?></td>
<td><?php echo $p_title;  ?></td>
<td><?php  echo $p_desc; ?></td>
<td><a href='delete_page.php?del_page=<?php echo $p_id; ?> '> Delete</a></td>
</tr>
<?php

}}

?>

</table>


<?php

if(isset($_GET['view_menu']))
{

?>

<table width='400' border='3' align='center'>

<tr>
<td colspan='5' bgcolor='aqua' align='center'><h2>All menus here</h2></td>
</tr>


<tr align='center'>
<th>Menu No:</th>
<th>Menu Title:</th>
<th>Delete</th>
</tr>



<?php

 $query = "select * from menus";
 $run = mysql_query($query);

while ($row= mysql_fetch_array($run))
{

$m_id = $row['m_id'];
$m_title = $row[1];   


?>

<tr align='center'>


<td><?php echo $m_id ?></td>
<td><?php echo $m_title ?></td>
<td><a href='delete_menu.php?del_menu=<?php echo $m_id;  ?>' >Delete</a>   </td>
</tr>

<?php }}  ?>


</table>


</body>
</html>


//////////////////////////////
///insert_page.php


<?php

 session_start();

if(!$_SESSION['admin_name'] ){
   
    header('location:login.php?error= You are not administrator');
   
}


?>

<html>
<?php
include ("includes/db.php");
include ("admin_panel.php");



?>


<form action='' method='post'>
<table width='500' border='3' align='center'>

<tr>
<td colspan='4' bgcolor='pink' align='center' > <h2>Insert New Page</h2></td>
</tr>

<tr>
<th>Page Title:</th>
<td><input type='text' name='page_title'></td>
</tr>

<tr>
<th>Page Content:</th>
<td><textarea name='page_content' cols='20' rows='10'></textarea>
</td>
</tr>

<tr>
<td align='center' colspan='6'> <input type='submit' name='submit' value='submit'/></td>

</tr>




</table>



</form>
</html>

<?php


if(isset($_POST['submit'])){
   
    $post_title = $_POST['page_title'];
     $post_content = $_POST['page_content'];
    
     $query = "insert into pages (p_title,p_desc) values ('$post_title','$post_content') ";
    
    
   if(mysql_query($query))
  
   {
  echo "<script>window.open('admin_panel.php?inserted=A new page has been inserterd','_self') </script>";
       
     }
    
    
         
}


//////////////////////////////////
//delete_page.php

?>


<?php
include ("includes/db.php");

$delete_page = $_GET['del_page'];

$query = "delete from pages where p_id='$delete_page'";

if(mysql_query($query)){

echo "<script>window.open('admin_panel.php?deleted= Your page has been deleted....','_self')</script>";  



   
}


?>

 ////////////////////
 //delete_menu.php


<?php
include ("includes/db.php");
$delete_menu = $_GET['del_menu'];
$query = "delete from menus where m_id='$delete_menu'";
if(mysql_query($query)){
echo "<script>window.open('admin_panel.php?deleted=Your Menu has been deleted....','_self')</script>";  
}
?>


////////////////////////////////////////////////
//login.php


<?php
session_start();
?>
<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="content-type" content="text/html" />
    <meta name="author" content="gencyolcu" />
    <title>Admin Login</title>
</head>
<body>
<form action='login.php' method='post' >
<table width='400' align='center' border='5' >
<tr>
<td colspan='5' align='center' bgcolor='green' >
<h2>Admin Login</h2>
</td>
</tr>
<tr>
<th aligin='right'>User Name:</th>
<td><input type='text' name='admin_name' /></td>
</tr>
<tr>
<th aligin='right'>User Password:</th>
<td><input type='text' name='admin_pass' /></td>
</tr>
<tr>
<td colspan='6' align='center' >
<input type= 'submit' name='submit' value='Login' />
</tr>
</table>
</form>
<h2 align='center'><?php echo @$_GET['logout'] ?></h2>
<h2 align='center'><?php echo @$_GET['error'] ?></h2>
</body>
</html>
<?php
include ("includes/db.php");
if(isset($_POST['submit']))
{
   $admin_name = $_SESSION['admin_name']= $_POST['admin_name'];
    $admin_pass = $_POST['admin_pass'];
 $query = "select * from admin_login where user_name ='$admin_name' AND user_pass ='$admin_pass'";
   $run = mysql_query($query);
    if(mysql_num_rows($run)==1){
echo "<script>window.open('admin_panel.php?logged= You are Loggedin Successsfully!','_self') </script>";
    }
else
{
    echo "<script> alert('User Name or passwrd is incorrect')</script>";
}   
}
?>


///////////////////////
//logout.php


<?php
session_start();
session_destroy();
header('location: login.php?logout= You have loggedout! Come Back soon....');
?>




Tuesday, 15 April 2014

How to use PHP & MySQL Display Multiple Column and Paging/Pagination

add just image folder and insert some images in it.



<html>
<head>
<title>phpmyqlmaster.blogspot.com</title>
</head>
<body>
    <?php
        $objConnect = mysql_connect("localhost","root","") or die(mysql_error());
        $objDB = mysql_select_db("test");
        $strSQL = "SELECT * FROM gallery";
        $objQuery = mysql_query($strSQL);
        $Num_Rows = mysql_num_rows($objQuery);

        $Per_Page = 4;   // Per Page

        $Page = $_GET["Page"];
        if(!$_GET["Page"])
        {
            $Page=1;
        }

        $Prev_Page = $Page-1;
        $Next_Page = $Page+1;

        $Page_Start = (($Per_Page*$Page)-$Per_Page);
        if($Num_Rows<=$Per_Page)
        {
            $Num_Pages =1;
        }
        else if(($Num_Rows % $Per_Page)==0)
        {
            $Num_Pages =($Num_Rows/$Per_Page) ;
        }
        else
        {
            $Num_Pages =($Num_Rows/$Per_Page)+1;
            $Num_Pages = (int)$Num_Pages;
        }

        $strSQL .=" order  by GalleryID ASC LIMIT $Page_Start , $Per_Page";
        $objQuery  = mysql_query($strSQL);


        echo"<table border=\"0\"  cellspacing=\"1\" cellpadding=\"1\"><tr>";
        $intRows = 0;
        while($objResult = mysql_fetch_array($objQuery))
        {
            echo "<td>";
            $intRows++;
    ?>
            <center>
                <img src="images/<?php echo $objResult["Picture"]; ?>"><br>
                <?php echo $objResult["GalleryName"];?>
                <br>
            </center>
    <?php
            echo"</td>";
            if(($intRows)%2==0)
            {
                echo"</tr>";
            }
        }
        echo"</tr></table>";
    ?>

        <br>
        Total <?php echo $Num_Rows; ?> Record : <?php echo $Num_Pages; ?> Page :
        <?php
        if($Prev_Page)
        {
            echo " <a href='$_SERVER[SCRIPT_NAME]?Page=$Prev_Page'><< Back</a> ";
        }

        for($i=1; $i<=$Num_Pages; $i++){
            if($i != $Page)
            {
                echo "[ <a href='$_SERVER[SCRIPT_NAME]?Page=$i'>$i</a> ]";
            }
            else
            {
                echo "<b> $i </b>";
            }
        }
        if($Page!=$Num_Pages)
        {
            echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$Next_Page'>Next>></a> ";
        }
        ?>


</body>
</html>
<?php
mysql_close($objConnect);
?>



PHP MYSQL DATABASE


CREATE TABLE `gallery` (
  `GalleryID` int(11) NOT NULL auto_increment,
  `GalleryName` varchar(100) NOT NULL,
  `Picture` varchar(100) NOT NULL,
  PRIMARY KEY  (`GalleryID`)
) ENGINE=MyISAM ;


INSERT INTO `gallery` VALUES (1, 'Picture 1', 'img1.jpg');
INSERT INTO `gallery` VALUES (2, 'Picture 2', 'img2.jpg');
INSERT INTO `gallery` VALUES (3, 'Picture 3', 'img3.jpg');
INSERT INTO `gallery` VALUES (4, 'Picture 4', 'img4.jpg');
INSERT INTO `gallery` VALUES (5, 'Picture 5', 'img5.jpg');
INSERT INTO `gallery` VALUES (6, 'Picture 6', 'img6.jpg');
INSERT INTO `gallery` VALUES (7, 'Picture 7', 'img7.jpg');
INSERT INTO `gallery` VALUES (8, 'Picture 8', 'img8.jpg');