Re: updating values in a while statement

From: Date: Tue, 03 Apr 2001 04:53:36 +0000
Subject: Re: updating values in a while statement
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-46760@lists.php.net to get a copy of this message
G'Day, You need to have an auto_incrementing field in your database. Then all lines that are displayed need to be in the form of fieldname[] so that PHP builds an array. Then you need a hidden field to represent the unique record number to be updated. Here is some code that I use to print out a list of products by category, and allow me to do mass updates on any of the information present, except the unique id. It then goes through a while loop to UPDATE TABLE for each record. Code follows: <!-- START CODE --> <html><head><title>Product Admin Page</title></head> <body BGCOLOR="white"> <!-- Product Administration page --> <? // FUNCTION DEF function showitems($prod,$cat) { // show all items in the product database, optionally sorted // selected by category or product id including '%' wildcard $mylink = mysql_connect(); mysql_select_db('printcart',$mylink); // select * from product where category like 'postcard' and prodid like '%' ($prod == '') ? $prod = '%' : $prod; ($cat == '') ? $cat = '%' : $cat; $query = "SELECT * FROM product WHERE category LIKE '$cat' AND prodid LIKE '$prod' order by category, prodid"; $myresult = mysql_query($query,$mylink); // build table and show all items in form fields print ' <form name="showitems" action="prodadmin.php?update" method="post"> <table bgcolor="#EEEEEE" border="1"><tr> <th>ProdID</th><th>Product Name</th><th>Product Description</th> <th>Unit</th><th>Price</th><th>Minimum Order</th><th>Packaged</th><th>Category</th></tr> '; while($row = mysql_fetch_array($myresult)) { print "<tr><td><input type=\"hidden\" name=\"id[]\" value=\"$row[prodid]\" > <font size=\"2\">$row[prodid]</font></t d>\n"; print "<td align=\"center\"><font size=\"2\"><input type=\"text\" name=\"name[]\" value=\"$row[prodname]\" ></font>< /td>\n"; print "<td align=\"center\"><font size=\"2\"><input type=\"text\" name=\"desc[]\" value=\"$row[proddesc]\" ></font>< /td>\n"; print "<td align=\"center\"><font size=\"2\"><input type=\"text\" name=\"unit[]\" value=\"$row[unit]\" size=\"6\"></ font></td>\n"; print "<td align=\"center\"><font size=\"2\"><input type=\"text\" name=\"price[]\" value=\"$row[price]\" size=\"6\"> </font></td>\n"; print "<td align=\"center\"><font size=\"2\"><input type=\"text\" name=\"minorder[]\" value=\"$row[minorder]\" size= \"5\"></font></td>\n"; print "<td align=\"center\"><font size=\"2\"><input type=\"text\" name=\"packaged[]\" value=\"$row[packaged]\" size= \"5\"></font></td>\n"; print "<td><font size=\"2\"><input type=\"text\" name=\"category[]\" value=\"$row[category]\" ></font></td></tr>\n"; } print '</table>'; print '<input type="submit" name="update" value="Update">'; print '<input type="hidden" name="cat" value="' . $cat . '">'; print '<input type="hidden" name="prod" value="' . $prod . '">'; print '</form>'; } // FUNCTION function updateproducts() { global $name,$id,$unit,$price,$category,$desc,$minorder,$packaged; $mylink = mysql_connect(); mysql_select_db('printcart', $mylink); $numrows = count($id); for ($i = 0; $i < $numrows; ++$i) { if($desc[$i] == '' ) { $desc[$i] = $name[$i]; } $line = "UPDATE product SET prodname='${name[$i]}',"; $line .= "proddesc='${desc[$i]}',"; $line .= "unit='${unit[$i]}',"; $line .= "price='${price[$i]}',"; $line .= "category='${category[$i]}', "; $line .= "minorder='${minorder[$i]}', "; $line .= "packaged='${packaged[$i]}' "; $line .= "WHERE prodid='${id[$i]}'"; $myresult = mysql_query($line,$mylink); print mysql_error($mylink); } } ?> <form NAME="filter" ACTION="prodadmin.php"> Product id is: <input TYPE="text" NAME="prod" VALUE="<? print $prod ?>"><br> Category is: <!-- <input type="text" name="cat" value="<? print $cat ?>">--> <select NAME="cat"> <? $catq = "select category,c.name from product,category as c where category=c.ckey group by c.name order by category"; $link = mysql_connect(); mysql_select_db('printcart',$link); $catsr = mysql_query($catq,$link); while(list($catop,$catname) = mysql_fetch_array($catsr)) { if($catop == $cat) { $select = "selected"; } else { $select = ""; } print "<option value=\"$catop\" $select>$catname</option>\n"; } ?> <option VALUE="%">%wildcard</option> </select> <br> <font SIZE="-1">(you may use the '%' as a wildcard for matching a portion of the code or category)</font><br><br> Too display all items, enter % into both fields. <br> <br> <input TYPE=submit VALUE="Get Matches"> </form> <? // Begin MAIN if(isset($update)) { updateproducts(); showitems($prod,$cat); } elseif(isset($prod) OR isset($cat)) { showitems($prod,$cat); } ?> </body></html> <!-- END CODE --> You should be able to get the gist of it ;) On 4/2/01 9:42 PM, "Peter Houchin" <peterh@vfsa.com.au> wrote: > Hiya i've got a script that basically draws values from a db and will display > them, as i have multiple records in there and want to display them all i'm > using a > while ( ($myrow = mysql_fetch_array($result) ) ) statement this is all good > as far as it will display them all how ever when i to be able to update the > fields as well as display them .. sounds easy to do i know .. but for some > reason my id value gets sent to the server as "id=++" can some one offer any > suggestions as to why this is as when i view the source it lists the vaules as > 1 2 3 etc. > > Thanks > Peter Houchin > peterh@sunrentals.com.au > ========================================================= > _____ __ /\ > /_/_/_\ / |_/ \ > /_/_/_ __ __ __ __ / \ > \_/_/_\ /_/ /_/ /_/ /_/ \ _ / > ___\_\_\/ /_/_/_/ /_//\/_/ \_/ \/\_/ > \_//_/_/ /_/_/_/ /_/ \/_/ v > ________ ________________________________________ > /_/_/_/_/ /_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ > /_/_ _/_/ ______ __ __ /_/ ____ __ ______ > /_/_/_/_/ /_/_/_/ /_/ /_/ /_/ /_/\_\ /_/ /_/_/_/ > /_/ \_\ /_/ _/ /_//\/_/ /_/ /_/__\_\ /_/___ _\_\_\ > /_/ \_\/_/_/_/ /_/ \/_/ /_/ /_/ \_\/_/_/_//_/_/_/ > ========================================================= > Telephone : (03) 9329 1455 Facsimile : (03) 9329 6755 > ************* We rent the dot in .COM! ************** > >

« previous php.general (#46760) next »