avincent Posted January 14, 2010 Share Posted January 14, 2010 I have a PHP page that could display multiple forms depending on how many records correspond to the select made on the previous page. If the results page displays more than one form the user is only able to update the last form regardless if they make changes to the first form and use the update button for that form. I am trying to be able to tell my MySQL database to update records from the form that has the update button the user clicks and only update the data that corresponds to that form and no others. Below find the code being used: <?php session_start(); include("db_connect.php"); ?> <!doctype html public "-//w3c//dtd html 3.2//en"> <html> <head> <title></title> <!-- OF COURSE YOU NEED TO ADAPT NEXT LINE TO YOUR tiny_mce.js PATH --> <script type="text/javascript" src="jscripts/tiny_mce/tiny_mce.js"></script> <script type="text/javascript"> tinyMCE.init({ // General options mode : "textareas", theme : "advanced" }); </script> </head> <body> <h1>Search Results</h1> <br> <hr> <?php if(isset($_POST['submit'])){ $subcat = urldecode($_POST['subcat']); $subcat = mysql_real_escape_string($subcat); if(isset($subcat) and strlen($subcat) > 0){ $sql = "SELECT * FROM `tbl_product` WHERE `product_name` ='{$subcat}'"; $query = mysql_query($sql) or die(mysql_error()); while ($row = mysql_fetch_array($query)) { $product_id= $row['product_id']; $product_sku= $row["product_sku"]; $product_name= urlencode($row["product_name"]); $product_short_description= $row["product_short_description"]; $product_long_description= $row["product_long_description"]; $product_active= $row["product_active"]; echo "<form method='post' action='search-results.php'><table width='700' align='center'><tr><td><h3>" . urldecode($product_name) . "</h3></td></tr>"; echo "<tr><td><label for='product_id'>Product Active: <br><input type='checkbox' name='" . "active" . $product_id . "' value='" . $product_active . "'></td>"; echo "<td><label for='product_id'>Product ID: <br><input type='textbox' name='product_id' value='" . $product_id . "'></td>"; echo "<td><label for='product_id'>Product SKU: <br><input type='textbox' name='product_sku' value='" . $product_sku . "'></td>"; echo "<td><label for='product_id'>Product Name: <br><input type='textbox' name='subcat' value=\"" . urldecode($product_name) . "\"></td></tr>"; echo "<tr><td colspan='4'><textarea cols='60' rows='1' name='product_short_description'>" . $product_short_description . "</textarea></td></tr>"; echo "<tr><td colspan='4'><textarea cols='60' rows='4' name='product_long_description'>" . $product_long_description . "</textarea></td></tr>"; echo "<tr><td><input type='submit' name='delete' value='Delete'></td><td> </td><td><input type='submit' name='update' value='Update'></td></tr>"; echo "<tr><td colspan='4'><br><br><hr></td></tr></table></form>"; } } } ?> <?php if(isset($_POST['update'])){ $subcat = urldecode($_POST['subcat']); $subcat = mysql_real_escape_string($subcat); $product_id = $_POST['product_id']; $product_sku = $_POST['product_sku']; $product_short_description = $_POST['product_short_description']; $product_long_description = $_POST['product_long_description']; $sql = "UPDATE tbl_product SET product_name = '" . $subcat . "' , product_id = '" . $product_id . "' , product_sku = '" . $product_sku . "' , product_short_description = '" . $product_short_description . "' , product_long_description = '" . $product_long_description . "' WHERE product_id = '" . $product_id . "';"; //$sql = "SELECT * FROM `tbl_product` WHERE `product_name` ='{$subcat}'"; $query = mysql_query($sql) or die(mysql_error()); } ?> <?php if(isset($_POST['delete'])){ $subcat = urldecode($_POST['subcat']); $subcat = mysql_real_escape_string($subcat); $product_id = $_POST['product_id']; $product_sku = $_POST['product_sku']; $product_short_description = $_POST['product_short_description']; $product_long_description = $_POST['product_long_description']; $sql = "DELETE FROM tbl_product WHERE product_id = '" . $product_id . "';"; //$sql = "SELECT * FROM `tbl_product` WHERE `product_name` ='{$subcat}'"; $query = mysql_query($sql) or die(mysql_error()); } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/188470-update-mysql-depending-on-which-form-they-submit-from/ Share on other sites More sharing options...
avincent Posted January 14, 2010 Author Share Posted January 14, 2010 I figured it out on my own. Seems that if i give each form a hidden field and unique form name and use the function array_key_exists() it knows which form to process. Quote Link to comment https://forums.phpfreaks.com/topic/188470-update-mysql-depending-on-which-form-they-submit-from/#findComment-995019 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.