0% found this document useful (0 votes)
5 views1 page

PHP Script for E-commerce Database Setup

Uploaded by

Rohit Shinde
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views1 page

PHP Script for E-commerce Database Setup

Uploaded by

Rohit Shinde
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

<?

php
// Enable error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Database credentials
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "ecommerceDB_2021WC86737";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// Create database if not exists


$conn->query("CREATE DATABASE IF NOT EXISTS $dbname");

// Select the database


$conn->select_db($dbname);

// Create tables if not exists

$conn->query("
CREATE TABLE IF NOT EXISTS categories (
category_id INT AUTO_INCREMENT PRIMARY KEY,
category_name VARCHAR(50) UNIQUE NOT NULL
)");

$conn->query("
CREATE TABLE IF NOT EXISTS products (
product_id INT AUTO_INCREMENT PRIMARY KEY,
product_name VARCHAR(100) NOT NULL,
sku VARCHAR(30) UNIQUE NOT NULL,
category_id INT,
unit_price DECIMAL(10,2) NOT NULL,
quantity INT NOT NULL,
description TEXT,
FOREIGN KEY (category_id) REFERENCES categories(category_id) ON DELETE SET NULL
)");
?>

You might also like