Skip navigation links

MySQL Forums :: PHP :: insert multiple rows into mysql with foreach loop


Advanced Search

insert multiple rows into mysql with foreach loop
Posted by: Ryan Bauer ()
Date: December 31, 2009 12:27AM

hello everybody thank you for taking the time to review this and helping me out once again...

My problem is simple I am trying to upload multiple rows in one table from a form.

First the user chooses from a menu/list drop-down thow many amenities they would like to add to a property. the script will then loop and create the amount of amenities they would like to add.

The form itself has three variables propID, aments, and amount. the propID is the same for every row in the form.


I will list the insert script with a foreach loop below first


//check to see if form has been filled out

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {

//if form was submitted then insert each row into database

FOREACH($_POST['amount'] as $row=>$Amount)
{
$Amount=count($_POST['amount']);
$ament=mysql_real_escape_string($_POST['ament'][$row]);
$propID=mysql_real_escape_string($_POST['propID'][$row]);

//if a row is null skip it and do not add to database

if (empty($ament)) {}else {

$insertSQL = sprintf("INSERT INTO aments (propID, ament) VALUES('".$propID."','".$ament."')");

}}


//////////////////////////////////////////////////////////////////////////////////
now I will list the form and html


<form action="adminSingle.php?pageID=newAments&amp;furnishedapartments=<?php echo $row_singleProp['furnishedapartments']; ?>"><table width="450" border="0" cellspacing="0" cellpadding="5">






<tr align="left">
<th width="149" scope="col">add more options</th>
<th width="107" scope="col"><label>
<select name="counter" id="counter">
<option value="1" selected="selected">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
</select>
</label></th>
<th width="164" scope="col"><input name="GO" type="submit" value="GO" /></th>
</tr>




</table></form>

<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
<table align="left">
<?php
$i = 0;
do {?>

<tr valign="baseline">
<td align="right" valign="middle" nowrap="nowrap">Feature:</td>
<td><textarea name="ament[]" cols="40" rows="3"></textarea>
<input type="hidden" name="propID[]" value="<? echo $_GET['furnishedapartments'];?>" />
<input type="hidden" name="amount[]" value="" /></td>
</tr>
<?
$i++;
} while ($i <= $moreRows);
?>

<tr valign="baseline">
<td nowrap="nowrap" align="right">&nbsp;</td>
<td align="right"><input type="submit" value="submit" /></td>
</tr>
</table>

<input type="hidden" name="MM_insert" value="form1" />
</form>



/////////////////////////////////////////////////////////////////////////////



My problem is that the script only inserts the last text area field
and does not insert any of the others. also if I leave the the last textfield blank then it enters in a null value.

Can anybody help out there I think I have spent the past 7 hours working on this simple problem.


Thank you
Ryan

Options: ReplyQuote


Subject Written By Posted
insert multiple rows into mysql with foreach loop Ryan Bauer 12/31/2009 12:27AM
Re: insert multiple rows into mysql with foreach loop Rick James 01/01/2010 02:57AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.