1

如何进行自动查询,根据名称输入的数量添加另一个 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>
4

2 回答 2

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);
于 2013-05-28T16:21:08.980 回答
0

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

基本上,我在表单字段名称中添加了一个数字,以便您区分它们。所以第一个名字是name_0,那么当你用javascript添加一行时,下一个名字就是name_1。然后在 PHP 中你将创建一个循环并检查是否有 a name_0,然后在下一个循环中你将检查是否有 aname_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;
    }
}
于 2013-05-28T15:36:19.797 回答