基于PHP+MySQL实现仓库管理系统的代码
时间: 2024-04-05 08:33:11 浏览: 132
仓库管理系统的代码实现需要考虑到多个模块的交互和数据的处理,下面是一个基于PHP+MySQL实现仓库管理系统的代码示例,供您参考:
1.数据库连接
首先需要连接到数据库,可使用mysqli_connect()函数实现,示例代码如下:
```
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// 创建连接
$conn = mysqli_connect($servername, $username, $password, $dbname);
// 检测连接
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
mysqli_close($conn);
?>
```
2.添加商品
添加商品时需要向商品表中插入数据,可使用mysqli_query()函数实现,示例代码如下:
```
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// 创建连接
$conn = mysqli_connect($servername, $username, $password, $dbname);
// 检测连接
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// 插入商品
$sql = "INSERT INTO goods (name, price, quantity)
VALUES ('Apple', 2.5, 100)";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
```
3.查询商品
查询商品时需要从商品表中获取数据,并展示给用户,可使用mysqli_query()函数实现,示例代码如下:
```
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// 创建连接
$conn = mysqli_connect($servername, $username, $password, $dbname);
// 检测连接
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// 查询商品
$sql = "SELECT id, name, price, quantity FROM goods";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// 输出数据
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. " - Name: " . $row["name"]. " - Price: " . $row["price"]. " - Quantity: " . $row["quantity"]. "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
```
4.出库操作
出库操作需要从库存表中获取商品数量,检查是否足够,如果足够则更新库存数量,并添加出库记录到出库表中,可使用mysqli_query()函数实现,示例代码如下:
```
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// 创建连接
$conn = mysqli_connect($servername, $username, $password, $dbname);
// 检测连接
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// 获取商品数量
$sql = "SELECT quantity FROM inventory WHERE goods_id=1";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// 更新库存数量
$row = mysqli_fetch_assoc($result);
$quantity = $row["quantity"];
if($quantity >= 10){
$sql = "UPDATE inventory SET quantity=".($quantity-10)." WHERE goods_id=1";
mysqli_query($conn, $sql);
// 添加出库记录
$sql = "INSERT INTO outstock (goods_id, quantity)
VALUES (1, 10)";
mysqli_query($conn, $sql);
echo "Outstock success";
}else{
echo "Not enough goods";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
```
5.销售记录
销售记录需要从销售表中获取数据,并展示给用户,可使用mysqli_query()函数实现,示例代码如下:
```
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// 创建连接
$conn = mysqli_connect($servername, $username, $password, $dbname);
// 检测连接
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// 查询销售记录
$sql = "SELECT id, goods_id, quantity, date FROM sales";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// 输出数据
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. " - Goods_id: " . $row["goods_id"]. " - Quantity: " . $row["quantity"]. " - Date: " . $row["date"]. "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
```
上述示例代码实现了添加商品、查询商品、出库操作和销售记录等功能,您可以根据实际需求进行相应的修改和优化。
阅读全文