帮我用php加mysql写一个游戏点卡管理系统,页面上包括在页面上对数据进行增删改查,还有跳转页面新增点卡
时间: 2024-03-19 12:43:11 浏览: 70
好的,我会尽力帮您完成这个任务。首先,您需要准备好一个MySQL数据库和一个web服务器,并在web服务器上安装PHP环境。
接下来,您需要创建一个名为 "cards" 的数据库,并在其中创建一个名为 "cards_info" 的表,该表包含以下字段:
- id:点卡ID,自增长整数类型
- card_name:点卡名称,VARCHAR类型
- card_price:点卡价格,DECIMAL类型
- card_stock:点卡库存,INT类型
下面是代码实现:
1.连接数据库
```
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "cards";
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检查连接
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
?>
```
2.查询数据
```
<?php
$sql = "SELECT id, card_name, card_price, card_stock FROM cards_info";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// 输出数据
while($row = $result->fetch_assoc()) {
echo "ID: " . $row["id"]. " - 点卡名称: " . $row["card_name"]. " - 价格: " . $row["card_price"]. " - 库存: " . $row["card_stock"]. "<br>";
}
} else {
echo "0 结果";
}
$conn->close();
?>
```
3.插入数据
```
<?php
$sql = "INSERT INTO cards_info (card_name, card_price, card_stock)
VALUES ('点卡A', '50', '100')";
if ($conn->query($sql) === TRUE) {
echo "新记录插入成功";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
```
4.更新数据
```
<?php
$sql = "UPDATE cards_info SET card_price='60' WHERE card_name='点卡A'";
if ($conn->query($sql) === TRUE) {
echo "记录更新成功";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
```
5.删除数据
```
<?php
$sql = "DELETE FROM cards_info WHERE card_name='点卡A'";
if ($conn->query($sql) === TRUE) {
echo "记录删除成功";
} else {
echo "Error deleting record: " . $conn->error;
}
$conn->close();
?>
```
6.跳转页面新增点卡
```
<!DOCTYPE html>
<html>
<body>
<form action="add_card.php" method="post">
点卡名称: <input type="text" name="card_name"><br>
价格: <input type="text" name="card_price"><br>
库存: <input type="text" name="card_stock"><br>
<input type="submit" value="新增">
</form>
</body>
</html>
```
```
<?php
$card_name = $_POST['card_name'];
$card_price = $_POST['card_price'];
$card_stock = $_POST['card_stock'];
$sql = "INSERT INTO cards_info (card_name, card_price, card_stock) VALUES ('$card_name', '$card_price', '$card_stock')";
if ($conn->query($sql) === TRUE) {
echo "新记录插入成功";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
```
希望这些代码能够满足您的需求。请注意,这些示例代码仅供参考,并且需要根据您的具体情况进行修改和调整。
阅读全文