【发布时间】:2021-05-22 17:15:15
【问题描述】:
您好,我有一个小型电子商务应用程序。 我想在单击加号按钮时添加新的 td 并使用数组发布所有数据 我想将此尺寸与 json 一起存储在我的产品表中
我的餐桌详情是这样的;
CREATE TABLE `products` (
`id` int(11) NOT NULL,
`title` varchar(255) NOT NULL,
`sizes` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL CHECK (json_valid(`sizes`)),
`created_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
我该怎么做?我的代码;
<div class="container">
<div class="row justify-content-center">
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
<div class="card">
<div class="card-header">Add Product</div>
<div class="card-body">
<form action="<?=baseURL?>/add.php" method="POST">
<div class="mb-3">
<label for="title" class="form-label">Title</label>
<input type="text" class="form-control" id="title" name="title" required>
</div>
<div class="mb-3">
<table class="table">
<thead>
<tr>
<th scope="col">Size</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="text" class="form-control" id="size" placeholder="42" name="size"></td>
<td><a class="btn btn-sm btn-success" id="addSize" name="addSize">+</td>
</tr>
</tbody>
</table>
</div>
<button type="submit" class="btn btn-sm btn-primary">Add</button>
</form>
</div>
</div>
</div>
</div>
</div>
jQuery 脚本
<script>
$( document ).ready(function() {
$( "#addSize" ).click(function() {
});
});
</script>
这是我的 php 代码
$title = $_POST['title'];
$sizes = json_encode($_POST['sizes']);
$stmt = $pdo->prepare("INSERT INTO products (title,sizes) VALUES (?, ?)");
$stmt->execute(array($title,$sizes));
$stmt = null;
header('location:'.baseURL);
谢谢。
【问题讨论】: