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";

}
?>
2

2 Answers 2

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).

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.