Сохранение данных сетки с использованием PHP в mySQL

Я использовал массив, но он не работает.

Код как ниже:

$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

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

Другие решения

Других решений пока нет …

По вопросам рекламы [email protected]