在mySQL中使用PHP保存网格数据

时间:2015-05-28 05:18:21

标签: php mysql grid save

我使用了一个数组,但它不起作用。

代码如下:

$resource = $_POST['resource'];
    for ($i=0; $i<count($resource); $i++) 
    {

        $sql = mysql_query("INSERT INTO tbl_user_permissions (`resource`,`role_id`,`membership_id`,`create`,`read`,`update`,`delete`,`approve`) VALUES ('".$_POST['resource'][$i]."','".$_POST['role_id']."','".$_POST['membership_id']."','".$_POST['create'][$i]."','".$_POST['read'][$i]."','".$_POST['update'][$i]."','".$_POST['delete'][$i]."','".$_POST['approve'][$i]."')");


    }

在执行print_r($_POST'resource']时,它会给我以下输出:

User [4] => Groups [5] => Sub Groups [6] => Roles [7] => My Trucks [8]
=> My Drivers [9] => e-Confirm [10] => e-Trak [11] => Product & Pricing [12] => Content Management ) 1Array ( [0] => Post Load [1] => Load Truck [2] => Truck Post [3] => User [4] => Groups [5] => Sub Groups [6] => Roles [7] => My Trucks [8] => My Drivers [9] => e-Confirm [10] => e-Trak [11] => Product & Pricing [12] => Content Management ) 1Array ( [0] => Post Load [1] => Load Truck [2] => Truck Post [3] => User [4] => Groups [5] => Sub Groups [6] => Roles [7] => My Trucks [8] => My Drivers [9] => e-Confirm [10] => e-Trak [11] => Product & Pricing [12] => Content Management ) 1Array ( [0] => Post Load [1] => Load Truck [2] => Truck Post [3] => User [4] => Groups [5]
=> Sub Groups [6] => Roles [7] => My Trucks [8] => My Drivers [9] => e-Confirm [10] => e-Trak [11] => Product & Pricing [12] => Content Management ) 1Array ( [0] => Post Load [1] => Load Truck [2] => Truck Post [3] => User [4] => Groups [5] => Sub Groups [6] => Roles [7] => My Trucks [8] => My Drivers [9] => e-Confirm [10] => e-Trak [11] => Product & Pricing [12] => Content Management ) 1Array ( [0] => Post Load [1] => Load Truck [2] => Truck Post [3] => User [4] => Groups [5]
=> Sub Groups [6] => Roles [7] => My Trucks [8] => My Drivers [9] => e-Confirm [10] => e-Trak [11] => Product & Pricing [12] => Content Management ) 1Array ( [0] => Post Load [1] => Load Truck [2] => Truck Post [3] => User [4] => Groups [5] => Sub Groups [6] => Roles [7] => My Trucks [8] => My Drivers [9] => e-Confirm [10] => e-Trak [11] => Product & Pricing [12] => Content Management ) 1Array ( [0] => Post Load [1] => Load Truck [2] => Truck Post [3] => User [4] => Groups [5]
=> Sub Groups [6] => Roles [7] => My Trucks [8] => My Drivers [9] => e-Confirm [10] => e-Trak [11] => Product & Pricing [12] => Content Management ) 1Array ( [0] => Post Load [1] => Load Truck [2] => Truck Post [3] => User [4] => Groups [5] => Sub Groups [6] => Roles [7] => My Trucks [8] => My Drivers [9] => e-Confirm [10] => e-Trak [11] => Product & Pricing [12] => Content Management ) 1Array ( [0] => Post Load [1] => Load Truck [2] => Truck Post [3] => User [4] => Groups [5]
=> Sub Groups [6] => Roles [7] => My Trucks [8] => My Drivers [9] => e-Confirm [10] => e-Trak [11] => Product & Pricing [12] => Content Management ) 1Array ( [0] => Post Load [1] => Load Truck [2] => Truck Post [3] => User [4] => Groups [5] => Sub Groups [6] => Roles [7] => My Trucks [8] => My Drivers [9] => e-Confirm [10] => e-Trak [11] => Product & Pricing [12] => Content Management ) 1Array ( [0] => Post Load [1] => Load Truck [2] => Truck Post [3] => User [4] => Groups [5]
=> Sub Groups [6] => Roles [7] => My Trucks [8] => My Drivers [9] => e-Confirm [10] => e-Trak [11] => Product & Pricing [12] => Content Management ) 1Array ( [0] => Post Load [1] => Load Truck [2] => Truck Post [3] => User [4] => Groups [5] => Sub Groups [6] => Roles [7] => My Trucks [8] => My Drivers [9] => e-Confirm [10] => e-Trak [11] => Product & Pricing [12] => Content Management ) 1

1 个答案:

答案 0 :(得分:0)

/* First i make the variable $i is 13 because i see there 13 record */

for ($i=0; $i<14; $i++){
    $resource = !empty($_POST['resource'][$i]) ? $_POST['resource'][$i] : 0;
    $create = !empty($_POST['create'][$i]) ? $_POST['create'][$i] : 0;
    $read = !empty($_POST['read'][$i]) ? $_POST['read'][$i] : 0;
    $update = !empty($_POST['update'][$i]) ? $_POST['update'][$i] : 0;
    $delete = !empty($_POST['delete'][$i]) ? $_POST['delete'][$i] : 0;
    $approve = !empty($_POST['approve'][$i]) ? $_POST['approve'][$i] : 0;
    $sql = mysql_query("INSERT INTO 
        tbl_user_permissions 
        (`resource`,`role_id`,`membership_id`,`create`,`read`,`update`,`delete`,`approve`) 
        VALUES 
        ('".$resource."','".$_POST['role_id']."','".$_POST['membership_id']."',
        '".$create."','".$read."','".$update."',
        '".$delete."','".$approve."')");
}