我试图从表单中接受多个项目,当它们到达 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";
}
}
最佳答案
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 )
{
$query = 'INSERT INTO `act` (`service`, `activity`, `department`, `company`,
`address`, `user`, `item`, `destroydate`, `date`
,`new`)
VALUES (\''.$service.'\', \''.$activity.'\', \''.$department.'\', \''.$company.'\', \''.$address.'\', \''.$authorised.'\', \''.$item_name.'\', NULL, NOW(), \''.$new.'\');';
mysql_query($query) or die('Error, query failed');
}
关于php - 多个项目作为一项插入到数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7348358/