Skip to content

Commit

Permalink
CRUD operation done on products.
Browse files Browse the repository at this point in the history
  • Loading branch information
Fahad-Md-Kamal committed Feb 26, 2019
1 parent 211c78c commit ddb245c
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 18 deletions.
Binary file removed img/1551108825.jpg
Binary file not shown.
21 changes: 21 additions & 0 deletions php/deleteProduct.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

<?php
session_start();
if (isset($_GET['id']) ) {

$pid = $_GET['id'];

include_once "DB/DB_Connection.php";
$conn = DataBaseConnection();

$sql ="DELETE FROM `products` WHERE `Id` = $pid";

if($conn->query($sql))
{
$_SESSION['msg'] = "Product deleted from the system";
}else {
$_SESSION['msg'] = "Failed to delete products: ".$conn->error;
}
header("location:../productRegistration.php");
}
?>
4 changes: 2 additions & 2 deletions php/loginChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
$sql = "SELECT * FROM users WHERE email ='$email' AND pass = '$pass' ";
if($result = $conn->query($sql)){
foreach($result AS $row){
$_SESSION['userName'] = $row['name'];
$_SESSION['userName']= $userName = $row['name'];
}
$_SESSION['loggedIn'] = true;
$_SESSION['msg'] = "You are successfully Logged In";
$_SESSION['msg'] = "Hey<br> $userName <br>Welcome Back ";
}else{
$_SESSION['msg'] = "Invalid Username or Password";
}
Expand Down
19 changes: 12 additions & 7 deletions php/productRegistrationCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<?php
session_start();
if ($_POST) {

$name = $_POST['name'];
$price = $_POST['price'];
$details = $_POST['details'];
Expand Down Expand Up @@ -56,13 +57,17 @@
if ($result->num_rows > 0) {
$_SESSION['msg'] = "This product already registared";
}else{
$sql = "INSERT INTO products(name,price,details,Image)
VALUES ('$name',$price,'$details','$fileNewName')";
if($conn->query($sql)){
$_SESSION['msg'] = "Product is registared into the system";
}else{
$_SESSION['msg'] = "Product registration failed ".$conn->error;
}


$sql = "INSERT INTO products(name,price,details,Image)
VALUES ('$name',$price,'$details','$fileNewName')";
if($conn->query($sql)){
$_SESSION['msg'] = "Product is registared into the system";
}else{
$_SESSION['msg'] = "Product registration failed ".$conn->error;
}


}
}
}
Expand Down
49 changes: 49 additions & 0 deletions php/productUpdateCode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@


<?php

session_start();

if($_POST){

$pid = $_POST['pid'];
$name = $_POST['name'];
$price = $_POST['price'];
$details = $_POST['details'];


// echo($pid. $name. $price .$details);
// exit;


if (empty($name)) {
$_SESSION['msg'] = "Please enter a Unique product name";
}else if(empty($price) || !is_numeric($price)){
$_SESSION['msg'] = "Please enter numeric price of the product";
}else if(empty($details)){
$_SESSION['msg'] = "Please enter product details";
}else if( strlen($details) > 300){
$_SESSION['msg'] = "Please enter product details in 300 words";
}else{
include_once "DB/DB_Connection.php";
$conn = DataBaseConnection();

$sql = "UPDATE `products`
SET
`name` = '$name',
`price` = '$price',
`details` = '$details'
WHERE `Id` = '$pid'";

if($conn->query($sql)){
$_SESSION['msg'] = "Product updated successfully";
}else{
$_SESSION['msg'] = "Product to update the product:".$conn->error;
}
}
header("location:../productRegistration.php");
}



?>
47 changes: 38 additions & 9 deletions productRegistration.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,35 @@
<!-- head -->
<?php include_once"templating/head.php" ?>

<?php if (!isset($_SESSION['loggedIn'])){
<?php
if (!isset($_SESSION['loggedIn'])){
$_SESSION['msg'] = "Please login first";
header("location:login.php");
exit;
}

$pid = '';
$p_name = $p_price = $p_desc = '';
$script = 'Registration';

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

$pid = $_GET['id'];

include_once "php/DB/DB_Connection.php";
$conn = DataBaseConnection();

$sql ="SELECT * FROM products WHERE Id = '$pid'";
$product = $conn->query($sql);

foreach ($product AS $prd) {
$p_name = $prd['name'];
$p_price = $prd['price'];
$p_desc = $prd['details'];
$p_img = $prd['Image'];
}
$script = 'Update';
}
?>

<!-- Navigation -->
Expand All @@ -22,21 +47,22 @@

<div class="col-lg-6 bg-dark text-light mt-5 pb-5 pt-5">

<form action="php/productRegistrationCode.php" method="post" enctype="multipart/form-data">
<form action="php/product<?=$script?>Code.php" method="post" enctype="multipart/form-data">
<legend class="text-center text-uppercase">Product Registration</legend>
<div class="form-group">
<input type="hidden" value="<?=$pid?>" name="pid">
<label for="name">Product Name :</label>
<input type="text" name="name" id="name" class="form-control" placeholder="Example: John Doe" value=''>
<input type="text" name="name" id="name" value="<?=$p_name?>" class="form-control" placeholder="Example: John Doe">
</div>

<div class="form-group">
<label for="price">Product Price :</label>
<input type="price" name="price" id="price" class="form-control" placeholder="Example: [email protected]">
<input type="price" name="price" id="price" value="<?=$p_price?>" class="form-control" placeholder="Example: [email protected]">
</div>

<div class="form-group">
<label for="details">Product Details :</label>
<textarea name="details" id="details" class="form-control" placeholder="Product Details ( In 300 words )"></textarea>
<textarea name="details" id="details" class="form-control" placeholder="Product Details ( In 300 words )"><?=$p_desc?></textarea>
</div>
<div class="form-group">
<label for="details">Product image :</label><br>
Expand All @@ -45,10 +71,13 @@
<div class="form-group">
<input type="submit" class="form-control btn btn-success mt-4" value="SUBMIT">
</div>
<p class="h4 text-center"><?php if (isset($_SESSION['msg'])) {
<p class="h4 text-center">
<?php
if (isset($_SESSION['msg'])) {
echo($_SESSION['msg']);
unset($_SESSION['msg']);
} ?></p>
} ?>
</p>
</form>

</div>
Expand Down Expand Up @@ -80,8 +109,8 @@
<td><?=$row['details']?></td>
<!-- <td>< ?=$row['image']?></td> -->
<td class="d-flex justify-content-between">
<a class="btn btn-primary text-light" href="#"> EDIT </a>
<a class="btn btn-danger text-light" href="#"> DELETE </a>
<a class="btn btn-primary text-light" href="productRegistration.php?id=<?=$row['Id']?>"> EDIT </a>
<a class="btn btn-danger text-light" href="php/deleteProduct.php?id=<?=$row['Id']?>"> DELETE </a>
</td>
</tr>
<?php } ?>
Expand Down

0 comments on commit ddb245c

Please sign in to comment.