Skip to content

Commit d8fbea5

Browse files
committed
Add all files
1 parent f184f4e commit d8fbea5

13 files changed

+470
-1
lines changed

README.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
1-
# crud-php-complete
1+
Complete Create, Read, Update, Delete (CRUD) in PHP & MySQL
2+
========
3+
4+
A complete CRUD system to add, edit, delete and view using PHP and MySQL. Complete in a sense that this system has login and register feature as well. I have another repository with [Simple CRUD System](https://github.com/chapagain/crud-php-simple) which doesn't have login and register feature.
5+
6+
Blog Article: [CRUD Create, Read, Update, Delete in PHP & MySQL with Login & Register: A simple & complete tutorial](http://blog.chapagain.com.np/crud-create-read-update-delete-php-mysql-login-register/)
7+
8+
At first, users need to register and login. After successful login, users can add their product data. Users can see only those data that they have added.
9+
10+
SQL script to create database and tables is present in **database.sql** file.
11+

add.html

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<html>
2+
<head>
3+
<title>Add Data</title>
4+
</head>
5+
6+
<body>
7+
<a href="index.php">Home</a> | <a href="view.php">View Products</a> | <a href="logout.php">Logout</a>
8+
<br/><br/>
9+
10+
<form action="add.php" method="post" name="form1">
11+
<table width="25%" border="0">
12+
<tr>
13+
<td>Name</td>
14+
<td><input type="text" name="name"></td>
15+
</tr>
16+
<tr>
17+
<td>Quantity</td>
18+
<td><input type="text" name="qty"></td>
19+
</tr>
20+
<tr>
21+
<td>Price</td>
22+
<td><input type="text" name="price"></td>
23+
</tr>
24+
<tr>
25+
<td></td>
26+
<td><input type="submit" name="Submit" value="Add"></td>
27+
</tr>
28+
</table>
29+
</form>
30+
</body>
31+
</html>
32+

add.php

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php session_start(); ?>
2+
3+
<?php
4+
if(!isset($_SESSION['valid'])) {
5+
header('Location: login.php');
6+
}
7+
?>
8+
9+
<html>
10+
<head>
11+
<title>Add Data</title>
12+
</head>
13+
14+
<body>
15+
<?php
16+
//including the database connection file
17+
include_once("connection.php");
18+
19+
if(isset($_POST['Submit'])) {
20+
$name = $_POST['name'];
21+
$qty = $_POST['qty'];
22+
$price = $_POST['price'];
23+
$loginId = $_SESSION['id'];
24+
25+
// checking empty fields
26+
if(empty($name) || empty($qty) || empty($price)) {
27+
28+
if(empty($name)) {
29+
echo "<font color='red'>Name field is empty.</font><br/>";
30+
}
31+
32+
if(empty($qty)) {
33+
echo "<font color='red'>Quantity field is empty.</font><br/>";
34+
}
35+
36+
if(empty($price)) {
37+
echo "<font color='red'>Price field is empty.</font><br/>";
38+
}
39+
40+
//link to the previous page
41+
echo "<br/><a href='javascript:self.history.back();'>Go Back</a>";
42+
} else {
43+
// if all the fields are filled (not empty)
44+
45+
//insert data to database
46+
$result = mysql_query("INSERT INTO products(name, qty, price, login_id) VALUES('$name','$qty','$price', '$loginId')");
47+
48+
//display success message
49+
echo "<font color='green'>Data added successfully.";
50+
echo "<br/><a href='view.php'>View Result</a>";
51+
}
52+
}
53+
?>
54+
</body>
55+
</html>

connection.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
// mysql_connect("database-host", "username", "password")
4+
$conn = mysql_connect("localhost","root","root")
5+
or die("cannot connected");
6+
7+
// mysql_select_db("database-name", "connection-link-identifier")
8+
@mysql_select_db("test2",$conn);
9+
10+
?>

database.sql

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
create database `test2`;
2+
3+
use `test2`;
4+
5+
CREATE TABLE `login` (
6+
`id` int(9) NOT NULL auto_increment,
7+
`name` varchar(100) NOT NULL,
8+
`email` varchar(100) NOT NULL,
9+
`username` varchar(100) NOT NULL,
10+
`password` varchar(100) NOT NULL,
11+
PRIMARY KEY (`id`)
12+
) ENGINE=InnoDB;
13+
14+
CREATE TABLE `products` (
15+
`id` int(11) NOT NULL auto_increment,
16+
`name` varchar(100) NOT NULL,
17+
`qty` int(5) NOT NULL,
18+
`price` decimal(10,2) NOT NULL,
19+
`login_id` int(11) NOT NULL,
20+
PRIMARY KEY (`id`),
21+
CONSTRAINT FK_products_1
22+
FOREIGN KEY (login_id) REFERENCES login(id)
23+
ON UPDATE CASCADE ON DELETE CASCADE
24+
) ENGINE=InnoDB;

delete.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php session_start(); ?>
2+
3+
<?php
4+
if(!isset($_SESSION['valid'])) {
5+
header('Location: login.php');
6+
}
7+
?>
8+
9+
<?php
10+
//including the database connection file
11+
include("connection.php");
12+
13+
//getting id of the data from url
14+
$id = $_GET['id'];
15+
16+
//deleting the row from table
17+
$result=mysql_query("DELETE FROM products WHERE id=$id");
18+
19+
//redirecting to the display page (view.php in our case)
20+
header("Location:view.php");
21+
?>
22+

edit.php

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php session_start(); ?>
2+
3+
<?php
4+
if(!isset($_SESSION['valid'])) {
5+
header('Location: login.php');
6+
}
7+
?>
8+
9+
<?php
10+
// including the database connection file
11+
include_once("connection.php");
12+
13+
if(isset($_POST['update']))
14+
{
15+
$id = $_POST['id'];
16+
17+
$name = $_POST['name'];
18+
$qty = $_POST['qty'];
19+
$price = $_POST['price'];
20+
21+
// checking empty fields
22+
if(empty($name) || empty($qty) || empty($price)) {
23+
24+
if(empty($name)) {
25+
echo "<font color='red'>Name field is empty.</font><br/>";
26+
}
27+
28+
if(empty($qty)) {
29+
echo "<font color='red'>Quantity field is empty.</font><br/>";
30+
}
31+
32+
if(empty($price)) {
33+
echo "<font color='red'>Price field is empty.</font><br/>";
34+
}
35+
} else {
36+
//updating the table
37+
$result = mysql_query("UPDATE products SET name='$name', qty='$qty', price='$price' WHERE id=$id");
38+
39+
//redirectig to the display page. In our case, it is view.php
40+
header("Location: view.php");
41+
}
42+
}
43+
?>
44+
<?php
45+
//getting id from url
46+
$id = $_GET['id'];
47+
48+
//selecting data associated with this particular id
49+
$result = mysql_query("SELECT * FROM products WHERE id=$id");
50+
51+
while($res = mysql_fetch_array($result))
52+
{
53+
$name = $res['name'];
54+
$qty = $res['qty'];
55+
$price = $res['price'];
56+
}
57+
?>
58+
<html>
59+
<head>
60+
<title>Edit Data</title>
61+
</head>
62+
63+
<body>
64+
<a href="index.php">Home</a> | <a href="view.php">View Products</a> | <a href="logout.php">Logout</a>
65+
<br/><br/>
66+
67+
<form name="form1" method="post" action="edit.php">
68+
<table border="0">
69+
<tr>
70+
<td>Name</td>
71+
<td><input type="text" name="name" value=<?php echo $name;?>></td>
72+
</tr>
73+
<tr>
74+
<td>Quantity</td>
75+
<td><input type="text" name="qty" value=<?php echo $qty;?>></td>
76+
</tr>
77+
<tr>
78+
<td>Price</td>
79+
<td><input type="text" name="price" value=<?php echo $price;?>></td>
80+
</tr>
81+
<tr>
82+
<td><input type="hidden" name="id" value=<?php echo $_GET['id'];?>></td>
83+
<td><input type="submit" name="update" value="Update"></td>
84+
</tr>
85+
</table>
86+
</form>
87+
</body>
88+
</html>

index.php

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php session_start(); ?>
2+
<html>
3+
<head>
4+
<title>Homepage</title>
5+
<link href="style.css" rel="stylesheet" type="text/css">
6+
</head>
7+
8+
<body>
9+
<div id="header">
10+
Welcome to my page!
11+
</div>
12+
<?php
13+
if(isset($_SESSION['valid'])) {
14+
include("connection.php");
15+
$result = mysql_query("SELECT * FROM login", $conn);
16+
?>
17+
18+
Welcome <?php echo $_SESSION['name'] ?> ! <a href='logout.php'>Logout</a><br/>
19+
<br/>
20+
<a href='view.php'>View and Add Products</a>
21+
<br/><br/>
22+
<?php
23+
} else {
24+
echo "You must be logged in to view this page.<br/><br/>";
25+
echo "<a href='login.php'>Login</a> | <a href='register.php'>Register</a>";
26+
}
27+
?>
28+
<div id="footer">
29+
Created by <a href="http://blog.chapagain.com.np" title="Mukesh Chapagain">Mukesh Chapagain</a>
30+
</div>
31+
</body>
32+
</html>

login.php

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php session_start(); ?>
2+
<html>
3+
<head>
4+
<title>Login</title>
5+
</head>
6+
7+
<body>
8+
<a href="index.php">Home</a> <br />
9+
<?php
10+
include("connection.php");
11+
12+
if(isset($_POST['submit'])) {
13+
$user = mysql_real_escape_string($_POST['username']);
14+
$pass = mysql_real_escape_string($_POST['password']);
15+
16+
if($user == "" || $pass == "") {
17+
echo "Either username or password field is empty.";
18+
echo "<br/>";
19+
echo "<a href='login.php'>Go back</a>";
20+
} else {
21+
$result = mysql_query("SELECT * FROM login WHERE username='$user' AND password=md5('$pass')",$conn)
22+
or die("Could not execute the select query.");
23+
24+
$row = mysql_fetch_assoc($result);
25+
26+
if(is_array($row) && !empty($row)) {
27+
$validuser = $row['username'];
28+
$_SESSION['valid'] = $validuser;
29+
$_SESSION['name'] = $row['name'];
30+
$_SESSION['id'] = $row['id'];
31+
} else {
32+
echo "Invalid username or password.";
33+
echo "<br/>";
34+
echo "<a href='login.php'>Go back</a>";
35+
}
36+
37+
if(isset($_SESSION['valid'])) {
38+
header('Location: index.php');
39+
}
40+
}
41+
} else {
42+
?>
43+
<p><font size="+2">Login</font></p>
44+
<form name="form1" method="post" action="">
45+
<table width="75%" border="0">
46+
<tr>
47+
<td width="10%">Username</td>
48+
<td><input type="text" name="username"></td>
49+
</tr>
50+
<tr>
51+
<td>Password</td>
52+
<td><input type="password" name="password"></td>
53+
</tr>
54+
<tr>
55+
<td>&nbsp;</td>
56+
<td><input type="submit" name="submit" value="Submit"></td>
57+
</tr>
58+
</table>
59+
</form>
60+
<?php
61+
}
62+
?>
63+
</body>
64+
</html>

logout.php

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
session_start();
3+
session_destroy();
4+
header("Location:index.php");
5+
?>

0 commit comments

Comments
 (0)