Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

E-Commerce Site This site will contain three parts Part 1 The site will utilize

ID: 3856634 • Letter: E

Question

E-Commerce Site This site will contain three parts Part 1 The site will utilize a login, this can be from your previous assignments or you can create a new table for holding usernames and passwords. There will need to be a database storage for the user information. The password should use the hash() function to hash the password. It should direct the user to a page to make a new account on the login page and should not allow for login in the password or username is not correct. Part 2 There will be a page for looking at the catalogue of items. A new table will be made of all the items to be sold. Step 1 Enter the MYSQL admin and create a table called products The table will have ProductID Name Description Price Step 2 After you have created the table you will insert these items Anvil Axle Grease Atom Re-Arranger Bed Springs Bird Seed Blasting Powder Cork Dsintigration Pistol Earthquake Pills Female Roadrunner costume Giant Rubber Band Instant Girl Iron Carrot Jet Propelled Unicycle Out-Board Motor Railroad Track Rocket Sled Super Outfit Time Space Gun X-Ray Step 3 Create a catalogue page called catalogue.php and then add the text “Please select one of our items:” then an options/drop down box The PHP will call the database and then populate the box with each of the items. Part 3 Making the shopping cart. Things the page will need to do- You will need to use the Session to track the cart products and quantity You will need to add products to the cart and remove them from the cart You will need to empty the cart. You will need to show information in individual products You will need to display an image of the product when selecting to show info(Use this file) You will need to show a total cost of all the products in the cart This page can be the same page as catalogue.

Explanation / Answer

<?php
include('login.php'); // Includes Login Script

if(isset($_SESSION['login_user'])){
header("location: profile.php");
}
?>
<!DOCTYPE html>
<html>
<head>

<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="login">
<h2>Login</h2>
<form action="" method="post">
<label>UserName :</label>
<input id="name" name="username" type="text">
<label>Password :</label>
<input id="password" name="password" type="password">
<input name="submit" type="submit" value=" Login ">
<span><?php echo $error; ?></span>
</form>
</div>
</div>
</bodzy>
</html>

CREATE TABLE `products` (

`ProductID ` int(8) NOT NULL AUTO_INCREMENT,

`Name` varchar(255) NOT NULL,

`Description` varchar(255) NOT NULL,

`price` double(10,2) NOT NULL,

PRIMARY KEY (`id`)

}

<?php

$product_array = $db_handle->runQuery("SELECT * FROM products ORDER BY ProductID ASC");

if (!empty($product_array)) {

foreach($product_array as $key=>$value){

?>

<div >

<form method="post" action="index.php?action=add&code=<?php echo $product_array[$key]["ProductID"]; ?>">

<div class="product-image"><img src="&lt;?php echo $product_array[$key]["image"]; ?>"></div>

<div><strong><?php echo $product_array[$key]["name"]; ?></strong></div>

<div><?php echo "$".$product_array[$key]["price"]; ?></div>

<div><input type="text" name="quantity" value="1" size="2" /><input type="submit" value="Add to cart" class="btnAddAction" /></div>

</form>

</div>

<?php }} ?>

case "add":

if(!empty($_POST["quantity"])) {

$productByCode = $db_handle->runQuery("SELECT * FROM products WHERE ProductID='" . $_GET["ProductID"] . "'");

$itemArray = array($productByCode[0]["code"]=>array('name'=>$productByCode[0]["name"], 'code'=>$productByCode[0]["code"], 'quantity'=>$_POST["quantity"], 'price'=>$productByCode[0]["price"]));

if(!empty($_SESSION["cart_item"])) {

if(in_array($productByCode[0]["ProductID"],array_keys($_SESSION["cart_item"]))) {

foreach($_SESSION["cart_item"] as $k => $v) {

if($productByCode[0]["code"] == $k) {

if(empty($_SESSION["cart_item"][$k]["quantity"])) {

$_SESSION["cart_item"][$k]["quantity"] = 0;

}

$_SESSION["cart_item"][$k]["quantity"] += $_POST["quantity"];

}

}

} else {

$_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);

}

} else {

$_SESSION["cart_item"] = $itemArray;

}

}

break;

remove code :

case "remove":

if(!empty($_SESSION["cart_item"])) {

foreach($_SESSION["cart_item"] as $k => $v) {

if($_GET["code"] == $k) unset($_SESSION["cart_item"][$k]);

if(empty($_SESSION["cart_item"])) unset($_SESSION["cart_item"]);

}

}

break;

case "empty":

unset($_SESSION["cart_item"]);

break;

<?php
include('session.php');
?>
<!DOCTYPE html>
<html>
<body>
<div id="profile">
<b id="welcome">Welcome : <i><?php echo $login_session; ?></i></b>
<b id="logout"><a href="logout.php">Log Out</a></b>
</div>
</body>
</html>

<?php
// Establishing Connection with Server by passing server_name, user_id and password as a parameter
$connection = mysql_connect("localhost", "root", "");
// Selecting Database
$db = mysql_select_db("prod", $connection);
session_start();// Starting Session
// Storing Session
$user_check=$_SESSION['login_user'];
// SQL Query To Fetch Complete Information Of User
$ses_sql=mysql_query("select username from login where username='$user_check'", $connection);
$row = mysql_fetch_assoc($ses_sql);
$login_session =$row['username'];
if(!isset($login_session)){
mysql_close($connection); // Closing Connection
header('Location: index.php'); // Redirecting To Home Page
}
?>

<?php
session_start();
if(session_destroy()) // Destroying All Sessions
{
header("Location: index.php"); // Redirecting To Home Page
}
?>

<?php
session_start(); // Starting Session
$error=''; // Variable To Store Error Message
if (isset($_POST['submit'])) {
if (empty($_POST['username']) || empty($_POST['password'])) {
$error = "Username or Password is invalid";
}
else
{
// Define $username and $password
$username=$_POST['username'];
$password=$_POST['password'];
// Establishing Connection with Server by passing server_name, user_id and password as a parameter
$connection = mysql_connect("localhost", "root", "");
// To protect MySQL injection for Security purpose
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
// Selecting Database
$db = mysql_select_db("prod", $connection);
// SQL query to fetch information of registerd users and finds user match.
$query = mysql_query("select * from login where password='$password' AND username='$username'", $connection);
$rows = mysql_num_rows($query);
if ($rows == 1) {
$_SESSION['login_user']=$username; // Initializing Session
header("location: profile.php"); // Redirecting To Other Page
} else {
$error = "Username or Password is invalid";
}
mysql_close($connection); // Closing Connection
}
}
?>