简体   繁体   English

PHP MySQL多次插入

[英]PHP MySQL Multiple Insertion

I'm not quite sure where I'm going wrong :( 我不太确定我要去哪里错了:(

if(isset($_POST['finish'])){
$objectives=addslashes(strip_tags($_POST['item']));

    foreach ($objectives AS $objective) {
        echo "$objective <br />";
    }

}

It's not showing anything.. what have I missed out? 它什么都没有显示..我错过了什么? I'm trying to get data from multiple input entries.. 我正在尝试从多个输入条目中获取数据。

<input class="item" id="objectives" name="item[]" />

Any ideas? 有任何想法吗?

Well if you have multiple <input class="item" id="objectives" name="item[]" /> then $_POST['item'] will be an array and not a string. 好吧,如果您有多个<input class="item" id="objectives" name="item[]" />$_POST['item']将是一个数组而不是字符串。 So you have to iterate over it or apply an array_map function. 因此,您必须对其进行迭代或应用array_map函数。

$items = array_map('strip_tags',$items);
$items = array_map('addslashes',$items);

Your code would then be 您的代码将是

if(isset($_POST['finish'])){
    $_POST['item'] = array_map('strip_tags',$_POST['item']);
    $_POST['item'] = array_map('addslashes',$_POST['item']);

    foreach ($_POST['item'] AS $objective) {
        echo "$objective <br />";
    }

}

The direct answer is that any tag that has brackets([]) is put in the superglobal array as an array. 直接的答案是,任何具有方括号([])的标签都将作为数组放入超全局数组中。 You will need to loop over your this or use array_map to perform functions on this array. 您将需要遍历this或使用array_map在此数组上执行功能。

My extended answer is that if you are using php 5.2 or later you can use the filter_var_array to perform this operation without iterating over your array in php. 我的扩展答案是,如果您使用的是php 5.2或更高版本,则可以使用filter_var_array来执行此操作,而无需遍历php中的数组。 As well as do type checking. 以及类型检查。 filter_var and filter_var_array have to many filter options for me to cover. filter_var和filter_var_array有很多过滤器选项供我介绍。 Please see the docs http://php.net/manual/en/function.filter-var-array.php 请参阅文档http://php.net/manual/en/function.filter-var-array.php

if(isset($_POST['finish'])) {
    $objectives = filter_var_array($_POST['item'], FILTER_SANITIZE_STRING);

foreach ($objectives AS $objective) {
    echo "$objective <br />";
}

} }

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

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