PHP打造在线二手市场平台

admin 2025-01-13 699 0

在这个数字化时代,二手市场的需求日益增长。利用PHP,我们可以轻松搭建一个功能齐全的在线二手市场平台,为用户提供便捷的交易体验。本文将带你一步步实现这一目标。

PHP打造在线二手市场平台

项目准备

首先,确保你已经安装了PHP环境和MySQL数据库。你可以使用XAMPP、WAMP等集成环境来快速搭建开发环境。

1. 创建数据库

在MySQL中创建一个名为second_market的数据库,并创建以下表:

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

CREATE TABLE products (
    id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT NOT NULL,
    name VARCHAR(100) NOT NULL,
    description TEXT,
    price DECIMAL(10, 2) NOT NULL,
    image VARCHAR(255),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id)
);

2. 项目结构

创建以下文件夹和文件:

/second_market
    /includes
        config.php
        functions.php
    /templates
        header.php
        footer.php
    index.php
    login.php
    register.php
    add_product.php
    product_details.php

核心功能实现

1. 配置数据库连接

includes/config.php中配置数据库连接:

<?php
$host = 'localhost';
$dbname = 'second_market';
$username = 'root';
$password = '';

try {
    $pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    die("Connection failed: " . $e->getMessage());
}
?>

2. 用户注册与登录

register.phplogin.php中实现用户注册和登录功能。以下是注册功能的示例:

<?php
include 'includes/config.php';
include 'includes/functions.php';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $username = $_POST['username'];
    $password = password_hash($_POST['password'], PASSWORD_DEFAULT);
    $email = $_POST['email'];

    $sql = "INSERT INTO users (username, password, email) VALUES (?, ?, ?)";
    $stmt = $pdo->prepare($sql);
    $stmt->execute([$username, $password, $email]);

    header('Location: login.php');
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Register</title>
</head>
<body>
    <form method="post">
        <label>Username:</label>
        <input type="text" name="username" required><br>
        <label>Password:</label>
        <input type="password" name="password" required><br>
        <label>Email:</label>
        <input type="email" name="email" required><br>
        <button type="submit">Register</button>
    </form>
</body>
</html>

3. 发布商品

add_product.php中实现发布商品功能:


<?php
include 'includes/config.php';
include 'includes/functions.php';

if (!isset($_SESSION['user_id'])) {
    header('Location: login.php');
    exit;
}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $user_id = $_SESSION['user_id'];
    $name = $_POST['name'];
    $description = $_POST['description'];
    $price = $_POST['price'];
    $image = $_FILES['image']['name'];
    $image_path = "uploads/" . $image;

    move_uploaded_file($_FILES['image']['tmp_name'], $image_path);

    $sql = "INSERT INTO products (user_id, name, description, price, image) VALUES (?, ?, ?, ?, ?)";
    $stmt = $pdo->prepare($sql);
    $stmt->execute([$user_id, $name, $description, $price, $image_path]);

    header('Location: index.php');
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Add Product</title>
</head>
<body>
    <form method="post" enctype="multipart/form-data">
        <label>Name:</label>
        <input type="text" name="name" required><br>
        <label>Description:</label>
        <textarea name="description"></textarea><br>
        <label>Price:</label>
        <input type="number" name="price" required><br>
        <label>Image:</label>
        <input type="file" name="image" required><br>
        <button type="submit">Add Product</button

评论(0)