Professional Documents
Culture Documents
Basic PHP System-View Edit Delete Add Records
Basic PHP System-View Edit Delete Add Records
DATABASE:
-- You'll need to create a database (I named mine 'records' but it can be changed) using PHPMyAdmin
--
-- Table structure for table `students`
--
CREATE TABLE `students` (
‘id` int(11) NOT NULL auto_increment,
‘student_number’ int(10) NOT NULL,
‘firstname’ varchar(32) NOT NULL,
‘lastname’ varchar(32) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
--
-- Dumping data for table `students`
--
1. Click phpMyAdmin
1.
How to insert a record
1. Click the insert tab. Type the values for your table
CODES:
CONNECT_DB.PHP
<?php
/*
CONNECT_DB.PHP
Allows PHP to connect to your database
*/
// Connect to Database
$connection = mysql_connect($server, $user, $pass)
or die ("Could not connect to server ... \n" . mysql_error ());
mysql_select_db($db)
or die ("Could not connect to database ... \n" . 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
/*
VIEW.PHP
Displays all data from 'students' table
*/
// close table>
echo "</table>";
?>
<p><a href="new_record.php">Add a new record</a></p>
</body>
</html>
NEW_RECORD.PHP
<?php
/*
NEW_RECORD.PHP
Allows user to create a new entry in the database
*/
// check if the form has been submitted. If it has, start to process the form and save it to the database
if (isset($_POST['submit']))
{
// get form data, making sure it is valid
$student_number = mysql_real_escape_string(htmlspecialchars($_POST['student_number']));
$firstname = mysql_real_escape_string(htmlspecialchars($_POST['firstname']));
$lastname = mysql_real_escape_string(htmlspecialchars($_POST['lastname']));
// check if the form has been submitted. If it has, process the form and save it to the database
if (isset($_POST['submit']))
{
// confirm that the 'id' value is a valid integer before getting the form data
if (is_numeric($_POST['id']))
{
// get form data, making sure it is valid
$id = $_POST['id'];
$student_number = mysql_real_escape_string(htmlspecialchars($_POST['student_number']));
$firstname = mysql_real_escape_string(htmlspecialchars($_POST['firstname']));
$lastname = mysql_real_escape_string(htmlspecialchars($_POST['lastname']));
// get the 'id' value from the URL (if it exists), making sure that it is valid (checing that it is numeric/larger than 0)
if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0)
{
// query db
$id = $_GET['id'];
$result = mysql_query("SELECT * FROM students WHERE id=$id")
or die(mysql_error());
$row = mysql_fetch_array($result);
// show form
renderForm($id,$student_number, $firstname, $lastname, '');
}
else
// if no match, display result
{
echo "No results!";
}
}
else
// if the 'id' in the URL isn't valid, or if there is no 'id' value, display an error
{
echo 'Error!';
}
}
?>
DELETE_RECORD.PHP
<?php
/*
DELETE_RECORD.PHP
Deletes a specific entry from the 'students' table
*/
// check if the 'id' variable is set in URL, and check that it is valid
if (isset($_GET['id']) && is_numeric($_GET['id']))
{
// get id value
$id = $_GET['id'];
?>
VIEW_PAGINATED.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
/*
VIEW-PAGINATED.PHP
Displays all data from 'STUDENTS' table
This is a modified version of view.php that includes pagination
*/
// display pagination
// pagination
?>
<p><a href="new_record.php">Add a new record</a></p>
</body>
</html>