heyy... i need to knw how to update many values from one submit. problem is i have 10 fields and every text field has same table field value.assume that my 10 fields have 10 quantity values. i need to update quantity field by all 10 fields. like this ; 10 different items quantity should be update by clicking on submit button.(User select 10 different items from each combo boxes and enter quantity for each appropriate text field.)
I NEED is how to write query for this... please Help..

You should have a different name for each field. When you retrieve the values update them using the name. For example, if you have a field with name 'product123' the query should be something like this:

$quantity = $_POST['product123'];
    mysql_query("Update products set amount = $quantity where idproduct = 'product123'");

When you have multiple fields you can do a 'for each' to the $_POST element to update the values according to the name, but remember that when you submit a form the button is sent too so put a name to it.

foreach ($_POST as $key => $val) {
       if($key == "submitName"){
           continue;
       }
       $qry = "update products set amount = $val where idproduct = $key";
       mysql_query($qry);
   }

hey bt here in foreach loop hw do i put variables name>>?? plz help me..

list( $itemId , $itemValue ) = explode("___",$_POST["item1"]);
$sql=mysql_query("SELECT Quantity FROM itemdetails WHERE itemID='$itemId'");
while($row=mysql_fetch_array($sql)){
$answer=$row[0];
}

$var1=trim($_POST['textfield1']);
$updateQuntity=$answer-$var1;

$sql1="UPDATE itemdetails SET Quantity='$updateQuntity' WHERE itemID='$itemId' ";
$result1=mysql_query($sql1);
}

i need to run this code for ten times.. please help me to run this 10 times.. i tried to use for loop. bt still i got so much errors .. plz help

where is for loop? I can't see it in your code!

yeahh... i put code in here with out for loop.. please can u do it for me.. because when i use for loop it get too much errors.please help me. with out for loop this code work nicely.. i dont knw how to use bt i can use $_POST["item$i"] and $_POST["textfield$i"] like this. can u please made this code as working.

In the html part you may have fields with different names, so when they are sent the names are the key, this is the form.html

<form action="handler.php" method="post">
<input type="text" name="product1">
<input type="text" name="product2">
<input type="text" name="product3">
<input type="submit" name="submitButton">

When the form is sent the php code to retrieve them can now work. Run this example to see what happens, this is the handle.php

foreach ($_POST as $key => $val) {
      if($key == "submitButton"){
          continue;
      }
      $qry = "update products set amount = $val where idproduct = '$key'";
      echo $qry;
      #mysql_query($qry);
}

heyy thnxxxzz... itz worky bt i dont need to get all fields in the form.. there are another text fields also. thn how i get only set of text fields that i want.

heyy thnxxxzz... itz worky bt i dont need to get all fields in the form.. there are another text fields also. thn how i get only set of text fields that i want.

Looping in this case is impractical because you will need a lot if if..else if..
Just retrieve them using normal way

$myVar = $_POST['myVariable'];

i really need your help on this one, its for a school project, ive got 3 tables on phpmyadmin (product, stock and collaboration). my webpage shows the product the price, a dropbox to choose the quantity of each product and a submit button.

what im trying to do is reduce stock quantity from the stock table for a product with the quantity chosen from the drop box of that product. this is what ive been able to do so far:

<?php
include_once("mysql/newconn.php");

$result = mysql_query("select * from product");
$num_rows = mysql_num_rows($result);

for($i=0;$i<=$num_rows;$i++)
{   
    $row_product = mysql_fetch_array($result);
?>
<h1><?php     echo $row_product["productNAME"]; ?></h1>
<h2>Description: <?php echo $row_product["productDESC"]."<br/>"; ?></h2>
<h2><br/>
  <img src="<?php echo $row_product["picturePATH"]; ?>" width="151" height="184"/></h2>
<h2>Price: £<?php echo $row_product["productPRICE"]."<br/>";?></h2>
<p><strong>Quantity </strong>
  <select name="quantity" id="quantity">
    <option selected="selected">1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option>
    <option>6</option>
    <option>7</option>
    <option>8</option>
    <option>9</option>
    <option>10</option>
  </select>
</p>
<form name="regForm" method="post" action="" enctype="multipart/form-data">

  <input type="submit" name="formtype" id="formtype" value="Purchase" />    
    </form>
  </p>
  </p>
<?php
}
?>

pls help me, submittion day is fast approaching

It seems that you want the user to be capable of buying many items at once. Then each 'quantity' item should have a unique name that allows you to determine which product to update. So in the for loop each 'select' print could have the product's Id.

<select name="<?php echo $row_product['productId']?>" >

So when you retrieve all those quantity numbers you can use the foreach technique. The $key will be the product's id and the $val will be the quantity selected.

You should add a 0 option by default so you don't force the user to buy everything he sees on the screen. Also remember to omit the submit button which in this case has the name of 'formtype'

hi again,
ive tried what you suggested but it seems im just confusing myself the more. im new to php, can you educate me one the steps to take (pls break down these steps). this is what ive been able to do with your earlier advice:

