Jump to content

Insert Multiple forms into mySQL


petenaylor

Recommended Posts

Hi all

 

I am trying to take the data from a form and add into a mySQL table.

 

I have multiple forms on one page that uses a loop:

 

<?php

		$textqty = $showbasket['qty'];
		for ($i = 1; $i <= $textqty ; $i++) { ?>
            
		<form id="texts" name="texts" method="post" action="">
             <input name="mainqty" type="hidden" value="<?php echo $textqty; ?>" />
             <input name="productid" type="hidden" value="<?php echo $showbasket['productid']; ?>" />
             <input name="productqtyid" type="hidden" value="<?php echo $i; ?>" />
              <input name="productsize" type="hidden" value="<?php echo $showbasket['size']; ?>" />
                Text:
                  <input name="text_<?php echo $i; ?>" type="text" value="<?php echo $showtext['text']; ?>" size="35" maxlength="250" />
                Colour:
                <select name="colour">
                  <?php $getcolours = mysql_query(" SELECT * FROM text_colours ORDER BY id ASC");
                   while($showcolours = mysql_fetch_array($getcolours)) { ?>
                  <option value="<?php echo $showcolours['colour']; ?>"><?php echo $showcolours['colour']; ?></option>
                  <?php  } ?>
                </select>
                 No. Characters: 
			<br />
			<?php } ?>
               
               <input name="update" type="submit" id="update" value="Update" />
              </form>

 

This data is then inserted into the mySQL:

 

<?php

if(isset($_POST['update'])) {
$mqty = $_POST['mainqty'];
for ($i = 1; $i <= $mqty ; $i++) {
$productid = $_POST['productid'];
$productqtyid = $_POST['productqtyid'];
$productsize = $_POST['productsize'];
$colour = $_POST['colour'];
$producttext = $_POST['text_$i'];

mysql_query (" INSERT INTO emb_texts SET sessionid = '".$sessionid."', productid = '".$productid."', qtyid = '".$productqtyid."', size = '".$productsize."', colour = '".$colour."', text = '".$producttext."'") or die(mysql_error());
}
}
?>

 

This almost works but it adds the $productqtyid the same very time. I'm not sure if I am going about the the right way?

 

Each form needs to add its own values into the mySQL.

 

Many thanks for your help

Link to comment
https://forums.phpfreaks.com/topic/259210-insert-multiple-forms-into-mysql/
Share on other sites

The <FORM> opening tag is inside your loop; while the closing tag is outside the loop. So either you have multiple forms and all but one are broken; or you have a single form and it is badly broken.

 

If you are trying to have all of the fields submitted as an array in a single form you need to:

 

1) move the form opening tag outside the for loop; and

2) change the field names that should be repeating to be arrays

 

Something along these lines:

<form id="texts" name="texts" method="post" action="">
<?php
$items = count($showbasket);
for ($i = 0; $i < $items; $i++) { 
?>
<input name="mainqty[$i]" type="hidden" value="<?php echo $showbasket[$i]['qty']; ?>" />
<input name="productid[$i]" type="hidden" value="<?php echo $showbasket[$i]['productid']; ?>" />
<input name="productqtyid[$i]" type="hidden" value="<?php echo $i; ?>" />
<input name="productsize[$i]" type="hidden" value="<?php echo $showbasket[$i]['size']; ?>" />

 

Then process them in a loop:

 

foreach($_POST['productqtyid'] as $i) {
$productid = $_POST['productid'][$i];
$productqtyid = $_POST['productqtyid'][$i];
$productsize = $_POST['productsize'][$i];

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.