Murtaza66 Posted March 4, 2015 Share Posted March 4, 2015 Hello everyone, I'm working on a project for school.It's an invoice program but I'm stuck, can anyone help mefigure out how to get the values out of multiple textboxes /fieldsand insert them in to my database. This is what it looks like.The user can select the amount of textfields that will be inserted in to the database. <div class="input_fields_wrap"> <button id="remove_field">x</button> <?php $query = mysql_query("SELECT * FROM producten"); ?> <select name="Producten[]" id="Producten"> <div><?php while($row = mysql_fetch_array($query)){ echo "<option>" . $row['Producten'] . "</option>"; }?> </div><input type="text" name="ProdOms[]"> <input type="text" size="3" name="Aantal[]"> <input type="text" size="3" name="Prijs[]"> <a href="javascript:void(0)" onclick="toggle_visibility('popup-box1');"><img src="../img/icons/info.png" height="16" width="16"></a> </select> </div> this is my html form I have 15 of these how to insert the values in the database Quote Link to comment https://forums.phpfreaks.com/topic/295072-how-to-insert-a-multiple-textbox-with-the-same-name/ Share on other sites More sharing options...
rwhite35 Posted March 4, 2015 Share Posted March 4, 2015 First, use mysqli instead of mysql for your database functionality. Next write a simple script to output your data on post. I think you'll see post data differently then you're expecting. /* form.php script */ <form action="seepostdata.php" method="post"> ... form fields, etc Then in your seepostdata.php script: /* output post array to viewer */ echo "<pre>"; print_r($_POST); echo "</pre>"; This should output something like Array ( [Producten] => array ( [0]=>"first option selection", [1]=>"next option selection", [2]=>"etc etc" ) [ProdOms] => array ( [0]=>"first string", [1]=>"another string", [2]=>"etc etc" ) ) Then copy paste that output to a text file and save it for reference. It is very difficult to visualize a multidimensional array. Which is being created when you name your fields like name="Poducten[]". Good luck. Quote Link to comment https://forums.phpfreaks.com/topic/295072-how-to-insert-a-multiple-textbox-with-the-same-name/#findComment-1507481 Share on other sites More sharing options...
rwhite35 Posted March 4, 2015 Share Posted March 4, 2015 (edited) To answer your question more directly. Here is one way to access your post variables. $producten = $_POST['Producten'][0]; //would be the first producton select item Edited March 4, 2015 by rwhite35 Quote Link to comment https://forums.phpfreaks.com/topic/295072-how-to-insert-a-multiple-textbox-with-the-same-name/#findComment-1507482 Share on other sites More sharing options...
Murtaza66 Posted March 4, 2015 Author Share Posted March 4, 2015 thanks rwhite35 but what if the user fills 2 form how to get the value of them I thought using a foreach statement like this $row_data = array(); foreach($_POST['Producten'] as $row=>$data){ $Producten = mysql_real_escape_string($data); $ProdOms = mysql_real_escape_string($_POST['ProdOms'][$row]); $Aantal = mysql_real_escape_string($_POST['Aantal'][$row]); $Prijs = mysql_real_escape_string($_POST['Prijs'][$row]); $row_data[] = "('$Producten','$ProdOms','$Aantal','$Prijs')"; $sql = mysql_query("INSERT into hulptabel (ID, FactuurNR, Product,ProdOms,Aantal, Prijs) VALUES ('$ID','$FactuurNR' ,'$Producten','$ProdOms','$Aantal','$Prijs')"); } But when I wants to insert more then 2 rows it will insert only 1 row Quote Link to comment https://forums.phpfreaks.com/topic/295072-how-to-insert-a-multiple-textbox-with-the-same-name/#findComment-1507483 Share on other sites More sharing options...
rwhite35 Posted March 4, 2015 Share Posted March 4, 2015 Check out this post here. You're looking to batch all 15 forms in to one transaction. Also read ConNix comment on prepared statements. Prepared statements will automatically optimize your POST data and it is more secure then what you have. http://forums.phpfreaks.com/topic/295060-inserting-list-in-sql-function/?p=1507436 Quote Link to comment https://forums.phpfreaks.com/topic/295072-how-to-insert-a-multiple-textbox-with-the-same-name/#findComment-1507484 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.