gpt4 book ai didi

php - 根据表单包含的输入数量自动执行 VALUE

转载 作者:行者123 更新时间:2023-11-29 00:30:29 25 4
gpt4 key购买 nike

我如何进行自动查询,根据有多少输入名称添加另一个 VALUES 行:名称、成分和价格?

表单如下所示:

the form当您按下新行时,将出现另一个“新项目”框,我希望 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>

最佳答案

你可以试试这个:

将所有输入字段名称更改为末尾有 [](在 Jquery 代码和 HTML 中)。

现在您将返回数组。

然后你像这样处理它们:

$name = $_POST["food_name"];
$ingredient = $_POST["food_ingredient"];
$price = $_POST["food_price"];
$length = count($name);

// Starting the query
$query = "INSERT INTO menues(
restaurant_id,
title,
subtitle,
name,
ingredients,
price,
category,
upload_date)
VALUES";

// Looping through all the input rows
for($key=0;$key<$length;$key++){
// Create each insert
$query .= "(:id,:title,:subtitle,:name_".$key.",:ingredient_".$key.",:price_".$key.",:cat,NOW())";
// Check if its the last row
$query .= (($key +1 == $length)?"":",");
}

// Now the query is done, next step is adding values to placeholders
$insert = $pdo->prepare($query);

// Create an array with matching placeholders
$param = array();
for($key=0;$key<$length;$key++){
$param[":name_".$key] = $name[$key];
$param[":ingredient_".$key] = $ingredient[$key];
$param[":price_".$key] = $price[$key];
}

// Add more parameters if wanted
// $param[":id"] = 1;

// Execute with parameters created above
$insert->execute($param);

关于php - 根据表单包含的输入数量自动执行 VALUE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16794854/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com