<?php
if($_POST['submitButton'] == 'submit')


 
 echo $row_product['productID'] ;
 
 
?>
 
<?php
include_once("mysql/newconn.php");

$result = mysql_query("select * from product");
$num_rows = mysql_num_rows($result);

for($i=0;$i<=$num_rows;$i++)
{	
	$row_product = mysql_fetch_array($result);
?>
<h1><?php 	echo $row_product["productNAME"]; ?></h1>
<h2>Description: <?php echo $row_product["productDESC"]."<br/>"; ?></h2>
<h2><br/>
  <img src="<?php echo $row_product["picturePATH"]; ?>" width="151" height="184"/></h2>
<h2>Price: £<?php echo $row_product["productPRICE"]."<br/>";?></h2>
<p><strong>Quantity </strong>
  <select name=
  "<?php 
  echo $row_product['productId']
  ?>"
   id="quantity">
    <option selected="selected">1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option>
    <option>6</option>
    <option>7</option>
    <option>8</option>
    <option>9</option>
    <option>10</option>
  </select>
</p>
<form name="regForm" method="post" action="http://localhost/project/index.php?option=productinfo" enctype="multipart/form-data">
      
  <input type="submit" name="submitButton" id="submitButton1" value="Purchase" />    
</form>
  </p>
  </p>
<?php
}
?>

can you advice on what should be in the productinfo.php because i used the handler.php example you posted earlier and the screen just comes back blank. thank you.

That is because the form tag is opened after the select tag, the select tag is not inside the form, and when you submit the form the select values are not sent.

To solve this you must open the form tag before the select one. I think i was wrong the user can't buy many items at once, right? Every product has its own 'purchase' button.

<form name="regForm" method="post" action="handler.php">
 <select name=
  "<?php 
  echo $row_product['productId']
  ?>"
   id="quantity">
    <option selected="selected">1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option>
    <option>6</option>
    <option>7</option>
    <option>8</option>
    <option>9</option>
    <option>10</option>
  </select>
  <input type="submit" name="submitButton" id="submitButton1" value="Purchase" />    
</form>

The handler will only receive two items:
1. The submit button called 'submitButton'
2. The selected option which won't have a certain name, the name will change according to the item that has been purchased.

In the handler.php do an echo on the $_POST and you'll see those items.

echo $_POST;

So the foreach technique should do the job

thanks, i think im getting there now. now this is what i get when i click the purchase button: "update stock set amount = 3 where productID = '1001_'Array". 3 being the number chosen from the drop box and 1001 the prductID of the product, i dont want this mesage to show. i need the page to show a message when i click the purchase button and then stock table to reduce by the quantity 3.

Well now you have the query, you just need to execute it with mysql_query, but i think that the chosen quantity is not the final amount. You have to take out that quantity from the amount like amount = amount - 3; where 3 is the chosen quantity

ok, this is what i have done

<?php


      foreach ($_POST as $key => $val) 
	  {
   
      if($key == "submitButton")
	    
		{
   
         continue;
   
        }
		
		$a = mysql_query("select stockQUANTITY from stock where productID = '$key'");
		
		$b = $a- $val;
		
		mysql_query ("update stock set stockQUANTITY = $b where productID = '$key'");
      
	 
        $qry = "update stock set amount = $val where productID = '$key'";
	  
	  
      
	  echo "<h1>You have ordered $val items. </h1><br/>";
	    
	  echo "<h1>Thanks for shopping with Hullfate Direct.</h2>";
	  
   
      #mysql_query($qry);
      }
 ?>

this script displays these errors

Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\xampp\htdocs\project\productinfo.php on line 15

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in C:\xampp\htdocs\project\productinfo.php on line 15

Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\xampp\htdocs\project\productinfo.php on line 19

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in C:\xampp\htdocs\project\productinfo.php on line 19
You have ordered 7 items.

Thanks for shopping with Hullfate Direct.

Well as the output stands, you have no access to mysql, are you connecting to the localhost server and selecting the correct database? That should be done before the foreach

i am connected (i suppose),

<?php
$dbc = mysql_connect ('localhost','root','');
if (!$dbc){
    die('Not connected :' . mysql_error());
	}
	
//select database
$db_selected = mysql_select_db("hullfate", $dbc);
if (!$db_selected)
{
	die("cant connect :" . mysql_error());
}
?>

which i even used in the main products script:

<?php
include_once("pages/mysql/newconn.php");

$result = mysql_query("select * from product");
$num_rows = mysql_num_rows($result);

this has been working

or have i messed it up again?

well i was able to sort the connection problem out by using:

mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("hullfate") or die(mysql_error());

but the quantity in the database is still not reducing, thanks for all your help.

When you do this

$a = mysql_query("select stockQUANTITY from stock where productID = '$key'");

The $a has a 'mysql_result' which is a special data type, $a does not contain the current stock. To get the current stock you must use mysql_fetch_array($a); just like when you retrieved certain fields from the product table

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.