如何进行自动查询,根据名称输入的数量添加另一个 VALUES 行:名称、成分和价格?
表格如下所示:
当您按下新行时,会出现另一个“新项目”框,我希望 SQL 记录有多少行。每个“新项目”= 查询的新值。
SQL:
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$status_input = $stmt = $dbh ->prepare("
INSERT INTO menues(
restaurant_id,
title,
subtitle,
name,
ingredients,
price,
category,
upload_date)
VALUES
(:restaurant_id,:title, :subtitle, :name, :ingredients, :price, :category, NOW())
(:restaurant_id,:title, :subtitle, :name, :ingredients, :price, :category, NOW())
(:restaurant_id,:title, :subtitle, :name, :ingredients, :price, :category, NOW())
");
$stmt->bindParam(":restaurant_id", $userdata[0]['user_id']);
$stmt->bindParam(":title", $_POST['title']);
$stmt->bindParam(":subtitle", $_POST['subtitle']);
$stmt->bindParam(":name", $_POST['name']);
$stmt->bindParam(":ingredients", $_POST['ingredients']);
$stmt->bindParam(":price", $_POST['price']);
$stmt->bindParam(":category", $userdata[0]['category']);
$stmt->execute();
}
Restaurant_id、title、subtitle、category 和 upload_date 对于每一行都应该相同。
jQuery 和 HTML:
$(document).ready( function() {
$('.newmenu').hide();
$('.zero h3 a').click(function() {
$('.newmenu').slideToggle();
});
$('.newmenu > ul li p.add').click(function(){
$('.newmenu > ul li.edit').before("<li class='newrow'><h6>New item</h6><div><p>Name:</p><input type='text' name='name' placeholder='name' /></div><div><p>Ingredients:</p><input type='text' name='ingredients' placeholder='ingredients' /></div><div><p>Price:</p><input type='text' name='price' placeholder='price' /></li>");
});
});
<form method='post' action='newmenu.php' class='newmenu'>
<table>
<tr><td>Namn:</td><td><input type='text' name='title' placeholder='namn' /></td></tr>
<tr><td>Undertext:</td><td><input type='text' name='subtitle' placeholder='namn' /></td></tr>
</table>
<ul>
<li class='newrow'>
<h6>New item</h6>
<div>
<p>Name:</p>
<input type='text' name='name' placeholder='name' />
</div>
<div>
<p>Ingredients:</p>
<input type='text' name='ingredients' placeholder='ingredients' />
</div>
<div>
<p>Price:</p>
<input type='text' name='price' placeholder='price' />
<div>
</li>
<li class="edit">
<input type="submit" value="Submit">
<p class="add">New row</p>
</li>
</ul>
</form>