blue_cat blue_cat - 1 year ago 30
PHP Question

PHP Update always update empty string

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

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



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