Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
添加到使用JavaScript生成的输入字段的数据库数组_Javascript_Php_Jquery_Database_Field - Fatal编程技术网

添加到使用JavaScript生成的输入字段的数据库数组

添加到使用JavaScript生成的输入字段的数据库数组,javascript,php,jquery,database,field,Javascript,Php,Jquery,Database,Field,我试着为我的问题到处寻找解决方案,但似乎找不到,所以我决定自己去问。我正在做一个项目,我正在用JavaScript创建多个输入字段,下面是代码: HTML: 它仍然不起作用。PHP处理一个表单帖子,其中包含多个与数组同名的元素,只要您向name属性添加[] 所以 现在,我看到您在javascript中为添加到页面中的每个元素创建ID。我想你会发现你根本不需要它。附带说明:一旦你让它工作起来,然后使用准备好的语句来保护你的数据库免受sql注入攻击。我尝试过使用这段代码,它在上下文中是有意义的,我在

我试着为我的问题到处寻找解决方案,但似乎找不到,所以我决定自己去问。我正在做一个项目,我正在用JavaScript创建多个输入字段,下面是代码:

HTML:


它仍然不起作用。

PHP处理一个表单帖子,其中包含多个与数组同名的元素,只要您向name属性添加
[]

所以


现在,我看到您在javascript中为添加到页面中的每个元素创建ID。我想你会发现你根本不需要它。

附带说明:一旦你让它工作起来,然后使用准备好的语句来保护你的数据库免受sql注入攻击。我尝试过使用这段代码,它在上下文中是有意义的,我在其他线程中也见过类似的解决方案,但出于某种原因,它对我不起作用,它没有向数据库中添加任何内容。我只是编辑了原始帖子,以显示我的新php代码的外观。看起来不错。检查查看
$\u POST['field']
包含的内容,并使用
var\u dump
或类似工具检查生成的查询。我现在可以使用了!我只需要做一个改变,用$\u GET代替$\u POST,非常感谢你的帮助,我已经将你的回复标记为最佳答案。
<form id="buildyourform">
<legend>Registration Page</legend>
    <hr>
<input type="button" value="New Registry" class="add" id="add" />
<input type="submit" value="Save" id="submitForms" />
    <hr>

    <script>myscript</script>

</form>
<script>
        $(document).ready(function() {
            $("#add").click(function() {
                var lastField = $("#buildyourform div:last");
                var intId = (lastField && lastField.length && lastField.data("idx") + 1) || 1;
                var fieldWrapper = $("<div class=\"fieldwrapper\" id=\"field" + intId + "\"/>");
                fieldWrapper.data("idx", intId);
                var fName = $("<input type=\"text\" class=\"fieldname\" placeholder=\"Name of New Registry\" name=\"field\" />");
                var removeButton = $("<input type=\"button\" class=\"remove\" value=\"x\" />");

                removeButton.click(function() {
                    $(this).parent().remove();
                });

                fieldWrapper.append(fName);
                fieldWrapper.append(removeButton);
                $("#buildyourform").append(fieldWrapper);
            });
        });
</script>
<?php
    require_once 'conector_superadmin.php';
    $link = connect($conn);

    $NameRegistryInput = $_POST['field'];

    $sql = "INSERT INTO `database` (`idRegistryName`, `Name`) VALUES (NULL, '$NameRegistryInput');";

    mysqli_query($link, $sql);
?>
require_once 'conector_superadmin.php';
    $link = connect($conn);


        foreach ($_POST['field'] as $NameRegistryInput) {
            $sql = "INSERT INTO `database` (`idRegistryName`, `Name`) VALUES (NULL, '$NameRegistryInput');";
            mysqli_query($link, $sql);
        }
var fName = $("<input type=\"text\" class=\"fieldname\" placeholder=\"Name of New Registry\" name=\"field[]\" />");
foreach ($_POST['field'] as $NameRegistryInput) {
  $sql = "INSERT INTO `database` (`idRegistryName`, `Name`) VALUES (NULL, '$NameRegistryInput');";
  mysqli_query($link, $sql);
}