0

这是来自 $_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)");
}

这是工作,但我需要它是动态的。

4

2 回答 2

0

也许你需要这样的东西:

您拥有从输入 $_POST 字段中获取的数据。

$data = array('fname' => $_POST['fname'], 'lname' => $_POST['lname']);

然后你需要像这样内爆它们:

$values = '\'' . implode('\', \'', $data) . '\'';
$fields = implode(', ', array_keys($data));

最后,您可以像这样将它们插入数据库:

$link = mysqli_connect("localhost", "user", "pass", "db");

if (mysqli_connect_errno()) 
{
    printf("Failed!", mysqli_connect_error());
    exit();
}

if (mysqli_query($link, "INSERT INTO $table ($fields) VALUES ($values) ") === TRUE) 
{
    printf("Success!");
}
于 2013-07-22T21:53:15.383 回答
0

您可以使用 foreach 获取密钥:

$desktop = array();
$laptop = array();
$keys = array();
foreach($_POST as $key => $value) {
    $desktop[] = $value[0];
    $laptop[] = $value[1];
    $keys[] = $key;
}

$fields = implode(', ', $keys);
$desktopQ = implode(', ', $desktop);
$laptopQ = implode(', ', $laptop);


$link = mysqli_connect("localhost", "my_user", "my_password", "db");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

/* Insert into database */
if (mysqli_query($link, "INSERT INTO $table ($fields) VALUES ($desktopQ) ") === TRUE) {
    printf("YEAH!  Insertion success for Desktops!.\n");
}

/* Insert into database */
if (mysqli_query($link, "INSERT INTO $table ($fields) VALUES ($laptopQ) ") === TRUE) {
    printf("YEAH!  Insertion success for Laptops!.\n");
}
于 2013-07-22T21:31:52.417 回答