简体   繁体   中英

How to insert values from the generated dynamic fields in the database

I have this sample code that generates dynamic fields.

My problem is I don't know how to insert the data to the database.

I tried implode but the result is not the format I want.

implode (',', $name);
implode (',', $sweets);
implode (',', $fruits);
implode (',', $drinks);

This is my columns (id, name, sweet, fruits, drinks)

so if I generate 3 rows it should be inserted like this

ID | NAME | SWEETS | FRUITS | DRINKS

1 | mark | cake | apple | 1

2 | jenny |candy | mango | 2

3 | randy | choco | ORANGE | 3

Actually I don't know if this is possible. Thanks

FIDDLE

https://jsfiddle.net/jqj1h4vb/2/

This code should get you rolling.
A few things to note before testing out the code:
1 . I noticed some plural inconsistencies in your code which may or may not have to adjusted in regard to the form field names and the database column names. Hopefully I guessed correctly and you can just copy-paste my code to get it to work.
2 . Also take note that my code expects the form data to be POSTed. If you're submitting the form data as a query string, it may or may not get chopped off due to query string limits in various browsers and depending on how much data the user submits at once.
3 . Also my code assumes your database's primary field auto increases, and that you're using MySQL or a SQL-based database, thus there's no need to specify an ID when inserting.
4 . I also hope that you'll add some sort of security to the code, such as flood control, or a captcha or invisible captcha etc to prevent any one user from flooding your database until it hits its limit.

Here's a PDO solution:

<?php
function getPostArray($v)
{
    if(!isset($_POST[$v]))return [];
    if(!is_array($_POST[$v]))return [$_POST[$v]];
    return $_POST[$v];
}

$n = getPostArray('name');
$s = getPostArray('sweet');
$f = getPostArray('fruit');
$d = getPostArray('drinks');

$insertQuery = [];
$insertData = [];
foreach($n as $k=>$v)
{
    $insertData[] = array_key_exists($k, $n) ? $n[$k] : '';
    $insertData[] = array_key_exists($k, $s) ? $s[$k] : '';
    $insertData[] = array_key_exists($k, $f) ? $f[$k] : '';
    $insertData[] = array_key_exists($k, $d) ? $d[$k] : '';
    $insertQuery[] = '(?, ?, ?, ?)';
}
// assuming your table is named `MyTable`
$sql = 'INSERT INTO `MyTable` (name, sweets, fruits, drinks) VALUES ';
if(!empty($insertQuery))
{
    $sql .= implode(', ', $insertQuery);

    // assuming you have a valid PDO $db connection open
    if(isset($db))
    {
        $stmt = $db->prepare($sql);
        $stmt->execute($insertData);
    // otherwise you just wanna see the query
    } else {
        printf("<pre>$sql\n%s</pre>",print_r($insertData,true));
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM