这是来自 $_POST 的 var_dump:
array(27) { ["name"]=> array(2) { [0]=> string(8) "desktops" [1]=> string(7) "laptops" } ["type"]=> array(2) { [0]=> string(7) "WorkPro" [1]=> string(8) "WorkPlus" } ["hdd"]=> array(2) { [0]=> string(0) "" [1]=> string(0) "" } ["monitor"]=> array(2) { [0]=> string(0) "" [1]=> string(0) "" } ["processor"]=> array(2) { [0]=> string(0) "" [1]=> string(0) "" } ["ram"]=> array(2) { [0]=> string(0) "" [1]=> string(0) "" } ["net"]=> array(2) { [0]=> string(0) "" [1]=> string(0) "" } ["battery"]=> array(2) { [0]=> string(0) "" [1]=> string(0) "" } ["burner"]=> array(2) { [0]=> string(0) "" [1]=> string(0) "" } ["keyboard"]=> array(2) { [0]=> string(0) "" [1]=> string(0) "" } ["os"]=> array(2) { [0]=> string(0) "" [1]=> string(0) "" } ["office"]=> array(2) { [0]=> string(0) "" [1]=> string(0) "" } ["antivirus"]=> array(2) { [0]=> string(0) "" [1]=> string(0) "" } ["extra_1"]=> array(2) { [0]=> string(0) "" [1]=> string(0) "" } ["extra_2"]=> array(2) { [0]=> string(0) "" [1]=> string(0) "" } ["extra_3"]=> array(2) { [0]=> string(0) "" [1]=> string(0) "" } ["extra_4"]=> array(2) { [0]=> string(0) "" [1]=> string(0) "" } ["extra_5"]=> array(2) { [0]=> string(0) "" [1]=> string(0) "" } ["extra_6"]=> array(2) { [0]=> string(0) "" [1]=> string(0) "" } ["description"]=> array(2) { [0]=> string(0) "" [1]=> string(0) "" } ["lapse"]=> array(2) { [0]=> string(0) "" [1]=> string(0) "" } ["period"]=> array(2) { [0]=> string(3) "mes" [1]=> string(3) "mes" } ["quantity"]=> array(2) { [0]=> string(0) "" [1]=> string(0) "" } ["price"]=> array(2) { [0]=> string(0) "" [1]=> string(0) "" } ["iva"]=> array(2) { [0]=> string(4) "true" [1]=> string(4) "true" } ["total_show"]=> array(2) { [0]=> string(4) "true" [1]=> string(4) "true" } ["save"]=> string(4) "true" }
我有类似的东西,但我不知道如何插入数据库。
foreach ($_POST as $key => $value) {
$data[$key] = $value;
$keys[] = $key;
}
for ($i=0; $i < count($data); $i++) {
print_r($data[$keys[$i]]);
}
我明白了:
Array ( [0] => desktops [1] => laptops ) Array ( [0] => [1] => ) Array ( [0] => [1] => ) Array ( [0] => [1] => ) Array ( [0] => [1] => ) Array ( [0] => [1] => ) Array ( [0] => [1] => ) Array ( [0] => [1] => ) Array ( [0] => [1] => ) Array ( [0] => [1] => ) Array ( [0] => [1] => ) Array ( [0] => [1] => ) Array ( [0] => [1] => ) Array ( [0] => [1] => ) Array ( [0] => [1] => ) Array ( [0] => [1] => ) Array ( [0] => [1] => ) Array ( [0] => [1] => ) Array ( [0] => [1] => ) Array ( [0] => [1] => ) Array ( [0] => [1] => ) Array ( [0] => mes [1] => mes ) Array ( [0] => [1] => ) Array ( [0] => [1] => ) Array ( [0] => true [1] => true ) Array ( [0] => true [1] => true )
我需要用这些数据制作两个插入一个用于台式机和另一个用于笔记本电脑等等。
像这样的东西:
INSERT INTO $table ($keys) VALUES ($values[0]), ($values[1]);
在我有这样的事情之前:
for ( $i=0; $i < count($_POST['name']) ; $i++ ){
$name = DB::esc($_POST['name'][$i]);
$type = DB::esc($_POST['type'][$i]);
$hdd = DB::esc($_POST['hdd'][$i]);
$query = DB::query("INSERT INTO product VALUES($name,$type,$hdd)");
}
这是工作,但我需要它是动态的。