Skip to content

Commit

Permalink
Adding Product to the database except product image
Browse files Browse the repository at this point in the history
  • Loading branch information
Big Bug committed Feb 16, 2019
1 parent ac14c18 commit 8867f6b
Show file tree
Hide file tree
Showing 8 changed files with 251 additions and 92 deletions.
15 changes: 15 additions & 0 deletions css/shop-homepage.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,18 @@
body {
padding-top: 56px;
}
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}

td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}

tr:nth-child(even) {
background-color: #dddddd;
}
3 changes: 2 additions & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@


<!-- product list -->
<div class="row">
<?php include_once"templating/product_list.php" ?>

</div>
<!-- /.row -->

</div>
Expand Down
19 changes: 16 additions & 3 deletions php/DB/table_Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,21 @@ function TableCreate(){
$conn->query($sql);

// Creating product table

}


// function productTbl(){
// $conn = DataBaseConnection();

// $sql = "CREATE TABLE products(
// id INT(10) PRIMARY KEY AUTO_INCREMENT,
// name VARCHAR(50),
// price INT(5),
// image VARCHAR(20),
// details VARCHAR(300)
// )";

// if ($conn->error) {
// die("Failed to Create table products:" . $conn->error);
// }
// $conn->query($sql);
// }
?>
99 changes: 99 additions & 0 deletions php/productRegistrationCode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@


<?php
session_start();
if ($_POST) {
$name = $_POST['name'];
$price = $_POST['price'];
$details = $_POST['details'];

print_r($_FILES['image']);
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{

$_SESSION['msg']="";
$target_dir = "img/";
$target_file = $target_dir . basename($_FILES["image"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
// if(isset($_POST["submit"])) {
// $check = getimagesize($_FILES["image"]["tmp_name"]);
// if($check !== false) {
// $_SESSION['msg'] .= "File is an image - " . $check["mime"] . ".";
// $uploadOk = 1;
// } else {
// $_SESSION['msg'] .= "File is not an image.";
// $uploadOk = 0;
// }
// }
// Check if file already exists
if (file_exists($target_file)) {
$_SESSION['msg'] .= "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["image"]["size"] > 3000000) {
$_SESSION['msg'] .= "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg") {
$_SESSION['msg'] .= "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
$_SESSION['msg'] .= "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)) {
$_SESSION['msg'] .= "The file ". basename( $_FILES["image"]["name"]). " has been uploaded.";

include_once "DB/DB_Connection.php";
$conn = DataBaseConnection();
$sql = "CREATE TABLE products(
Id INT(5) PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50),
price INT(10),
details VARCHAR(300),
Image VARCHAR(30)
)";
$conn->query($sql);

$sql = "SELECT * FROM products WHERE name = '$name'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$_SESSION['msg'] .= "This product already registared";
}else{
$sql = "INSERT INTO products(name,price,details,Image) VALUES ('$name',$price,'$details','$target_file')";
if($conn->query($sql)){
$_SESSION['msg'] .= "Product is registared into the system";
}else{
$_SESSION['msg'] .= "Product registration failed ".$conn->error;
}
}
} else {
$_SESSION['msg'] .= "Sorry, there was an error uploading your file.";
}
}


}

header("location:../productRegistration.php");
}




?>
96 changes: 96 additions & 0 deletions productRegistration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@




<!-- head -->
<?php include_once"templating/head.php" ?>

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

<!-- Navigation -->
<?php include_once"templating/main_nav.php" ?>


<!-- Page Content -->
<div class="container">

<div class="row justify-content-center">

<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">
<legend class="text-center text-uppercase">Product Registration</legend>
<div class="form-group">
<label for="name">Product Name :</label>
<input type="text" name="name" id="name" class="form-control" placeholder="Example: John Doe" value=''>
</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]">
</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>
</div>
<div class="form-group">
<label for="details">Product image :</label><br>
<input type="file" name="image">
</div>
<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'])) {
echo($_SESSION['msg']);
unset($_SESSION['msg']);
} ?></p>
</form>

</div>

</div>
<!-- /.row -->
<div class="col-lg-12 mt-5 mb-5">
<p class="h1 text-center">Product List</p>
<table>
<tr>
<th class="text-center">Product Name</th>
<th class="text-center">Price</th>
<th class="text-center">Details</th>
<th class="text-center">Action</th>
<!-- <th></th> -->
</tr>

<?Php
include_once "php/DB/DB_Connection.php";
$conn = DataBaseConnection();
$sql = "SELECT * FROM products";
$result = $conn->query($sql);
foreach($result AS $row){
?>

<tr>
<td><?=$row['name']?></td>
<td>$ <?=$row['price']?></td>
<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>
</td>
</tr>
<?php } ?>

</table>
</div>
</div>
<!-- /.container -->

<!-- Footer -->
<?php include_once"templating/footer_area.php" ?>

4 changes: 2 additions & 2 deletions signup.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<div class="row justify-content-center">

<div class="col-lg-6 bg-dark text-light pb-5 pt-5">
<!-- < ?php if(isset($_GET['name'])){ ?> -->

<form action="php/userReg.php" method="post">
<legend class="text-center text-uppercase">User Registration</legend>
<div class="form-group">
Expand Down Expand Up @@ -40,7 +40,7 @@
} ?></p>
</form>

<!-- < ?php }?> -->


</div>

Expand Down
10 changes: 7 additions & 3 deletions templating/main_nav.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@
</li>

<?php if (isset($_SESSION['loggedIn'])) {?>

<li class="nav-item text-info">
<?=$_SESSION['userName'] ?>
<li class="nav-item">
<a class="nav-link" href="productRegistration.php">Product Reg</a>
</li>

<li class="nav-item">
<a class="nav-link text-info" href="#"><?=$_SESSION['userName'] ?></a>
</li>

<li class="nav-item">
<a class="nav-link" href="php/logout.php">Log Out</a>
</li>
Expand Down
Loading

0 comments on commit 8867f6b

Please sign in to comment.