Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7619975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:49:31+00:00 2026-05-31T03:49:31+00:00

Can someone please explain to me how I can import the array data I

  • 0

Can someone please explain to me how I can import the array data I am outputting into rows on my database.

HTML

<form id="AddRecipeForm" method="post" action="includes/add-recipe.php" class="form-inline">    
    <input type="text" name="recipe[ingredient][1]" class="input-large" placeholder="Title 1"><input type="text" name="recipe[quantity][1]" class="input-large" placeholder="Quantity 1"><br /><br />   
    <input type="text" name="recipe[ingredient][2]" class="input-large" placeholder="Title 2"><input type="text" name="recipe[quantity][2]" class="input-large" placeholder="Quantity 2"><br /><br />   
    <input type="text" name="recipe[ingredient][3]" class="input-large" placeholder="Title 3"><input type="text" name="recipe[quantity][3]" class="input-large" placeholder="Quantity 3"><br /><br />
    <button type="submit" class="btn">Add Recipe</button>
</form>

This is passed to a php form:

foreach($_POST['recipe'] as $key=>$value)
    {   

    }

print_r($_POST);

and outputs the following array:

Array ( 
    [recipe] => Array ( 
        [ingredient] => Array ( 
            [1] => eggs 
            [2] => milk 
            [3] => flour
        ) [quantity] => Array ( 
            [1] => 12 
            [2] => 13 
            [3] => 14 
        ) 
    ) 
)

I need to import each of the individual ingredients and quantities to a new row in my database table. I am using PDO to connect to my database but I am unsure how I can insert the data from the array into the rows on my database.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T03:49:33+00:00Added an answer on May 31, 2026 at 3:49 am

    Well i would stucture my form a bit differently so you get ingerdients assemebled as their own array, but with the stcuture you have:

    $db = new PDO($dsn, $user, $pass);
    
    $stmt = $db->prepare('INSERT INTO ingredient (name, quantity) VALUES (?,?)');
    
    // youll want to verify that both arrays have the same number of elements before doing this
    // as part of your validation 
    $ingredients = array_combine($_POST['recipe']['ingredient'], $_POST['recipe']['quantity']);
    
    $errors = array();
    
    foreach($ingredients as $name => $quantity)
    {
        try {
           $stmt->execute(array($name, $quantity));
        } catch (PDOException $e) {
           $errors[] = array(
              'message' => "Could not insert \"$name\", \"$quantity\".",
              'error' => $e
        }    
    }
    
    if(!empty($errors)) {
       //do something?
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone please explain to me the static HashMap#hash(int) method? What's the justification behind
Can someone please explain to me why the padTo method of ArrayBuffer doesn't work
Can someone please explain why the following doesn't work works: function displayResults(data) { $(#odNextPage).click(function()
Can someone please explain how to obtain this result from the following array? Here
Can someone please explain me what's the difference between Swing and AWT? Are there
Can someone please explain why int (0.4 * 10.0) is 4 yet int ((2.4
Can someone please explain why this program outputs 0x00000004? class AndAssignment { static void
Can someone please explain what the & does in the following: class TEST {
Can someone please explain to me the difference between the AppSettings and ApplicationSettings sections
Can someone please explain how to understand a logcat from an android force close.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.