php和mysql插入输入数组

时间:2022-09-26 07:46:38

I have a problem with 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.

我对php有问题。我会尽量解释清楚,尽量简短,希望你能理解我的意思。

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. 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. I want to run the result from an HTML form in my database.

正如您在codepen的示例中看到的,如果单击超链接,我的文本框可以相乘。但是第一个输入不应该相乘。在输入中,我使用方括号。我要算出这个的php部分。我想在我的数据库中运行HTML表单的结果。

So, Lets say I multiply textboxes 3 times. The query should be something like

假设我将文本框乘以3。查询应该是这样的

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. 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.

我该怎么做呢?我知道我需要一个foreach循环。如果我把文本框相乘,我可以把它固定在一个文本框上但是因为我有更多的文本框,我不知道该怎么做。

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作为itemEntry,但是我不知道如何对其他文本框进行处理。

Is it possible to do maybe something like:

有没有可能做这样的事情:

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

2 个解决方案

#1


1  

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

#2


1  

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.

对不起,我不能运行代码来测试它。可能有一些拼写错误/语法错误,但希望这能让您找到正确的方向。

#1


1  

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

#2


1  

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.

对不起,我不能运行代码来测试它。可能有一些拼写错误/语法错误,但希望这能让您找到正确的方向。