Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/438.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.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 PHP数组未向数据库插入多行_Javascript_Php_Jquery_Mysql_Sql - Fatal编程技术网

Javascript PHP数组未向数据库插入多行

Javascript PHP数组未向数据库插入多行,javascript,php,jquery,mysql,sql,Javascript,Php,Jquery,Mysql,Sql,我已经创建了复杂的表单,我将尝试提供简化的示例。单击+按钮可以生成更多字段 例如,表单中的字段: Certificate Date Of Issue Date of Expire [ ] [ ] [ ] + 通过单击+按钮,它添加了重复的行(通过javascript),因此在单击+按钮后,表单的一部分如下所示: NameOfVessel TypeOfVessel YearBuilt

我已经创建了复杂的表单,我将尝试提供简化的示例。单击
+
按钮可以生成更多字段

例如,表单中的字段:

Certificate    Date Of Issue    Date of Expire   
[         ]    [           ]    [            ]   +
通过单击
+
按钮,它添加了重复的行(通过javascript),因此在单击
+
按钮后,表单的一部分如下所示:

NameOfVessel    TypeOfVessel       YearBuilt  
[          ]    [           ]    [            ]

NameOfVessel    TypeOfVessel       YearBuilt  
[          ]    [           ]    [            ]   +
用户可以根据需要多次单击
+
按钮

我有如下HTML表单:

<li>
    <ul class="column">         
        <li>
            <label for="NameOfVessel">Name of Vessel</label>
            <input id="NameOfVessel" type="text" name="NameOfVessel[]" class="field-style field-split25 align-left" placeholder="Name of Vessel" /> 
        </li>
    </ul>
</li>
<li>
    <ul class="column">         
        <li>
            <label for="TypeOfVessel">Type of Vessel</label>
            <input id="TypeOfVessel" type="text" name="TypeOfVessel[]" class="field-style field-split25 align-left" placeholder="Type of Vessel" /> 
        </li>           
    </ul>
</li>
<li>
    <ul class="column">         
        <li>
            <label for="YearBuilt">Year Built</label>
            <input id="YearBuilt" type="text" name="YearBuilt[]" class="field-style field-split25 align-left" placeholder="Year Built" />   
        </li>           
    </ul>
</li>
我使用了
var\u dump
,但它返回
NULL
。这个代码怎么了?你有什么想法吗

更新

我试着做到以下几点:

JS:

var noOfClicks = 0;
$(document).ready(function() {
    $(".add-row").click(function() {
        $("ul.sea-service").first().clone().appendTo(".personal-details1").append('<button class="remove">X</button>').find('input').val('');
        noOfClicks += 1;

    });
    $("body").on('click', '.remove', function() {
        $(this).closest('.sea-service').remove();
    });
});
<input id="NameOfVessel' + noOfClicks + '" type="text" name="NameOfVessel[]" class="field-style field-split25 align-left" placeholder="Name of Vessel" />
var noOfClicks=0;
$(文档).ready(函数(){
$(“.add行”)。单击(函数(){
$(“ul.sea服务”).first().clone().appendTo(“.personal-details1”).append('X').find('input').val('');
noOfClicks+=1;
});
$(“body”)。在('click','remove',function()上{
$(this).最近('.sea service').remove();
});
});
HTML:

var noOfClicks = 0;
$(document).ready(function() {
    $(".add-row").click(function() {
        $("ul.sea-service").first().clone().appendTo(".personal-details1").append('<button class="remove">X</button>').find('input').val('');
        noOfClicks += 1;

    });
    $("body").on('click', '.remove', function() {
        $(this).closest('.sea-service').remove();
    });
});
<input id="NameOfVessel' + noOfClicks + '" type="text" name="NameOfVessel[]" class="field-style field-split25 align-left" placeholder="Name of Vessel" />

但在本例中,我得到了Id=
“+”容器名称“+noOfClicks+”
。据我所知,我需要通过javascript进行连接,只是我不能正确地实现它

字符串mysqli\u real\u escape\u字符串(mysqli$link,字符串$escapestr)

()

mysqli\u real\u escape\u string
需要字符串,而不是数组

您应该首先循环
$\u POST['nameofVesser']
数组,并对值应用
mysqli\u real\u escape\u字符串。其他post键也是如此

假设
$\u POST['nameofVesser']
$\u POST['typeofVesser']
$\u POST['yearbuilded']
具有相同数量的元素,可以执行以下操作:

$userId = $UserId[$key]; // because you're overriding `$key` below.
foreach($_POST['NameOfVessel'] as $key => $val){
    $NameOfVessel = $val;
    $TypeOfVessel = $_POST['TypeOfVessel'][$key];
    $YearBuilt    = $_POST['YearBuilt'][$key];

    $NameOfVessel = mysqli_real_escape_string($link, $NameOfVessel); 
    $TypeOfVessel = mysqli_real_escape_string($link, $TypeOfVessel); 
    $YearBuilt    = mysqli_real_escape_string($link, $YearBuilt); 

    $sql2 = "INSERT INTO CV_SeaServices 
            (NameOfVessel, UserId, TypeOfVessel, YearBuilt) 
            VALUES 
            ('$res', '$userId', '$TypeOfVessel', '$YearBuilt')";

    if(mysqli_query($link, $sql2)){
        echo "Resume created successfully.";
    } else {
        echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
    }
}
要在克隆后实现ID的唯一性,请参阅以下答案:


这需要一些调整。可能更容易完全删除id。

那么-您的mysqli链接/连接可能有问题?当你
var\u dump($\u POST['nameofVesser'])
时你会得到什么?谢谢你的回答,但我有点不清楚,我应该如何正确地使用loop和
$\u POST['nameofVesser']
呢?谢谢你,这几乎可以工作了。通过单击
+
按钮添加多个字段,也可以添加
下拉列表,但只能从第一行插入值的下拉列表中添加。你知道为什么吗?你的ID不是唯一的。刚刚注意到了这一点:
id=“nameofVesser”
如果您对每一行都重复这一点,再加上一个通过id发送它们的AJAX调用,就会得到重叠的值。确保您的id是唯一的:)谢谢您的回答,但是我如何才能实现所有id都是唯一的呢?您可以有一个js变量,如
var noOfClicks=0并在“添加行”按钮上增加它(或不管它叫什么)。将初始id设置为
NameOfVessel0
(以及所有其他)。在您的
javascript复制中
,只需连接类似
id=“+'nameofvesser'+noOfClicks+'”
的内容,而不是
id=“nameofvesser”