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

时间:2011-01-26 12:25:50

标签: php database arrays forms

我正在尝试将表单上传到数据库,该表单使用相同的字段名称作为CMS的一部分。

这是表单(我使用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>

这就是我到目前为止的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;
}

它有点工作,但只是将“数组”这个词放入数据库中。此外,htmlentities和mysql_real_escape_string帖子不会向数据库上传任何内容。

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

你忘记了什么:)

$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]."');");

在上面的代码中查看所有这些$i。如果你不使用它们,脚本将尝试使用整个数组(并尝试打印或保存数组作为字符串总是导致打印“数组”)。

P.S.I've编辑了我的答案。对于前一个,很抱歉,我误解了代码并发布了错误的答案。

答案 1 :(得分:1)

你需要循环

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

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