【发布时间】:2011-09-08 12:55:11
【问题描述】:
我试图从一个表单中接受多个项目,当它们到达 php 进行处理时,拆分这些项目并将它们作为单独的项目插入到数据库中。目前正在发生的事情是这些项目作为一个项目被插入到数据库中。例如,我输入 item1,item2,item3。这将在一列中插入到数据库中。一列中的所有三个项目。如何更正我的代码以使每个项目都在自己的列中。非常感谢
php页面
foreach($_POST['BRVbrtrv_boxnumber'] as $i=>$value){
$_POST['BRVbrtrv_boxnumber'][$i]=mysql_real_escape_string($value);
}
$boxnumber = implode( ',', $_POST['BRVbrtrv_boxnumber']);
$query = 'INSERT INTO `act` (`service`, `activity`, `department`, `company`, `address`, `user`, `item`, `destroydate`, `date`, `new`)
VALUES (\''.$service.'\', \''.$activity.'\', \''.$department.'\', \''.$company.'\', \''.$address.'\', \''.$authorised.'\', \''.strtoupper($boxnumber).'\', NULL, NOW(), \''.$new.'\');';
mysql_query($query) or die('Error, query failed');
jquery 输入
for(var i = 0;i < $(this).val();i++) {
$("#BRVbrtrv_boxnumber").append('<div data-role="fieldcontain"><label for="BRVbrtrv_boxnumber" class="ui-input-text">Enter box ' + (i + 1) + ' number:</label><input type="text" name="BRVbrtrv_boxnumber['+i+']" id="BRVbrtrv_boxnumber['+i+']" class="BRV_boxnumber ui-input-text ui-body-null ui-corner-all ui-shadow-inset ui-body-c" /></div>')
}
json 输出
boxnumber: "rff,tgg" <- this is correct values for 2 items that I input
+++更新+++
foreach($_POST['BRVbrtrv_boxnumber'] as $i=>$value){
$_POST['BRVbrtrv_boxnumber'][$i]= strtoupper( mysql_real_escape_string($value) );
}
foreach( $_POST['BRVbrtrv_boxnumber'] as $k => $item_name ){
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
header("Cache-Control: no-cache, must-revalidate" );
header("Pragma: no-cache" );
header("Content-type: application/json");
$json = "";
if(empty($service)) {
$json .= "{\"ErrorService\": \"ERROR: You mest select a service level\"}";
}
else
if($department=="Choose Department") {
$json .= "{\"ErrorService\": \"ERROR: You must select a department\"}";
}
else
if($address=="Choose Address") {
$json .= "{\"ErrorService\": \"ERROR: You must select a retrieval address\"}";
}
else
if(empty($item_name)) {
$json .= "{\"ErrorService\": \"ERROR: You must enter a box for retrieval\"}";
}
else
{
$json .= "{\n";
$json .= "\"boxnumber\": \"".$item_name."\",\n";
$json .= "\"boxcount\": \"".$boxcount."\"\n";
$json .= "}\n";
}
}
【问题讨论】:
-
你的 $_POST 数组在 foreach 之前是什么样子的?
-
@interstellar 这是 json 输出,只是 $boxnumber = $_POST['BRVbrtrv_boxnumber']; { "boxnumber": "数组", "boxcount": "2" }
标签: php javascript jquery