简体   繁体   English

PHP和MySQL插入输入数组

[英]php and mysql insert input array

I have a problem with php. 我对php有问题。 I will try to explain it as good and short as I possibly can and I hope you will understand what I'm trying to do. 我将尽我所能来解释它的优缺点,希望您能理解我正在尝试做的事情。

I have this HTML form: http://codepen.io/anon/pen/aNYLvJ 我有这个HTML表单: http : //codepen.io/anon/pen/aNYLvJ

As you see in the example from codepen, my text boxes can multiply if you click the hyperlink. 正如您从Codepen的示例中看到的那样,如果您单击超链接,则我的文本框会成倍增加。 But the first input should not multiply. 但是第一个输入不应相乘。 In my inputs, I use square brackets. 在输入中,使用方括号。 I'm trying to figure out the php part for this. 我试图为此找出php部分。 I want to run the result from an HTML form in my database. 我想从数据库中的HTML表单运行结果。

So, Lets say I multiply textboxes 3 times. 因此,可以说我将文本框乘以3次。 The query should be something like 查询应该是这样的

INSERT INTO loot_template (item_entry, npc_entry, chance, mincount, maxcount, ffa) VALUES 
(1, 2, 3, 4, 5,0),
(2, 3, 4, 5, 6,1),
(3, 4, 5, 6, 7,0);

So how do I do this? 那么我该怎么做呢? I know that I need an foreach loop. 我知道我需要一个foreach循环。 I could fix it for one textbox if I multiply the textboxes but since i have more textboxes I'm not sure what to do. 如果我将多个文本框相乘,则可以修复一个文本框,但是由于我有更多的文本框,因此我不确定该怎么做。

So this is the php code that I have: 这是我拥有的php代码:

$npcentry = $_POST['npcEntry'];
mysqli_select_db($conn, $webdb);
foreach ( $_POST['itemEntry'] as $value) {
  $query = mysqli_query($conn, "INSERT INTO loot_template (npc_entry, item_entry) VALUES($npcentry, $value)");
}

as you see in this code example it uses foreach for itemEntry but I don't know how do to it for the rest of the textboxes. 如您在此代码示例中看到的,它对项目条目使用foreach,但对于其他文本框,我不知道如何处理。

Is it possible to do maybe something like: 是否有可能做类似的事情:

foreach($_POST['itemEntry'] as $value1, $_POST['chance'] as $value2, $_POST['mincount'] as $value3)

If you know that there will always be one of every element in the group you could do something simple like 如果您知道组中总是有每个元素之一,则可以做一些简单的事情,例如

foreach ($_POST['itemEntry'] as $key => $value) {
    $itemEntry = $value;
    $change = $_POST['chance'][$key];
    $minCount = $_POST['mincount'][$key];

    // do entry to db here with extra values
}

I would also change your js to add the counter to the other inputs as well 我也将更改您的js以将计数器也添加到其他输入中

$('#container').append(
            '<div id="inputbox"><strong>Item' + counter + '</strong><br />'
            + '<input name="itemEntry[' + counter + ']' + '" type="text" placeholder="Item Entry"><br>'
            + '<input type="text" name="chance[' + counter + ']" placeholder="Drop Chance"><br>'
            + '<input type="text" name="mincount[' + counter + ']" placeholder="Mincount"><br>'
            + '<input type="text" name="maxcount[' + counter + ']" placeholder="Maxcount"><br>'
            + 'No<input type="radio" name="ffa[' + counter + ']" value="0"><br>'
            + 'Yes<input type="radio" name="ffa[' + counter + ']" value="1"><br></div>'
          );

Placing each group of textboxes inside of an array should make it a little easier to construct your query. 将每组文本框放置在数组中应使构造查询更加容易。

naming your form elements something like: 将表单元素命名为:

group[0][itemEntry]
group[0][chance]
group[0][mincount]
group[0][maxcount]
group[0][ffa]

so your JS might look something like: 因此您的JS可能类似于:

var counter = 1;
      $(function(){
        $('#clicker').click(function(){
          counter += 1;
          $('#container').append(
            '<div id="inputbox"><strong>Item' + counter + '</strong><br />'
            + '<input name="group[' + counter + '][itemEntry]' + '" type="text" placeholder="Item Entry"><br>'
            + '<input type="text" name="group[' + counter + '][chance]" placeholder="Drop Chance"><br>'
            + '<input type="text" name="group[' + counter + '][mincount]" placeholder="Mincount"><br>'
            + '<input type="text" name="group[' + counter + '][maxcount]" placeholder="Maxcount"><br>'
            + 'No<input type="radio" name="group[' + counter + '][ffa]" value="0"><br>'
            + 'Yes<input type="radio" name="group[' + counter + '][ffa]" value="1"><br></div>'
          );
        });
      });

Then building your query in PHP might look like: 然后在PHP中构建查询可能如下所示:

$query = "INSERT INTO loot_template (item_entry, npc_entry, chance, mincount, maxcount, ffa) VALUES";

$numGroups = count($_POST['group'];

foreach($_POST['group'] as $i=>$group){
   $valueSet = "('" . 
                 $group['itemEntry'] . "','" .
                 $_POST['npcEntry'] . "','" .
                 $group['chance'] .  "','" .
                 $group['mincount'] .  "','" .
                 $group['maxcount'] .  "','" .
                 $group['ffa'] . "'" .
                "')";

    valueSet .= ($i+1 < numGroups) ? "," : ";";

   $query .= $valueSet;
}

Sorry, I can't run the code to test it. 抱歉,我无法运行代码进行测试。 There are probably some typos/syntax errors but hopefully that points you in the right direction. 可能存在一些拼写错误/语法错误,但希望可以为您指明正确的方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM