Skip to content

Commit cd8722d

Browse files
committed
Initial Commit
0 parents  commit cd8722d

11 files changed

+383
-0
lines changed

add.html

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<html>
2+
<head>
3+
<title>Add Data</title>
4+
<script type="text/javascript">
5+
function validate() {
6+
if (document.form1.name.value == '') {
7+
alert('Please provide your name');
8+
document.form1.name.focus();
9+
return false;
10+
}
11+
if (document.form1.email.value == '') {
12+
alert('Please provide your email');
13+
document.form1.email.focus();
14+
return false;
15+
}
16+
return true;
17+
}
18+
</script>
19+
</head>
20+
21+
<body>
22+
<a href="index.php">Home</a>
23+
<br/><br/>
24+
<div id="msg"></div>
25+
<!--<form action="add.php" method="post" name="form1" onsubmit = "return(validate());">-->
26+
<form action="add.php" method="post" name="form1" >
27+
<table width="25%" border="0">
28+
<tr>
29+
<td>Name</td>
30+
<td><input type="text" name="name"></td>
31+
</tr>
32+
<tr>
33+
<td>Age</td>
34+
<td><input type="text" name="age"></td>
35+
</tr>
36+
<tr>
37+
<td>Email</td>
38+
<td><input type="text" name="email"></td>
39+
</tr>
40+
<tr>
41+
<td></td>
42+
<td><input type="submit" name="Submit" value="Add"></td>
43+
</tr>
44+
</table>
45+
</form>
46+
</body>
47+
</html>
48+

add.php

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<html>
2+
<head>
3+
<title>Add Data</title>
4+
</head>
5+
6+
<body>
7+
<?php
8+
//including the database connection file
9+
include_once("classes/Crud.php");
10+
include_once("classes/Validation.php");
11+
12+
$crud = new Crud();
13+
$validation = new Validation();
14+
15+
if(isset($_POST['Submit'])) {
16+
$name = $_POST['name'];
17+
$age = $_POST['age'];
18+
$email = $_POST['email'];
19+
20+
$msg = $validation->check_empty($_POST, array('name', 'age', 'email'));
21+
$check_age = $validation->is_age_valid($_POST['age']);
22+
$check_email = $validation->is_email_valid($_POST['email']);
23+
24+
// checking empty fields
25+
if($msg != null) {
26+
echo $msg;
27+
//link to the previous page
28+
echo "<br/><a href='javascript:self.history.back();'>Go Back</a>";
29+
} elseif (!$check_age) {
30+
echo 'Please provide proper age.';
31+
} elseif (!$check_email) {
32+
echo 'Please provide proper email.';
33+
}
34+
else {
35+
// if all the fields are filled (not empty)
36+
37+
//insert data to database
38+
$result = $crud->execute("INSERT INTO users(name,age,email) VALUES('$name','$age','$email')");
39+
40+
//display success message
41+
echo "<font color='green'>Data added successfully.";
42+
echo "<br/><a href='index.php'>View Result</a>";
43+
}
44+
}
45+
?>
46+
</body>
47+
</html>

classes/Crud.php

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
include_once 'DbConfig.php';
3+
4+
class Crud extends DbConfig
5+
{
6+
public function __construct()
7+
{
8+
parent::__construct();
9+
}
10+
11+
public function getData($query)
12+
{
13+
$result = $this->connection->query($query);
14+
15+
if ($result == false) {
16+
return false;
17+
}
18+
19+
$rows = array();
20+
21+
while ($row = $result->fetch_assoc()) {
22+
$rows[] = $row;
23+
}
24+
25+
return $rows;
26+
}
27+
28+
public function execute($query)
29+
{
30+
$result = $this->connection->query($query);
31+
32+
if ($result == false) {
33+
echo 'Error: cannot execute the command';
34+
return false;
35+
} else {
36+
return true;
37+
}
38+
}
39+
40+
public function delete($id, $table)
41+
{
42+
$query = "DELETE FROM $table WHERE id = $id";
43+
44+
$result = $this->connection->query($query);
45+
46+
if ($result == false) {
47+
echo 'Error: cannot delete id ' . $id . ' from table ' . $table;
48+
return false;
49+
} else {
50+
return true;
51+
}
52+
}
53+
}
54+
?>

classes/DbConfig.php

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
class DbConfig
3+
{
4+
private $_host = 'localhost';
5+
private $_username = 'root';
6+
private $_password = 'root';
7+
private $_database = 'test';
8+
9+
protected $connection;
10+
11+
public function __construct()
12+
{
13+
if (!isset($this->connection)) {
14+
15+
$this->connection = new mysqli($this->_host, $this->_username, $this->_password, $this->_database);
16+
17+
if (!$this->connection) {
18+
echo 'Cannot connect to database server';
19+
exit;
20+
}
21+
}
22+
23+
return $this->connection;
24+
}
25+
}
26+
?>

classes/Validation.php

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
class Validation
3+
{
4+
public function check_empty($data, $fields)
5+
{
6+
$msg = null;
7+
foreach ($fields as $value) {
8+
if (empty($data[$value])) {
9+
$msg .= "$value field empty <br />";
10+
}
11+
}
12+
return $msg;
13+
}
14+
15+
public function is_age_valid($age)
16+
{
17+
//if (is_numeric($age)) {
18+
if (preg_match("/^[0-9]+$/", $age)) {
19+
return true;
20+
}
21+
return false;
22+
}
23+
24+
public function is_email_valid($email)
25+
{
26+
//if (preg_match("/^[_a-z0-9-+]+(\.[_a-z0-9-+]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/", $email)) {
27+
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
28+
return true;
29+
}
30+
return false;
31+
}
32+
}
33+
?>

