I have a form, whose fields can be increased by click on ‘[+]’ sign.
the code that I use for the form is
Description
Quantity
Price
‘;
// 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="'.stripslashes($text['quantity'][$i]).'" size="4" />
</td>
<td>
<input name="text[unit]['.$i.']" id="u'.$i.'" value="'.stripslashes($text['unit'][$i]).'" size="7" /> USD
</td>
</tr>';
}
echo '
</table>
<input type="submit" name="save" value="Save" />
</form>
';
if(isset($_POST['save']))
{
echo $text['quantity']['.$i.'];
mysql_connect("localhost","root","");
mysql_select_db("rvt") or die("unable to select db");
extract($_POST);
$insert=mysql_query("insert into add(description, quantity, price) values('$text[detail][".$i."]','$text[quantity][".$i."]','$text[unit][".$i."]')");
if($insert)
{
echo "hi";
}
}
?>
I want to store the data to database. It not works.
Plz help me in storing data in database.
Your query will not work due to a PHP parser “bug”. It is not a greedy parser, so something like:
will actually produce as output
and not whatever value is stored at index 2 of the sub-array at index 1.
because PHP’s parser stops considering array references after the first set of
[]. You need to use{}notation:note the position of the
{}, and the additional'around the detail key.As well, please do not use
extract(), especially on _GET/_POST. You’re essentially reproducing the very horribly nastily ugly bad days of when register_globals was turned on in PHP. It’s a security hole just waiting to get exploited.