根据表单包含的输入数量自动执行VALUE [英] Automate VALUEs based on how many inputs the form contains

查看:103
本文介绍了根据表单包含的输入数量自动执行VALUE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建一个自动查询,根据名称,成分和价格等多少个输入添加另一个VALUES行?

表单看起来像这样:



>
当你按下新行时,会出现另一个'New item'框,我希望SQL记录有多少行。每个'New Item'=查询的新值。

SQL:

  if($ _ SERVER ['REQUEST_METHOD'] =='POST'){

$ status_input = $ stmt = $ dbh - >准备(
INSERT INTO menues(
restaurant_id,
title,
副标题,
名称,
成分,
价格,
类别,
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,标题,副标题,类别和上传日期应该全部相同


$ b

jQuery和HTML:

  $('。newmenu')。hide(); 
$('。zero h3 a')。click(function(){
$('。newmenu')。slideToggle();
});
$('。newmenu> ul li p.add')。click(function(){$ b $ (< li class ='newrow'>< h6>新项目< / h6>< div>< p>名称:< / p>< input type ='text'name ='name'placeholder ='name'/>< / div>< div>< p>成分:< / p>< input type =' < / 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>新项目< / h6>
< div>
< p>名称:< / p>
< input type ='text'name ='name'placeholder ='name'/>
< / div>
< div>
< p>成分:< / p>
< input type ='text'name ='ingredients'placeholder ='ingredients'/>
< / div>
< div>
< p>价格:< / p>
< input type ='text'name ='price'placeholder ='price'/>
< div>
< / li>
< li class =edit>
< input type =submitvalue =提交>
< p class =add>新行< / p>
< / li>
< / ul>
< / form>


解决方案

您可以试试这个:



更改所有输入字段名称的末尾都有[](在JQuery代码和HTML中)。



现在,

然后你像这样处理它们:

  $ name = $ _POST [food_name]; 
$成分= $ _POST [food_ingredient];
$ price = $ _POST [food_price];
$ length = count($ name);

//开始查询
$ query =INSERT INTO menues(
restaurant_id,
title,
subtitle,
name,
成分,
价格,
类别,
upload_date)
价值;

//遍历所有输入行
for($ key = 0; $ key< $ length; $ key ++){
//创建每个插入
$ query。=(:id,:title,:subtitle,:name _。$ key。,: ingredient_。$ key。,:price _。$ key。,: cat,NOW()) ;
//检查它是否是最后一行
$ query。=(($ key +1 == $ length)?:,);
}

//现在查询已完成,下一步是向占位符添加值
$ insert = $ pdo-> prepare($ query);

//创建一个带有匹配占位符的数组
$ param = array(); ($ key = 0; $ key< $ length; $ key ++){
$ param [:name _。$ key] = $ name [$ key];
;
$ param [:ingredient _。$ key] = $ ingredients [$ key];
$ param [:price _。$ key] = $ price [$ key];
}

//如果需要添加更多参数
// $ param [:id] = 1;

//使用上面创建的参数执行
$ insert-> execute($ param);


How do I make an automated query that adds another VALUES row based on how many inputs with the names: name, ingredients and price there are?

The form looks like this:

When you press new row there will be another 'New item' box showing up and I want the SQL to record how many rows there are. Each 'New Item' = new value for the query.

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 and upload_date should all be the same for each row.

jQuery and 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>

解决方案

You could try this:

Change all input fields names to have [] at the end (In Jquery Code and HTML).

Now you're getting arrays returned.

Then you handle them like this:

$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);

这篇关于根据表单包含的输入数量自动执行VALUE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