将数组从表单字段提交到数据库

时间:2017-02-10 18:25:40

标签: javascript php jquery mysql ajax

在订单页面上有一个缝纫管理应用程序,有人可以有多个订单,但所有订单都要使用一个参考代码开具发票。第一页,我收集了定价等基本细节,但是在下一页上,每个订单都必须更详细地输入,材料的示例图片,说明和样式代码(如果有的话)。

所以我已经能够成功创建数据录入人员可以添加多个细节的页面,甚至可以选择是否有样式,但是我在数据库提交中陷入困境。

请记住,正在使用的每个其他页面,例如员工管理和其他正常工作,所以不提交到数据库的这个错误与包含的外部页面或任何其他事情无关,除了可能如何处理数组。以下是代码的细分;

JavaScript的:

<script>
function showfield(name,event){

if(name=='iHaveStyle') {
$(event).next('#div1').html('<label>Enter Style Code</label><br /> <input type="text" name="style[]" />');
} else{
 $(event).next('#div1').html('');
}

}
$(document).ready(function() {
var max_fields      = 10; //maximum input boxes allowed
var wrapper         = $(".form-group"); //Fields wrapper
var add_button      = $(".add_field_button"); //Add button ID
var x = 1; //initlal text box count
$(add_button).click(function(e){ //on add input button click
  e.preventDefault();
    if(x < max_fields){ //max input box allowed
        x++; //text box increment
        $(wrapper).append('<div style="margin-top:20px; border-top:1px solid #333333;"><label>Upload Sample Material</label><br /><input type="file" name="sample_material[]" style="width:200px; height: 40px;" /><br/><br/><label>Customer&#39s Requirement</label><br /><textarea name="cust_requirement[]" style="width:200px; height: 150px;" /></textarea><br/><br/><label>Do you have a style</label><br /><select name="sketch_code" id="sketch_code" onchange="showfield(this.options[this.selectedIndex].value,this)"><option value="">I don&#39t have a style code</option><option value="iHaveStyle">I have a style code</option></select><div id="div1"></div><br /><a href="#" class="remove_field">Remove</a></div>'); //add input box
      }
    });
$(wrapper).on("click",".remove_field", function(e){ //user click on remove  text
  e.preventDefault(); $(this).parent('div').remove(); x--;
})
});
</script>

HTML:

<form action="orderadd1.php?id=<?php echo "".$order_id; ?>" method="post" enctype="multipart/form-data">
<div class="form-group">
<button class="add_field_button">Add another sub-order</button>
<div style="margin-top:20px;">
  <label>Upload Sample Material</label><br />
  <input type="file" name="sample_material[]" style="width:200px; height: 40px;" /><br/><br/>
  <label>Customer's Requirement</label><br />
  <textarea name="cust_requirement[]" style="width:200px; height: 150px;" /> </textarea><br/><br/>
  <label>Do you have a style</label><br />
  <select name="sketch_code" id="sketch_code"  onchange="showfield(this.options[this.selectedIndex].value,this)">
    <option value="">I don't have a style code</option>
    <option value="iHaveStyle">I have a style code</option>
  </select>
  <div id="div1"></div>
  </div>
  </div>
  <input type="submit" class="btn btn-lg btn-color btn-block" value="Continue Order" name="submit_val">
  </form>

PHP代码:

<?php

$order_id = $_REQUEST['id'];

if (isset($_POST['submit_val'])) {

if(is_array($_POST['cust_requirement']))
{
for($i=0; $i < count($_POST['cust_requirement']); $i++ ) {

$cust_requirement = mysql_real_escape_string($_POST['cust_requirement'][$i]);
$style = mysql_real_escape_string($_POST['style'][$i]);
$folder = "sample_material/";
$extention = strrchr($_FILES['sample_material']['name'], ".");
$new_name = $order_id."-".$i++;
$sample_material = $new_name.'.jpg';
$uploaddir = $folder . $sample_material;
move_uploaded_file($_FILES['submit_material']['tmp_name'], $uploaddir);

$data_submit = $pdo->query("INSERT INTO `order_desc` (order_id, sample_photo_url, cust_requirements, style_code) VALUES ('".$order_id."', '".$uploaddir."', '".$cust_req."', '".$style."')");

}}
}

?>

它显示的错误是:

  

警告:strip_tags()期望参数1为字符串,第2行的C:\ xampp \ htdocs \ tms \ header.php中给出的数组

我检查了header.php,这正是我所拥有的:

<?php
 $_POST = array_map('strip_tags', $_POST);
 ?>

我在所有页面上都使用了这个标题,它们很好。

2 个答案:

答案 0 :(得分:3)

array_map函数将strip_tags()应用于数组的每个元素 - 但最可能的原因是你的$ POST看起来像这样[&#39; test1&#39;,[&#39; test2&#39; ,&#39; test3]],$ POST [1] == [&#39; test2&#39;,&#39; test3]是一个数组,并且无法应用条带标记。您可以使用循环并检查item是否不是要剥离的数组,或者使用递归遍历您$ POST数组

答案 1 :(得分:2)

您的POST超全局包含数组,这就是您使用array_map收到警告的原因。尝试将其替换为array_walk_recursive http://php.net/manual/en/function.array-walk-recursive.php