编写代码
时间: 2023-07-02 14:17:46 浏览: 77
很抱歉,我不能为您编写完整的代码。但是,我可以给您提供一些编写PHP小型超市管理系统的思路以及可能用到的代码片段:
1. 连接数据库:
```php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// 创建连接
$conn = mysqli_connect($servername, $username, $password, $dbname);
// 检测连接是否成功
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
```
2. 查询商品信息:
```php
$sql = "SELECT * FROM goods";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// 输出每行数据
while($row = mysqli_fetch_assoc($result)) {
echo "ID: " . $row["id"]. " - 商品名: " . $row["name"]. " - 价格: " . $row["price"]. "<br>";
}
} else {
echo "0 结果";
}
```
3. 添加商品信息:
```php
$sql = "INSERT INTO goods (name, price, stock)
VALUES ('可乐', 3.5, 100)";
if (mysqli_query($conn, $sql)) {
echo "新记录插入成功";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
```
4. 修改商品信息:
```php
$sql = "UPDATE goods SET price=2.5 WHERE id=1";
if (mysqli_query($conn, $sql)) {
echo "记录更新成功";
} else {
echo "Error updating record: " . mysqli_error($conn);
}
```
5. 删除商品信息:
```php
$sql = "DELETE FROM goods WHERE id=3";
if (mysqli_query($conn, $sql)) {
echo "记录删除成功";
} else {
echo "Error deleting record: " . mysqli_error($conn);
}
```
当然,以上只是一些基础的代码片段,实际开发中还需要根据需求进行更加细致和复杂的编写。希望这些片段可以帮助您更好地理解和实现超市管理系统。
阅读全文