多表单插入查询mysql

时间:2015-05-29 04:14:35

标签: php mysql

有没有人知道如何在循环中格式化这个数组,以便它将在一个查询中插入数据库,但是使用php以循环方式。

我顺便做了一个多重表格。

这里是字段的数组格式,这是我的表单attendance



Array
(
    [sup_payroll_type] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 1
        )

    [sup_month] => Array
        (
            [0] => January
            [1] => January
            [2] => May
        )

    [sup_year] => Array
        (
            [0] => 2015
            [1] => 2015
            [2] => 2015
        )

    [sup_late_days] => Array
        (
            [0] => 1
            [1] => 1
            [2] => 2
        )

    [sup_absent_days] => Array
        (
            [0] => 2
            [1] => 1
            [2] => 0
        )

    [sup_id] => Array
        (
            [0] => 1
            [1] => 5
            [2] => 6
        )

    [sup_emp_id] => Array
        (
            [0] => 24
            [1] => 24
            [2] => 24
        )

    [save_edit_satt] => Array
        (
            [0] => SAVE
        )

)




任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:0)

请尝试以下循环: -

$insertQuery = 'INSERT INTO attendence (sup_payroll_type, sup_month, sup_year, 
                sup_late_days, sup_absent_days, sup_id, sup_emp_id, 
                save_edit_satt) VALUES';
foreach($attendenceArray AS $key => $value){
    $insertQuery .= '("'.$value['sup_payroll_type'].'","'.$value['sup_month'].'",
                    "'.$value['sup_year'].'","'.$value['sup_late_days'].'",
                    "'.$value['sup_absent_days'].'","'.$value['sup_id'].'",
                    "'.$value['sup_emp_id'].'","'.$value['save_edit_satt'].'"),';    
}
$insertQuery = rtrim($insertQuery,',');

$ insertQuery包含你的插入查询。  $ attendenceArray包含你的示例数组。

如果你使用mysqli然后使用mysqli_query($ mysqli,$ insertQuery)来执行你的查询。 $ mysqli是连接对象。

答案 1 :(得分:0)

您只能使用循环

 $insertQuery = 'INSERT INTO attendence (sup_payroll_type, sup_month, sup_year, 
            sup_late_days, sup_absent_days, sup_id, sup_emp_id, 
            save_edit_satt) VALUES';
 foreach($attendenceArray['sup_payroll_type'] AS $key => $value){
 $insertQuery .= '("'.$value.'","'.$attendenceArray['sup_month'][$key].'",
                "'.$attendenceArray['sup_year'][$key].'","'.$attendenceArray['sup_late_days'][$key].'",
                "'.$attendenceArray['sup_absent_days'][$key].'","'.$attendenceArray['sup_id'][$key].'",
                "'.$attendenceArray['sup_emp_id'][$key].'","'.$attendenceArray['save_edit_satt'][$key].'"),';    
}

echo $insertQuery; // Query for insert

答案 2 :(得分:0)

我明白了。



$ctr = 0;
foreach($_POST['sup_payroll_type'] as $row){

	$upd_value 							= array();
	$upd_value['sup_payroll_type'] 		= $_POST['sup_payroll_type'][$ctr];
	$upd_value['sup_month'] 			= $_POST['sup_month'][$ctr];
	$upd_value['sup_year'] 				= $_POST['sup_year'][$ctr];
	$upd_value['sup_late_days'] 		= $_POST['sup_late_days'][$ctr];
	$upd_value['sup_absent_days'] 		= $_POST['sup_absent_days'][$ctr];
	$upd_value['sup_emp_id'] 			= $_POST['sup_emp_id'][$ctr];

	$upd_result = $db_conn->mysql_update($upd_value,'tbl_attendance_support','sup_id='.$_POST['sup_id'][$ctr],'db_norkis');

	$ctr++;
}




顺便说一句,感谢Pankaj K和Shijin分享你的想法=)