Jump to content

Update MySQL depending on which form they submit from


avincent

Recommended Posts

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>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.