Home  >  Article  >  Database  >  How to develop a simple e-commerce website using MySQL and Ruby

How to develop a simple e-commerce website using MySQL and Ruby

PHPz
PHPzOriginal
2023-09-22 08:51:221180browse

How to develop a simple e-commerce website using MySQL and Ruby

How to use MySQL and Ruby to develop a simple e-commerce website

The development of e-commerce websites is an important part of modern Internet applications. In this article, we will introduce how to develop a simple e-commerce website using MySQL and Ruby. We'll discuss database design, back-end logic, and front-end interactions, and provide specific code examples.

  1. Database design

First of all, we need to design a database suitable for e-commerce websites. We will use MySQL as our database management system and define the following tables:

  • User (User): stores the user's basic information, such as user name, password, email, etc.
  • Product: Stores product information, such as product name, price, inventory, etc.
  • Order: Stores order information, such as order number, user ID, product ID, quantity, etc.

The code to create these tables in MySQL is as follows:

CREATE TABLE User (
    id INT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(50) NOT NULL,
    password VARCHAR(50) NOT NULL,
    email VARCHAR(50) NOT NULL
);

CREATE TABLE Product (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(50) NOT NULL,
    price DECIMAL(10, 2) NOT NULL,
    stock INT NOT NULL
);

CREATE TABLE Order (
    id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT NOT NULL,
    product_id INT NOT NULL,
    quantity INT NOT NULL,
    FOREIGN KEY (user_id) REFERENCES User(id),
    FOREIGN KEY (product_id) REFERENCES Product(id)
);
  1. Backend logic

Next, we will use Ruby to implement Backend logic. We will use Sinatra as our web framework and ActiveRecord to interact with the MySQL database. The following is a simple backend code example:

require 'sinatra'
require 'sinatra/activerecord'

# 配置数据库连接
set :database, {adapter: 'mysql2', host: 'localhost', database: 'ecommerce', username: 'root', password: 'password'}

# 定义User模型
class User < ActiveRecord::Base
  has_many :orders
end

# 定义Product模型
class Product < ActiveRecord::Base
  has_many :orders
end

# 定义Order模型
class Order < ActiveRecord::Base
  belongs_to :user
  belongs_to :product
end

# 创建用户
post '/users' do
  user = User.create(params[:user])
  if user.valid?
    {status: 'success', message: 'User created successfully'}.to_json
  else
    {status: 'error', message: user.errors.full_messages.join(', ')}.to_json
  end
end

# 创建商品
post '/products' do
  product = Product.create(params[:product])
  if product.valid?
    {status: 'success', message: 'Product created successfully'}.to_json
  else
    {status: 'error', message: product.errors.full_messages.join(', ')}.to_json
  end
end

# 创建订单
post '/orders' do
  order = Order.create(params[:order])
  if order.valid?
    {status: 'success', message: 'Order created successfully'}.to_json
  else
    {status: 'error', message: order.errors.full_messages.join(', ')}.to_json
  end
end

In the above example, we have defined three models (User, Product, and Order) to interact with the database. We created three routes to handle user, product, and order creation requests.

  1. Front-end interaction

Finally, we will use HTML, CSS and JavaScript to implement front-end interaction. Here is a simple front-end code example:

<!DOCTYPE html>
<html>
<head>
  <title>E-commerce Website</title>
  <link rel="stylesheet" type="text/css" href="style.css">
  <script src="script.js"></script>
</head>
<body>
  <h1>Welcome to our E-commerce Website</h1>

  <form id="user-form" onsubmit="createUser(event)">
    <input type="text" name="username" placeholder="Username" required>
    <input type="password" name="password" placeholder="Password" required>
    <input type="email" name="email" placeholder="Email" required>
    <button type="submit">Create User</button>
  </form>

  <form id="product-form" onsubmit="createProduct(event)">
    <input type="text" name="name" placeholder="Product Name" required>
    <input type="number" name="price" step="0.01" placeholder="Price" required>
    <input type="number" name="stock" placeholder="Stock" required>
    <button type="submit">Create Product</button>
  </form>

  <form id="order-form" onsubmit="createOrder(event)">
    <input type="number" name="user_id" placeholder="User ID" required>
    <input type="number" name="product_id" placeholder="Product ID" required>
    <input type="number" name="quantity" placeholder="Quantity" required>
    <button type="submit">Create Order</button>
  </form>

  <script src="http://code.jquery.com/jquery-3.5.1.min.js"></script>
  <script src="script.js"></script>
</body>
</html>
/* style.css */
input {
  margin-bottom: 10px;
}

button {
  margin-top: 10px;
}
// script.js
function createUser(event) {
  event.preventDefault();

  $.ajax({
    url: '/users',
    method: 'POST',
    data: $('#user-form').serialize(),
    success: function(response) {
      console.log(response);
    }
  });
}

function createProduct(event) {
  event.preventDefault();

  $.ajax({
    url: '/products',
    method: 'POST',
    data: $('#product-form').serialize(),
    success: function(response) {
      console.log(response);
    }
  });
}

function createOrder(event) {
  event.preventDefault();

  $.ajax({
    url: '/orders',
    method: 'POST',
    data: $('#order-form').serialize(),
    success: function(response) {
      console.log(response);
    }
  });
}

In the above example, we created a simple form to create users, items, and orders. When the form is submitted, the data is sent to the backend via an Ajax request and the response is displayed in the console.

Summary:

This article introduces how to use MySQL and Ruby to develop a simple e-commerce website. We discuss database design, back-end logic, and front-end interactions, and provide concrete code examples. By studying these examples, you can start developing your own e-commerce website, extending and customizing it as needed. Hope this article can help you!

The above is the detailed content of How to develop a simple e-commerce website using MySQL and Ruby. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn