【发布时间】:2013-05-28 14:38:39
【问题描述】:
如何进行自动查询,根据输入的名称:名称、成分和价格添加另一个 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>
【问题讨论】:
-
表单是如何发布的?
-
method="post" and action="new_menu.php" - new_menu.php = 上面的代码。如果我的答案很糟糕,我没有再问这个问题了哈哈。您可以通过单击右侧的提交来发布它。
-
当您点击“新行”时会发生什么?它是用 javascript 动态添加的吗?
-
当您点击提交时,这些新的表单元素是否也被添加到 POST 中?