请帮我利用php代码写一个商品购物界面可以浏览和购买商品要求制作精美一些并且与后台数据库进行交互实现增删改查的操作
时间: 2024-03-07 18:50:29 浏览: 137
好的,以下是我为您编写的PHP代码,用于制作一个商品购物界面,可以浏览和购买商品,并且与后台数据库进行交互实现增删改查的操作。
```php
<!DOCTYPE html>
<html>
<head>
<title>商品购物界面</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container-fluid">
<nav class="navbar navbar-expand-sm bg-dark navbar-dark">
<a class="navbar-brand" href="#">商品购物界面</a>
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="#">首页</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">手机</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">电脑</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">家电</a>
</li>
</ul>
<form class="form-inline" action="/action_page.php">
<input class="form-control mr-sm-2" type="text" placeholder="搜索">
<button class="btn btn-success" type="submit">搜索</button>
</form>
</nav>
<div class="row">
<div class="col-sm-2">
<h4>分类</h4>
<ul class="list-group">
<li class="list-group-item"><a href="#">手机</a></li>
<li class="list-group-item"><a href="#">电脑</a></li>
<li class="list-group-item"><a href="#">家电</a></li>
</ul>
</div>
<div class="col-sm-10">
<h4>热销商品</h4>
<div class="row">
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检测连接
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
if (isset($_POST['add_to_cart'])) {
$product_id = $_POST['product_id'];
$product_name = $_POST['product_name'];
$product_price = $_POST['product_price'];
$sql = "INSERT INTO cart (id, name, price) VALUES ('$product_id', '$product_name', '$product_price')";
if ($conn->query($sql) === TRUE) {
echo '<div class="alert alert-success alert-dismissible">';
echo '<button type="button" class="close" data-dismiss="alert">×</button>';
echo '<strong>成功!</strong> 商品已添加到购物车.';
echo '</div>';
} else {
echo '<div class="alert alert-danger alert-dismissible">';
echo '<button type="button" class="close" data-dismiss="alert">×</button>';
echo '<strong>失败!</strong> 商品添加到购物车时出错.';
echo '</div>';
}
}
$sql = "SELECT * FROM products";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// 输出数据
while($row = $result->fetch_assoc()) {
echo '<div class="col-sm-3">';
echo '<div class="card">';
echo '<img class="card-img-top" src="' . $row["image"] . '" alt="Card image">';
echo '<div class="card-body">';
echo '<h4 class="card-title">' . $row["name"] . '</h4>';
echo '<p class="card-text">' . $row["description"] . '</p>';
echo '<h5 class="card-text">' . $row["price"] . '</h5>';
echo '<form method="post" action="">';
echo '<input type="hidden" name="product_id" value="' . $row["id"] . '">';
echo '<input type="hidden" name="product_name" value="' . $row["name"] . '">';
echo '<input type="hidden" name="product_price" value="' . $row["price"] . '">';
echo '<button type="submit" name="add_to_cart" class="btn btn-primary">加入购物车</button>';
echo '</form>';
echo '</div>';
echo '</div>';
echo '</div>';
}
} else {
echo "0 结果";
}
$conn->close();
?>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<h4>购物车</h4>
<table class="table table-hover">
<thead>
<tr>
<th>商品ID</th>
<th>商品名称</th>
<th>商品价格</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检测连接
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
if (isset($_POST['remove_from_cart'])) {
$cart_id = $_POST['cart_id'];
$sql = "DELETE FROM cart WHERE id='$cart_id'";
if ($conn->query($sql) === TRUE) {
echo '<div class="alert alert-success alert-dismissible">';
echo '<button type="button" class="close" data-dismiss="alert">×</button>';
echo '<strong>成功!</strong> 商品已从购物车中移除.';
echo '</div>';
} else {
echo '<div class="alert alert-danger alert-dismissible">';
echo '<button type="button" class="close" data-dismiss="alert">×</button>';
echo '<strong>失败!</strong> 商品从购物车中移除时出错.';
echo '</div>';
}
}
$sql = "SELECT * FROM cart";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// 输出数据
while($row = $result->fetch_assoc()) {
echo '<tr>';
echo '<td>' . $row["id"] . '</td>';
echo '<td>' . $row["name"] . '</td>';
echo '<td>' . $row["price"] . '</td>';
echo '<td>';
echo '<form method="post" action="">';
echo '<input type="hidden" name="cart_id" value="' . $row["id"] . '">';
echo '<button type="submit" name="remove_from_cart" class="btn btn-danger">移除</button>';
echo '</form>';
echo '</td>';
echo '</tr>';
}
} else {
echo '<tr>';
echo '<td colspan="4">购物车为空</td>';
echo '</tr>';
}
$conn->close();
?>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
```
请注意,您需要将以下变量替换为您自己的数据库信息:
```php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
```
此代码使用Bootstrap框架创建基本的网页布局,并从数据库中获取商品信息,然后将其用卡片形式展示出来。在每个产品卡片下面,有一个“加入购物车”按钮,当用户点击时,该商品将被添加到购物车数据库中。购物车页面显示了所有添加到购物车的商品,并允许用户从购物车中删除商品。
阅读全文