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 7043321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:16:30+00:00 2026-05-28T02:16:30+00:00

I have a form that submits steps and ingredients for recipes that I am

  • 0

I have a form that submits steps and ingredients for recipes that I am practicing with for another project. I have set the form up to submit the data in an array, but I can’t get the PHP code correct to insert the data into the database. I have pasted the form layout here. The form comes up as part of another PHP page that is called when the user enters a recipe name to add to the database. I would like to have 10 separate step entries on this form if I can figure out how to insert them correctly into the database.

<form action="add_recipe2.php" method="post">
    <fieldset>
        <legend>Add a Recipe</legend>
        <table>
            <tr>
                <td>Recipe Name:</td>
                <td><input type="text" name="recipename" value="$recipename"></td>
           </tr>
            <tr>
                <td>Step:</td>
                <td><input type="text" name="recipe[0][step]" placeholder="1"></td>
                <td>Ingredients:</td>
                <td><input type="text" name="recipe[0][ingredients]" placeholder="Ingredients"></td>
            </tr>
            <tr>
                <td>Step:</td>
                <td><input type="text" name="recipe[1][step]" placeholder="2"></td>
                <td>Ingredients:</td>
                <td><input type="text" name="recipe[1][ingredients]" placeholder="Ingredients"></td>
            </tr>
            <tr>
                <td>Step:</td>
                <td><input type="text" name="recipe[2][step]" placeholder="3"></td>
                <td>Ingredients:</td>
                <td><input type="text" name="recipe[2][ingredients]" placeholder="Ingredients"></td>
            </tr>

        </table>
        <button type="submit">Add a Recipe</button>
        <button type="reset">Reset</button>

    </fieldset>
</form>

This is the PHP that enters the data into the database. The problem is when I only add two records to the database the last record still inserts, but it inserts a blank line. I need a way to only add the data that is being passed from the form even if it is only one line. I have researched this a long time and this represents one of the answers that I found. However, it still does not stop inserting into the database when there is no more data from the form.

$recipename = $_REQUEST["recipename"];

$conn = mysql_connect("localhost","user","password") or die(mysql_error());
mysql_select_db("test");

foreach($_POST['recipe'] as $recipe) { 

// Add to database
$sql1 = "INSERT INTO `recipes` (recipe, step, ingredients) VALUES   ('".$_POST['recipename']."', '".$recipe['step']."', '".$recipe['ingredients']."')";
mysql_query($sql1, $conn) or die(mysql_error());
 } //end foreach

I just can’t figure this out. I need help. I suspect that I have to have a way to tell how many records I am actually sending if it is not the number of form entries that exist.

  • 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-28T02:16:30+00:00Added an answer on May 28, 2026 at 2:16 am

    You will need to test if values in the array component are filled in before querying them. Also, you MUST escape all the insert values against SQL injection with mysql_real_escape_string():

    $recipename = mysql_real_escape_string($_POST['recipename']);
    
    foreach($_POST['recipe'] as $recipe) { 
      // Only insert if step is non-empty.
      if (!empty($recipe['step']) {
        // Add to database
    
        // Escape against SQL injection
        $recipe['step'] = mysql_real_escape_string($recipe['step'];
        $recipe['ingredients'] = mysql_real_escape_string($recipe['ingredients'];
    
        $sql1 = "INSERT INTO `recipes` (recipe, step, ingredients) VALUES   ('".$recipename."', '".$recipe['step']."', '".$recipe['ingredients']."')";
        mysql_query($sql1, $conn) or die(mysql_error());
      }
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a form that submits data to a database, i have a function
I have a form that the user submits and returns a result, but it
i have a PHP contact form that submits data, and an email...: <?php $dbh=mysql_connect
I have a form on one page that submits to another page. There, it
I have a form that submits to a third-party service that runs some validation.
I have a form that submits a search for blogs on my site via
I have a form that auto submits and updates the same page. I have
I have a js and html form that submits through php. Once submitted, the
I have a web form that collects information and submits it to a cgi
I have a search form on my site that submits the url in the

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.