I am trying to insert multiple rows using foreach, with no luck. I am able to insert the rows i want, however all the rows have the same value (that of the last row). This is the script i'm using:

foreach($_POST as $key=>$val) {

$contaminant = $_POST['contaminant'];
//$gac_value   = $_POST['gac_value'];
$ms_conc0 = $_POST['ms_conc0']; 
$ms_conc1 = $_POST['ms_conc1'];
$ms_conc2 = $_POST['ms_conc2'];
$ms_conc3 = $_POST['ms_conc3'];
$ms_conc4 = $_POST['ms_conc4']; 

$query = "INSERT INTO qgra (id, contaminant, gac_value, ms_conc0, ms_conc1, ms_conc2, ms_conc3, ms_conc4) 
            VALUES ('','$contaminant', '$gac_value', '$ms_conc0', '$ms_conc1', '$ms_conc2', '$ms_conc3', '$ms_conc4') "; 
                      
$result = mysql_query($query) or  die(mysql_error());  }

echo "<br><br> Worked?! ";

Tried using foreach($_POST as $key=>$val) { } as well but was that wayyyy off.

Any ideas where im going wrong?

Thanks

Recommended Answers

All 8 Replies

We need at least some HTML to see what you are trying to loop through. I think $_POST will fix your problem, if your HTML is correct.

Oh yeah -- sorry. Here is the form:

<form name="gac" id="gac" action="action.php" method="POST" > 

<table class="qra"> 
<tr>
<td class="row2" >&nbsp;</td>
<?php

$query = "SELECT * FROM sample ORDER BY id ASC LIMIT 5"; 
$result = mysql_query($query) or die(mysql_error());        
    
while ($row = mysql_fetch_array($result)) {
extract($row);  

echo "<td class=\"row2\" ><label> <span> $sample_name</span> </label></td> " ; 

} ?> 

</tr>

<tr class="row3">
        <td>

     <?php 
    $query3 = "SELECT contaminant, gac_value FROM gac_ea WHERE land_use = 'Allotment' ORDER BY id";
    $result3 = mysql_query($query3) or die(mysql_error());   
    echo "<select name=\"contaminant\" id=\"contaminant\" value=''></option>";
    echo "<option>Select </option>";
    while($row=mysql_fetch_array($result3)){     
        echo "<option value=$row[contaminant]> $row[contaminant] </option>"; } // End contaminant loop 
        echo "</select>"; ?>
    </td>

<td class="row3"><input type="text3" name="ms_conc0" id="ms_conc0" /></td>
<td class="row3"><input type="text3" name="ms_conc1" id="ms_conc1" /></td>
<td class="row3"><input type="text3" name="ms_conc2" id="ms_conc2" /></td>
<td class="row3"><input type="text3" name="ms_conc3" id="ms_conc3" /></td>
<td class="row3"><input type="text3" name="ms_conc4" id="ms_conc4" /></td>
</tr> 

<tr class="row3">
        <td>

     <?php 
    $query3 = "SELECT contaminant, gac_value FROM gac_ea WHERE land_use = 'Allotment' ORDER BY id";
    $result3 = mysql_query($query3) or die(mysql_error());   
    echo "<select name=\"contaminant\" id=\"contaminant\" value=''></option>";
    echo "<option>Select </option>";
    while($row=mysql_fetch_array($result3)){     
        echo "<option value=$row[contaminant]> $row[contaminant] </option>"; } // End contaminant loop 
        echo "</select>"; ?>
    </td>

<td class="row3"><input type="text3" name="ms_conc0" id="ms_conc0" /></td>
<td class="row3"><input type="text3" name="ms_conc1" id="ms_conc1" /></td>
<td class="row3"><input type="text3" name="ms_conc2" id="ms_conc2" /></td>
<td class="row3"><input type="text3" name="ms_conc3" id="ms_conc3" /></td>
<td class="row3"><input type="text3" name="ms_conc4" id="ms_conc4" /></td>
</tr>  

<tr class="row3">
        <td>

     <?php 
    $query3 = "SELECT contaminant, gac_value FROM gac_ea WHERE land_use = 'Allotment' ORDER BY id";
    $result3 = mysql_query($query3) or die(mysql_error());   
    echo "<select name=\"contaminant\" id=\"contaminant\" value=''></option>";
    echo "<option>Select </option>";
    while($row=mysql_fetch_array($result3)){      
        echo "<option value=$row[contaminant]> $row[contaminant] </option>"; } // End contaminant loop 
        echo "</select>"; ?>
    </td>

<td class="row3"><input type="text3" name="ms_conc0" id="ms_conc0" /></td>
<td class="row3"><input type="text3" name="ms_conc1" id="ms_conc1" /></td>
<td class="row3"><input type="text3" name="ms_conc2" id="ms_conc2" /></td>
<td class="row3"><input type="text3" name="ms_conc3" id="ms_conc3" /></td>
<td class="row3"><input type="text3" name="ms_conc4" id="ms_conc4" /></td>
</tr> 

<tr>
<td colspan="6" class="button" >
<input type="reset" value="Clear input"  />
<input type="submit" name="submit" value="Save" /></td>
</tr>
</table> 

</form>

Anyone there? :)

You're still referring to the POST object while you're going through your loop. It's still going to be pointing at the same field. You need to use the variable you defined with AS in the foreach condition.

For example:

foreach ($_POST as $val) {
   $otherVar = $val['inputFieldName'];
   $anotherVar = $val['anotherInputFieldName'];
}

The $val variable defined there is the one that progresses through the different indexes of $_POST in this case.

That worked perfectly. Thank you. It saves the values i want in the database.

Now im trying to display the values of the selected options in a text box, but no luck - again! This is what i tried:

<?php 
    $query3 = "SELECT contaminant, gac_value FROM gac_ea WHERE land_use = 'Allotment' ORDER BY id";
    $result3 = mysql_query($query3) or die(mysql_error());   
    echo "<select name=\"contaminant[]\" id=\"contaminant\" value=''></option>";
    echo "<option>Select </option>";
    while($row=mysql_fetch_array($result3)){   
        echo "<option value=$row[contaminant]> $row[contaminant] </option>"; } 
        echo "</select></td>"; 
        echo "<td class=\"row3\">";
        echo "<input type=\"text3\" name=\"gac_value[]\" id=\"gav_value\" value=\"$row[gac_value]\" disabled=\"disabled\" /> </td>";
 // End contaminant loop 
        
    ?>

Thanks in advance.

That's just a typo, you don't have quotes around your gac_value in the $row object.

//supposed to be
$row['gac_value'];

//instead of
$row[gac_value];

Thanks Galapogos. Tried that, but that didn't work either :s

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.