[thelist] php mysql form problem

Rich Gray richardgray at onetel.co.uk
Thu Aug 29 06:27:01 CDT 2002


Hi Kris

Add in a hidden form field with the product_id in it then you can associate
the input price with the product id... e.g. below

for ($i=0; $i<$rows; $i++) {
	echo "<tr><td>" . stripslashes($product['product_name'][$i]) . "</td><td>";
	echo "<input size=\"20\" name=\"product_price[]\" value=\""
.$product['product_price'][$i] . "\" />";
	echo "<input type=\"hidden\" name=\"product_id[]\" value=\""
.$product['product_id'][$i] . "\" />";	echo "</td></tr>";
}

Notice I added the [] after the names - this will make PHP create an array
when the form gets submitted.

Then when you process the input ...

for ($i=0; $i<$sizeof($_GET['product_id']); $i++) {
	$sql = "update cs_test set product_price = '".$_GET['product_price'][$i]."'
where product_id = '".$_GET['product_id'][$i]."'";
               $update =  mysql_query($sql, $conn) or die ('Couldn\'t update
the product price in the database');
}

HTH
Rich
-----Original Message-----
From: thelist-admin at lists.evolt.org
[mailto:thelist-admin at lists.evolt.org]On Behalf Of kris burford
Sent: 29 August 2002 11:43
To: thelist at lists.evolt.org
Subject: [thelist] php mysql form problem


hi all,

i've a client with a large list of products (some 300+) that he updates the
price of once every 6-12 months. he's wanting me to create a form where he
can change all the prices, then submit the data (rather than submit each
price change individually) and i'm having difficulty with the mass change...

the fields i've got to play with are product_id, product_name,
product_price and i realise that it should be simple, i just can't see how
i can attribute each item's price with the product id in the form. below is
as far as i've managed...

	$conn = db_connect();
	$sql = "select product_name, product_id, product_price from cs_test order
by product_name";
	$result = mysql_query($sql, $conn) or die ('Couldn\'t select products from
the database');
	$rows = mysql_num_rows($result);
	$product = mysql_fetch_array($result);
	echo "<form action=\"" . $PHP_SELF . "\" method=\"get\" >";
	echo "\n<table cellspacing=\"5\" cellpadding=\"0\">";
	for ($i=0; $i<$rows; $i++) {
		echo "<tr><td>" . stripslashes($product['product_name'][$i]) .
"</td><td>";
		echo "<input size=\"20\" name=\"product_price\" value=\"" .
$product['product_price'][$i] . "\" />";
		echo "</td></tr>";
	}
	echo "<tr><td><input type=\"submit\" value=\"Change\" name=\"task\"
class=\"button\" /></td>";
	echo "</tr></table>";
	echo "</form>";


	$conn = db_connect();
		for ($i=0; $i<$rows; $i++) {
	$sql = "update cs_test set product_price = '$product_price' where
?????????????
	$update = mysql_query($sql, $conn) or die ('Couldn\'t update the product
price in the database');
	}

tia

kris
--
For unsubscribe and other options, including
the Tip Harvester and archive of thelist go to:
http://lists.evolt.org Workers of the Web, evolt !




More information about the thelist mailing list