config.php

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
/*
4+
// mysql_connect("database-host", "username", "password")
5+
$conn = mysql_connect("localhost","root","root")
6+
or die("cannot connected");
7+
8+
// mysql_select_db("database-name", "connection-link-identifier")
9+
@mysql_select_db("test",$conn);
10+
*/
11+
12+
$mysqli = mysqli_connect("localhost", "root", "root", "test");
13+
14+
//$res = mysqli_query($mysqli, "SELECT 'Please, do not use ' AS _msg FROM DUAL");
15+
//$row = mysqli_fetch_assoc($res);
16+
17+
?>

database.sql

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
create database test;
2+
3+
use test;
4+
5+
CREATE TABLE `users` (
6+
`id` int(11) NOT NULL auto_increment,
7+
`name` varchar(100) NOT NULL,
8+
`age` int(3) NOT NULL,
9+
`email` varchar(100) NOT NULL,
10+
PRIMARY KEY (`id`)
11+
);

delete.php

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
//including the database connection file
3+
include_once("classes/Crud.php");
4+
5+
$crud = new Crud();
6+
7+
//getting id of the data from url
8+
$id = $_GET['id'];
9+
10+
//deleting the row from table
11+
//$result = $crud->execute("DELETE FROM users WHERE id=$id");
12+
$result = $crud->delete($id, 'users');
13+
14+
if ($result) {
15+
//redirecting to the display page (index.php in our case)
16+
header("Location:index.php");
17+
}
18+
?>
19+

edit.php

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
// including the database connection file
3+
include_once("classes/Crud.php");
4+
5+
$crud = new Crud();
6+
7+
//getting id from url
8+
$id = $_GET['id'];
9+
10+
//selecting data associated with this particular id
11+
$result = $crud->getData("SELECT * FROM users WHERE id=$id");
12+
13+
foreach ($result as $res) {
14+
$name = $res['name'];
15+
$age = $res['age'];
16+
$email = $res['email'];
17+
}
18+
?>
19+
<html>
20+
<head>
21+
<title>Edit Data</title>
22+
</head>
23+
24+
<body>
25+
<a href="index.php">Home</a>
26+
<br/><br/>
27+
28+
<form name="form1" method="post" action="editaction.php">
29+
<table border="0">
30+
<tr>
31+
<td>Name</td>
32+
<td><input type="text" name="name" value=<?php echo $name;?>></td>
33+
</tr>
34+
<tr>
35+
<td>Age</td>
36+
<td><input type="text" name="age" value=<?php echo $age;?>></td>
37+
</tr>
38+
<tr>
39+
<td>Email</td>
40+
<td><input type="text" name="email" value=<?php echo $email;?>></td>
41+
</tr>
42+
<tr>
43+
<td><input type="hidden" name="id" value=<?php echo $_GET['id'];?>></td>
44+
<td><input type="submit" name="update" value="Update"></td>
45+
</tr>
46+
</table>
47+
</form>
48+
</body>
49+
</html>

editaction.php

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
// including the database connection file
3+
include_once("classes/Crud.php");
4+
include_once("classes/Validation.php");
5+
6+
$crud = new Crud();
7+
$validation = new Validation();
8+
9+
if(isset($_POST['update']))
10+
{
11+
$id = $_POST['id'];
12+
13+
$name=$_POST['name'];
14+
$age=$_POST['age'];
15+
$email=$_POST['email'];
16+
17+
$msg = $validation->check_empty($_POST, array('name', 'age', 'email'));
18+
$check_age = $validation->is_age_valid($_POST['age']);
19+
$check_email = $validation->is_email_valid($_POST['email']);
20+
21+
// checking empty fields
22+
if($msg) {
23+
echo $msg;
24+
//link to the previous page
25+
echo "<br/><a href='javascript:self.history.back();'>Go Back</a>";
26+
} elseif (!$check_age) {
27+
echo 'Please provide proper age.';
28+
} elseif (!$check_email) {
29+
echo 'Please provide proper email.';
30+
} else {
31+
//updating the table
32+
$result = $crud->execute("UPDATE users SET name='$name',age='$age',email='$email' WHERE id=$id");
33+
34+
//redirectig to the display page. In our case, it is index.php
35+
header("Location: index.php");
36+
}
37+
}
38+
?>

index.php

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
//including the database connection file
3+
include_once("classes/Crud.php");
4+
5+
$crud = new Crud();
6+
7+
//fetching data in descending order (lastest entry first)
8+
$query = "SELECT * FROM users ORDER BY id DESC";
9+
$result = $crud->getData($query);
10+
//echo '<pre>'; print_r($result); exit;
11+
?>
12+
13+
<html>
14+
<head>
15+
<title>Homepage</title>
16+
</head>
17+
18+
<body>
19+
<a href="add.html">Add New Data</a><br/><br/>
20+
21+
<table width='80%' border=0>
22+
23+
<tr bgcolor='#CCCCCC'>
24+
<td>Name</td>
25+
<td>Age</td>
26+
<td>Email</td>
27+
<td>Update</td>
28+
</tr>
29+
<?php
30+
foreach ($result as $key => $res) {
31+
//while($res = mysqli_fetch_array($result)) {
32+
echo "<tr>";
33+
echo "<td>".$res['name']."</td>";
34+
echo "<td>".$res['age']."</td>";
35+
echo "<td>".$res['email']."</td>";
36+
echo "<td><a href=\"edit.php?id=$res[id]\">Edit</a> | <a href=\"delete.php?id=$res[id]\" onClick=\"return confirm('Are you sure you want to delete?')\">Delete</a></td>";
37+
}
38+
?>
39+
</table>
40+
</body>
41+
</html>

0 commit comments

Comments
 (0)