将多个项目作为一个项目插入到db中

时间:2011-09-08 12:55:12

标签: php javascript jquery

我正在尝试接受表单中的多个项目,当它们到达php进行处理时,拆分项目并将它们视为单独的项目以插入到数据库中。目前正在发生的事情是,项目作为一个项目被插入到数据库中。例如,我输入item1,item2,item3。这将在一列中插入到db中。一列中的所有三个项目。如何更正我的代码以使每个项目都在自己的列中。非常感谢

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

+++ +++ UPDATE

    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";
    }
 }

2 个答案:

答案 0 :(得分:1)

我认为您需要explode $ _POST ['BRVbrtrv_boxnumber'],然后使用for循环处理返回的数组,在其中进行插入。

答案 1 :(得分:1)

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');
} 
相关问题