PHP数组没有向数据库插入多行

时间:2016-03-05 14:36:30

标签: javascript php jquery mysql sql

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

例如,在表单中是字段:

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>

PHP要插入数据库。它应该将所有添加的行的值插入到多个数据库表的行中,但是现在它不会插入任何内容。

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

我使用了var_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();
    });
});

HTML:

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

但在这种情况下,我得到了Id = ' + 'NameOfVessel' + noOfClicks + '。据我所知,我需要通过javascript进行连接,只是我无法正确实现它。

1 个答案:

答案 0 :(得分:4)

  

string mysqli_real_escape_string(mysqli $ link,string $ escapestr)

docs

mysqli_real_escape_string需要一个字符串,而不是一个数组。

您应首先循环$_POST['NameOfVessel']数组并对值应用mysqli_real_escape_string。其他帖子也一样。

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

要在克隆后获得ID的唯一性,请参阅以下答案:jQuery clone and change Ids

需要一些适应性。也许完全删除id更容易。