Jump to content

Recommended Posts

I am trying to loop through an array and update a database with a $post variable.  I am pretty sure it has something to do with the way i am referencing the post variable... maybe I am missing a quote or I have to many in there any help would be awesome.

 

if(isset($_POST['submit3'])){

for($i=0;$i<23;$i++){
$sql="UPDATE price2 SET price='".$_POST[$products1[$i]]."' WHERE storeID='".$_POST['storeid']."' AND storeType = '".$_POST['storetype']."'";

	$result = $db->query($sql);

	echo "success";
							}
			}			

Link to comment
https://forums.phpfreaks.com/topic/196260-using-_post-variable-in-update-string/
Share on other sites

There's nothing technically wrong with the posted code, provided that you have an array named $products1 with indexes 0 - 22 and values that match the post index names.

 

It would take some point of reference, such as your form and your definition of the $products1 array to be able to help you.

Here is more info regarding the problem.

 

Array:

$products1 = array("mocha_small", "mocha_medium", "mocha_large", "icemocha_small", "icemocha_medium", "icemocha_large", "lat_cap_small", "lat_cap_medium", "lat_cap_large", "icelat_small", "icelat_medium", "icelat_large", "coffee_small", "coffee_medium", "coffee_large", "icecoffee_small", "icecoffee_medium", "icecoffee_large", "hotchoc_small", "hotchoc_medium", "hotchoc_large");

 

form info:

 <form action="<?=$_SERVER['PHP_SELF']?>" method="POST">
					<input type="hidden" name="storeid" value="<? echo $store_info; ?>">
					<input type="hidden" name="storetype" value="<? echo $type; ?>">
				             <?

						for($i=0;$i<21;$i++){
						 if (($i + 1) % 3 == 0  )
  							 { 
      echo '<div style="float:left;margin-right:20px;"><input type="text" name="'.$products1[$i].'" value="'. $content[$i]['price'].' " size="6"> </div><br/><br/><br/>';
   							 }else{

      echo '<div style="float:left;margin-right:20px;"><input type="text" name="'.$products1[$i].'" value="'. $content[$i]['price'].'" size="6"> </div>';
   								}
						}
						?>
                         <div id="submitbutton"><input type="submit" value="Submit" border="0" name="submit3"></div>
                         </form>

 

the form loops though and shows the value as is from the database and what I am doing is letting the user make changes to the fields and then click submit to update.

 

I echoed my $sql variable and I am not telling which row to update. Thanks for your guys help. I will post if I am still having issues.

 

UPDATE price2 SET price='5.99' WHERE storeID='201' AND storeType = '1'UPDATE price2 SET price='6.99' WHERE storeID='201' AND storeType = '1'UPDATE price2 SET price='1.69' WHERE storeID='201' AND storeType = '1'UPDATE price2 SET price='1.49' WHERE storeID='201' AND storeType = '1'UPDATE price2 SET price='1.59' WHERE storeID='201' AND storeType = '1'UPDATE price2 SET price='1.69' WHERE storeID='201' AND storeType = '1'UPDATE price2 SET price='1.59' WHERE storeID='201' AND storeType = '1'UPDATE price2 SET price='1.69' WHERE storeID='201' AND storeType = '1'UPDATE price2 SET price='1.79' WHERE storeID='201' AND storeType = '1'UPDATE price2 SET price='1.69' WHERE storeID='201' AND storeType = '1'UPDATE price2 SET price='1.79' WHERE storeID='201' AND storeType = '1'UPDATE price2 SET price='1.89' WHERE storeID='201' AND storeType = '1'UPDATE price2 SET price='1.79' WHERE storeID='201' AND storeType = '1'UPDATE price2 SET price='1.89' WHERE storeID='201' AND storeType = '1'UPDATE price2 SET price='1.99' WHERE storeID='201' AND storeType = '1'UPDATE price2 SET price='1.89' WHERE storeID='201' AND storeType = '1'UPDATE price2 SET price='1.99' WHERE storeID='201' AND storeType = '1'UPDATE price2 SET price='2.09' WHERE storeID='201' AND storeType = '1'UPDATE price2 SET price='1.99' WHERE storeID='201' AND storeType = '1'UPDATE price2 SET price='2.09' WHERE storeID='201' AND storeType = '1'UPDATE price2 SET price='2.19' WHERE storeID='201' AND storeType = '1'

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.