帮我用PHP写一个进销存后端管理系统。前端使用Java语言来写
时间: 2023-12-29 11:04:22 浏览: 97
好的,首先你需要安装一个PHP开发环境,比如XAMPP或者WAMP。
然后你可以开始编写PHP代码来实现进销存后端管理系统。以下是一个简单的示例代码:
```php
<?php
// 连接数据库
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
$conn = mysqli_connect($servername, $username, $password, $dbname);
// 检查连接
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// 处理请求
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$action = $_POST["action"];
switch ($action) {
case "add_product":
// 添加商品
$name = $_POST["name"];
$price = $_POST["price"];
$quantity = $_POST["quantity"];
$sql = "INSERT INTO products (name, price, quantity) VALUES ('$name', '$price', '$quantity')";
if (mysqli_query($conn, $sql)) {
echo "Product added successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
break;
case "update_product":
// 更新商品信息
$id = $_POST["id"];
$name = $_POST["name"];
$price = $_POST["price"];
$quantity = $_POST["quantity"];
$sql = "UPDATE products SET name='$name', price='$price', quantity='$quantity' WHERE id='$id'";
if (mysqli_query($conn, $sql)) {
echo "Product updated successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
break;
case "delete_product":
// 删除商品
$id = $_POST["id"];
$sql = "DELETE FROM products WHERE id='$id'";
if (mysqli_query($conn, $sql)) {
echo "Product deleted successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
break;
}
}
// 查询商品列表
$sql = "SELECT * FROM products";
$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 "No products found";
}
// 关闭连接
mysqli_close($conn);
?>
```
这个示例代码实现了添加、更新和删除商品的功能,并且查询并输出了所有商品的信息。你可以根据你的具体需求来修改代码。关于前端使用Java语言来写,你可以使用Java Swing或者JavaFX来实现。
阅读全文