php - 多个项目作为一项插入到数据库中

标签 php javascript jquery

我试图从表单中接受多个项目,当它们到达 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/

相关文章:

php - 如何从 jQuery 将对象(或数组)传递到 PHP?

PHP,缩进宽度为4,CodeSniffer : is "4" a de-facto standard?

php - 如何在联系表 7 中提交表单后重定向?

javascript - jquery通过php将新行追加到ul li

php - 非对称和对称 key 存储

javascript - 销毁 jQuery 对话框会删除附加到其中元素的任何处理程序吗?

javascript - 检查对话框是否打开会抛出 "cannot call methods on dialog prior to initialization"错误

javascript - 使用 Javascript 在浏览器中解析 PDF 表单数据

jquery - 有没有办法在 JQuery Datatable 中的标题文本旁边显示排序图标?

javascript - 使用jQuery(或JSF方式)来防止h :commandButton submit