根据表单包含的输入数量自动化VALUE

时间:2013-05-28 14:38:39

标签: php mysql forms

如何创建一个自动查询,根据名称,名称,成分和价格的输入添加另一个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>

2 个答案:

答案 0 :(得分:1)

你可以试试这个:

将所有输入字段名称更改为最后包含[](在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);

答案 1 :(得分:0)

尝试这些更改。我在代码中添加了注释。

基本上我在表单字段名称中添加了一个数字,因此您可以区分它们。所以名字是name_0,然后当你用javascript添加一行时,下一个名字将是name_1。然后在PHP中你将循环并检查是否有name_0,然后在下一个循环中你将检查是否有name_1等等。当它找不到任何行时,它会使用break;退出循环。

我没有测试过代码,但基本上它应该可行。尝试自己实现它而不是复制粘贴。阅读评论并查看我在代码中更改的内容。

祝你好运。

HTML:

<!--
Make the first fields use row number 0
-->

<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_0' placeholder='name' />
      </div>
      <div>
        <p>Ingredients:</p>
        <input type='text' name='ingredients_0' placeholder='ingredients' />
      </div>
      <div>
        <p>Price:</p>
        <input type='text' name='price_0' placeholder='price' />
      <div>
    </li>
    <li class="edit">
      <input type="submit" value="Submit">
      <p class="add">New row</p>
    </li>
  </ul>
</form>

jQuery的:

$(document).ready( function() {

  $('.newmenu').hide();
  $('.zero h3 a').click(function() {
    $('.newmenu').slideToggle();
  });

  // dynamically added rows start at row 1
  var row_number = 1;

  $('.newmenu > ul li p.add').click(function(){

    // append the fields with the row number
    $('.newmenu > ul li.edit').before("<li class='newrow'><h6>New item</h6><div><p>Name:</p><input type='text' name='name_" + row_number + "' placeholder='name' /></div><div><p>Ingredients:</p><input type='text' name='ingredients_" + row_number + "' placeholder='ingredients' /></div><div><p>Price:</p><input type='text' name='price_" + row_number + "' placeholder='price' /></li>");

    // increment row number
    row_number++;

  });

});

PHP:

if($_SERVER['REQUEST_METHOD'] == 'POST'){

    // start at row 0
    $row_number = 0;

    while(true) {

        // check if there are still fields in this row offset
        if(isset($_POST['name_' + $row_number])) {

            $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_' . $row_number]);
             $stmt->bindParam(":ingredients", $_POST['ingredients_' . $row_number]);
             $stmt->bindParam(":price", $_POST['price_' . $row_number]);
             $stmt->bindParam(":category", $userdata[0]['category']);
             $stmt->execute();

        }

        // increment row number
        $row_number++;

    }
    else {
        // if there are no rows anymore, exit the while loop
        break;
    }
}