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

  • Home
  • SEARCH
  • 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 8573087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:10:16+00:00 2026-06-11T19:10:16+00:00

I have a form in which user can add more fields, as he want…

  • 0

I have a form in which user can add more fields, as he want…

he can add 1,2,3,…. rows of fields..

the code that I use is here,

<?php
echo '
<form method="post" action="">
   <input type="hidden" name="mod" value="'.$mod.'" />
   <table style="width: 700px">
   <tr>
       <th>Description</th>
       <th>Quantity</th>
       <th>Price</th>
   </tr>';

// Loop to prepare the display of 100 product lines
for ($i=0; $i<100; $i++) {

   if ($text['quantity'][$i] == "") $text['quantity'][$i] = 1;
   if ($text['unit'][$i] == "") $text['unit'][$i] = "0.00";
   // Display only the first line
   if ($nbr_ligne == 0) $nbr_ligne = 1;
   if ($i >= $nbr_ligne) $display = 'style="display:none"';
   echo '
   <tr id="cell'.$i.'" '.$display.'>
       <td>
           <textarea name="text[detail]['.$i.']">'.stripslashes($text['detail'][$i]).'</textarea>
           <br />
           <a href="javascript:void(0)" onclick="javascript:document.getElementById(\'cell'.($i+1).'\').style.display=\'table-row\'; this.style.display=\'none\'">[+]</a>
       </td>
       <td>
           <input name="text[quantity]['.$i.']" id="q'.$i.'" value="" size="4" />
       </td>
       <td>
           <input name="text[unit]['.$i.']" id="u'.$i.'" value="" size="7" /> USD
       </td>
   </tr>';
}

echo '
   </table>
   <input type="submit" name="save" value="Save" />
</form>
';
?>

the fields are adding successfully.

now I want to store the values of these fields in database.

the code I use for this is:

if(isset($_POST['save']))
{
    echo mysql_error($db);


       extract($_POST);
       $insert=mysql_query(" insert into add (description, quantity, price) values ('{$text['detail'][$i]}','{$text['quantity'][$i]}','{$text['unit'][$i]}')") or die("unable to insert"); 
}

but it does not work. plz help me guys. I need it very much.

  • 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-06-11T19:10:17+00:00Added an answer on June 11, 2026 at 7:10 pm

    The issue here is that $i has not been initialized in the script that you are using to save to the database. You will need to loop through from 0 to 100 and BREAK if a number does not exist. You’re also using the reserved keyword add. Use backticks ` on reserved keywords.

    extract($_POST);
    
    foreach(range(0,100) as $i){
        if(isset($text['detail'][$i]) && isset(isset($text['quantity'][$i]) && isset(isset($text['unit'][$i])){
             $insert= mysql_query(" INSERT INTO `add` (`description`, `quantity`, `price`) VALUES ('".$text['detail'][$i]."','".$text['quantity'][$i]."','".$text['unit'][$i]."')") or die(mysql_error());
        }
        else{
            break;
        }
    }
    

    Not too fond of the way you’re looping on inserts, but the above piece of code should work, even though it’s clearly not the best solution. A better way would be to create one singular INSERT query that inserts multiple rows:

    INSERT INTO example (name, age)
    VALUES   ('John', 24),   ('Katrina', 21),   ('Michael', 22),   ('Hui Ling', 25),   ('Catherine', 29)
    

    If you must loop on separate queries, you should use prepared statements for performance reasons.

    Other issues:

    1. You’re open to SQL injection.
    2. You’re using the mysql functions, which are no longer recommended. Use PDO or mysqli instead.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form into which the user can add more fields to fill
I have a HTML form which allows a user to add rows ad hoc
I have a jsp form validate.jsp which contains 2 text fields where user enters
I have a user control on a form that can spawn several non-modal child
I have a form, in which I can set various client fields like client
So I have two form input fields which are mutually exclusive to each other(User
I have a form which have many check boxes in it. User will check
I have created a PHP form which requires the user to select a postcode
I have an HTML form which, when submitted by the user, will call a
I have a form with a textfield element which when a user starts typing

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.