2011-09-08 79 views
0

我試圖接受多個項目從一個窗體,當他們到達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 

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

您的$ _POST數組在foreach之前的樣子是什麼? –

+0

@interstellar這是JSON輸出做只是$ boxnumber = $ _POST ['BRVbrtrv_boxnumber']; { 「boxnumber」:「Array」, 「boxcount」:「2」 } – bollo

回答

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'); 
} 
+0

我已經更新了我的原始代碼,並將db從等式中刪除。我只返回2個輸入中的第一個框。謝謝 – bollo

1

我想你需要explode $ _POST ['BRVbrtrv_boxnumber'],然後有一個for循環處理返回的數組,在其中進行插入操作。

+0

請問你有一個工作的例子嗎?謝謝 – bollo