下面是一个简单的PHP代码示例,用于实现库存管理系统中的商品入库功能:
<?php
// 连接数据库
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "inventory";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("连接数据库失败: " . $conn->connect_error);
}
// 处理表单提交
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$product_name = $_POST["product_name"];
$quantity = $_POST["quantity"];
// 插入数据到数据库
$sql = "INSERT INTO products (product_name, quantity) VALUES ('$product_name', '$quantity')";
if ($conn->query($sql) === true) {
echo "商品入库成功";
} else {
echo "商品入库失败: " . $conn->error;
}
}
// 关闭数据库连接
$conn->close();
?>
<!DOCTYPE html>
<html>
<head>
<title>商品入库</title>
</head>
<body>
<h2>商品入库</h2>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
<label for="product_name">商品名称:</label>
<input type="text" name="product_name" required><br><br>
<label for="quantity">数量:</label>
<input type="number" name="quantity" required><br><br>
<input type="submit" value="提交">
</form>
</body>
</html>
上述代码中,首先通过mysqli
类连接到数据库。然后,通过$_POST
获取表单提交的商品名称和数量。接下来,将商品名称和数量插入到数据库中的products
表中。最后,关闭数据库连接。
在HTML部分,创建一个表单,用户可以输入商品名称和数量,并通过提交按钮将数据发送到服务器。
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站