0

let me explain the code I'm getting data from the db and display the data in text input then when the user make changes I get the user input and update the table however when it update it update to empty data

<?php
echo "<form action='' method='get'>";

echo "<td class='data'><input type='product_name' id='product_name' value=" . $row['product_name'] . "> </td>";
echo "<td class='data'><input type='products_price' id='products_price' value=" . $row['products_price'] . "> </td>";
echo "<td class='data'><input type='products_desc' id='products_desc' value=" . $row['products_desc'] . "> </td>";

echo "</form>";

echo "</tr>";
echo "</table>";
echo "<form action='' method='POST'>
                 <input name='update' type='submit' value='Update' style='margin-left: 720px'>
                </form>";
$product_name = isset($_GET['product_name']) ? $_GET['product_name'] : '';

$products_desc = isset($_GET['products_desc']) ? $_GET['products_desc'] : '';

$products_price = isset($_GET['products_price']) ? $_GET['products_price'] : '';
if (isset($_POST["update"])) {




    $sql1 = "UPDATE tbl_products SET products_desc='" . $products_desc . "',product_name='" . $product_name . "',products_price='" . $products_price . "' WHERE products_id='" . $product_id . "'";

    mysqli_query($conn, $sql1) or die(mysqli_error($conn));
    echo "yess";

}
?>
blue_cat
  • 83
  • 7
  • you've explained what you're doing but you didn't tell us what's wrong or what's not working or if there are any errors......etc. etc. etc. – Funk Forty Niner May 19 '16 at 18:04
  • possible duplicate of [PHP: “Notice: Undefined variable” and “Notice: Undefined index”](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Funk Forty Niner May 19 '16 at 18:08

2 Answers2

1

Your input elements have no name attribute, so things like $_GET['product_name'] will always be empty. The name attribute is the key in the key/value pair sent to the server.

Additionally, the type attributes are all broken. (Though I suspect the browser is automatically "correcting" that by defaulting to a text input.)

Add the name attribute (and fix type):

<input type='text' name='product_name' id='product_name' ...

Additionally, you have two forms. So when you click your button, that form doesn't submit any of the inputs. Because they're in a different form.

Put them all into the same form, and decide whether you want to use GET or POST (since your server-side code is going to need to know that).

David
  • 208,112
  • 36
  • 198
  • 279
0

You defined your data form in one form tag and submit form in the another form tag:

<form action='' method='POST'>
     <input name='update' type='submit' value='Update' style='margin-left: 720px'>
</form>

echo "<form action='' method='get'>";

echo "<td class='data'><input type='product_name' id='product_name' value=" . $row['product_name'] . "> </td>";
echo "<td class='data'><input type='products_price' id='products_price' value=" . $row['products_price'] . "> </td>";
echo "<td class='data'><input type='products_desc' id='products_desc' value=" . $row['products_desc'] . "> </td>";

echo "</form>";

When you submit your first form but your data is on another form

this is what is wrong

Majid Abbasi
  • 1,531
  • 3
  • 12
  • 22