简体   繁体   English

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

[英]PHP array not inserting multiple rows to database

I have created complex form, I'll try to provide simplified examples. 我创建了复杂的表单,我将尝试提供简化的示例。 There are ability to generate more fields by clicking + button. 单击+按钮可以生成更多字段。

For example in form are fields: 例如,形式是字段:

Certificate    Date Of Issue    Date of Expire   
[         ]    [           ]    [            ]   +

by clicking + button It add duplicate row (via javascript) so after clicking + button part of form looks like: 通过单击+按钮它添加重复的行(通过javascript),因此在单击+按钮后,表单的一部分看起来像:

NameOfVessel    TypeOfVessel       YearBuilt  
[          ]    [           ]    [            ]

NameOfVessel    TypeOfVessel       YearBuilt  
[          ]    [           ]    [            ]   +

There are ability to click + button as many times as user needs. 可以按用户需要多次单击+按钮。

I have HTML form like this: 我有这样的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>

PHP to insert to database. PHP插入数据库。 It should insert values from all added rows to multiple database table's rows, but for now it not inserting anything. 它应该将所有添加的行中的值插入到多个数据库表的行中,但目前不插入任何内容。

$UserID = get_current_user_id();
$NameOfVessel = mysqli_real_escape_string($link, $_POST['NameOfVessel']);       
$TypeOfVessel = mysqli_real_escape_string($link, $_POST['TypeOfVessel']);       
$YearBuilt = mysqli_real_escape_string($link, $_POST['YearBuilt']); 

foreach($NameOfVessel as $key=>$res) {
    $sql2 = "INSERT INTO CV_SeaServices (NameOfVessel, UserId, TypeOfVessel, YearBuilt) VALUES ('$res', '$UserId[$key]', '$TypeOfVessel[$key]', '$YearBuilt[$key]')";
    if(mysqli_query($link, $sql2)){
        echo "Resume created successfully.";
    } else {
        echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
    }
}
var_dump($NameOfVessel);

I've used var_dump , but it returning NULL . 我用过var_dump ,但是它返回NULL What's wrong with this code? 此代码有什么问题? Have you any ideas? 你有什么想法吗?

UPDATE UPDATE

I've tried to do in following: 我试图在以下方面做:

JS: 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();
    });
});

HTML: HTML:

<input id="NameOfVessel' + noOfClicks + '" type="text" name="NameOfVessel[]" class="field-style field-split25 align-left" placeholder="Name of Vessel" />

But in this case I got Id = ' + 'NameOfVessel' + noOfClicks + ' . 但是在这种情况下,我得到了Id = ' + 'NameOfVessel' + noOfClicks + ' As I understood I need to do that concatenation via javascript, just I can't achieve It correctly. 据我了解,我需要通过javascript进行串联,只是我无法正确实现它。

string mysqli_real_escape_string ( mysqli $link , string $escapestr ) 字符串mysqli_real_escape_string(mysqli $ link,字符串$ escapestr)

( docs ) docs

mysqli_real_escape_string expects a string, not an array. mysqli_real_escape_string需要一个字符串,而不是数组。

You should first loop the $_POST['NameOfVessel'] array and apply mysqli_real_escape_string on the values. 您应该首先循环$_POST['NameOfVessel']数组,并将mysqli_real_escape_string应用于这些值。 Same goes for the other post keys. 其他帖子键也是如此。

Assuming that $_POST['NameOfVessel'] , $_POST['TypeOfVessel'] and $_POST['YearBuilt'] have the same number of elements, you can do something like: 假设$_POST['NameOfVessel']$_POST['TypeOfVessel']$_POST['YearBuilt']具有相同数量的元素,则可以执行以下操作:

$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);
    }
}

To achieve uniqueness of IDs after cloning, see this answer: jQuery clone and change Ids . 要在克隆后实现ID的唯一性,请参见此答案: jQuery clone and change Ids

It requires some adapting. 它需要一些适应。 Maybe it's easier to remove the id's altogether. 完全删除ID可能更容易。

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

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