[thelist] php mysql form problem

Bill Haenel bill at webmarketingworx.com
Thu Aug 29 10:10:00 CDT 2002


> i've a client with a large list of products (some 300+)...
> he's wanting me to create a form where he
> can change all the prices, then submit the data

You might try using product_id as part of the form field to make a specific
association between the form field and the correct product.

Use a do...while to run through each product and write the form fields for
each one, and use array-type form field names to set up for processing after
submission (ex. name="price[id]"):

=============
Form input...
=============
<?
$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');
echo "<form action=\"" . $PHP_SELF . "\" method=\"get\" >";
echo "\n<table cellspacing=\"5\" cellpadding=\"0\">";
do {
	echo "<tr><td>" . stripslashes($product['product_name']) . "</td><td>";
	echo '<input size="20" name="product_price['.$product['product_id'];
	echo ']" value="'.$product['product_price'].'" />';
	echo "</td></tr>";
} while ($product = mysql_fetch_array($result));
echo "<tr><td>";
echo "<input type=\"submit\" value=\"Change\" name=\"task\" class=\"button\"
/></td>";
echo "</tr></table>";
echo "</form>";
?>


Then when processing the data from the form, send it to the DB using the id
number it came with in the first place:

==================
Form processing...
==================
<?
$conn = db_connect();
foreach ($product_price as $id=>$price) {
	$sql = "update cs_test set product_price='$price' where product_id='$id'";
	$update = mysql_query($sql, $conn) or die ('Couldn\'t update the product
price in the database');
}
?>

I haven't tested this specific bit of code for bugs, but I think it should
work for you.

Good luck.

BH




More information about the thelist mailing list