简体   繁体   English

将多个表单$ _post数组上传到数据库

[英]Upload multiple form $_post arrays to database

I'm trying to upload a form to the database, that uses the same field names, as part of a CMS. 我正在尝试将表单上传到数据库,该表单使用相同的字段名称作为CMS的一部分。

This is the form (I have used JQuery to multiply the div the number of options per product): 这是表格(我使用JQuery将div乘以每个产品的选项数量):

<form method="post" enctype="multipart/form-data" action="testing.php">
<div class="OptExtra1">
  <h3>Additional Option</h3>
  <label for="RESAddType">File type (i.e. &ldquo;CD&rdquo; or &ldquo;Download&rdquo;)</label>
  <input name="RESAddType[]" type="text" id="RESAddType" size="48" class="FW" />
  <label for="RESAddTitle">File title (i.e. &ldquo;Boxed Set of 4 CDs&rdquo;)</label>
  <input name="RESAddTitle[]" type="text" id="RESAddTitle" size="48" class="FW" />
  <label for="RESAddFType">File format (As &ldquo;MP3&rdquo; // &ldquo;WORD&rdquo; // &ldquo;PDF&rdquo;)</label>
  <input name="RESAddFType[]" type="text" id="RESAddFType" size="48" class="FW" />
  <label for="RESAddPrice">File price (Enter as &ldquo;6.99&rdquo; &ndash; <strong>NO &ldquo;&pound;&rdquo; SIGN!</strong>)</label>
  <input name="RESAddPrice[]" type="text" id="RESAddPrice" size="48" class="FW" />
  <label for="RESAddFName">File name</label>
  <input name="RESAddFName[]" type="text" id="RESAddFName" size="48" class="FW" />
  <label for="RESAddTxt">File text</label>
  <textarea name="RESAddTxt[]" id="RESAddTxt" cols="70" rows="50" class="mceAdvanced"></textarea>
  <label for="RESAddSample">File text</label>
  <textarea name="RESAddSample[]" id="RESAddSample" cols="70" rows="50" class="mceVSimple"></textarea>
 <input type="button" value="Add another option" class="SubmitButton" onclick="inserter()" />
 <hr />
</div>
<p><input type="submit" name="submit" value="Add Resource" class="SubmitButton"/><input type="hidden" name="RESCatCode" value="2" /><input type="hidden" name="RESCatSubCode" value="5" /><input type="hidden" name="submitted" value="true" /></p>

and this is what I have so far for the PHP 这就是我到目前为止对PHP的了解

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

 $RESCode = 100;
 $RESAddType = $_POST['RESAddType'];
 $RESAddTitle = htmlentities($_POST['RESAddTitle'], ENT_QUOTES);
 $RESAddFType = $_POST['RESAddFType'];
 $RESAddPrice = $_POST['RESAddPrice'];
 $RESAddFName = $_POST['RESAddFName'];
 $RESAddTxt = mysql_real_escape_string($_POST['RESAddTxt']);
 $RESAddSample = mysql_real_escape_string($_POST['RESAddSample']);


 for ($i=0; $i < count($_POST['RESAddType']); $i++) {
  $OptionQuery = mysql_query ("INSERT INTO ResAdd (RESAddCode, RESCode, RESAddType, RESAddTitle, RESAddFType, RESAddPrice, RESAddFName, RESAddTxt, RESAddSample) VALUES ('', '".$RESCode."', '".$RESAddType."', '".$RESAddTitle."', '".$RESAddFType."', '".$RESAddPrice."', '".$RESAddFName."', '".$RESAddTxt."', '".$RESAddSample."');");
 }

header("Location: welcome.php");
exit;
}

It kind of worked, but is just putting in the word "array" into the database. 有点奏效,但只是在数据库中添加了“数组”一词。 Also the htmlentities and mysql_real_escape_string posts don't upload anything to the database. htmlentities和mysql_real_escape_string帖子也不会将任何内容上传到数据库。

Any ideas please? 有什么想法吗?

You've forgotten something :) 您忘记了一些东西:)

$OptionQuery = mysql_query ("INSERT INTO ResAdd (RESAddCode, RESCode, RESAddType, RESAddTitle, RESAddFType, RESAddPrice, RESAddFName, RESAddTxt, RESAddSample) VALUES ('', '".$RESCode[$i]."', '".$RESAddType[$i]."', '".$RESAddTitle[$i]."', '".$RESAddFType[$i]."', '".$RESAddPrice[$i]."', '".$RESAddFName[$i]."', '".$RESAddTxt[$i]."', '".$RESAddSample[$i]."');");

Look at all these $i in the code above. 在上面的代码中查看所有这些$i If you don't use them, the script will try to use the entire array (and trying to print or save an array as a string always results in printing "Array"). 如果不使用它们,脚本将尝试使用整个数组(并且尝试将数组打印或保存为字符串总是导致打印“ Array”)。

PSI've edited my answer. PSI编辑了我的答案。 Sorry for the previous one, I've misunderstood the code and posted a wrong answer. 对不起,上一个,我误解了代码并发布了错误的答案。

You need to looping 你需要循环

$cnt=count($_POST['RESAddType']);

for ($counter=0; $counter < $cnt; $counter++)
{
   $_POST['RESAddType'][$counter]// to access the value
   $_POST['RESAddPrice'][$counter]//
   //create your query here
}

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

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