Khazz T. Khazz T. - 5 months ago 13
SQL Question

Why html input submit not assigning $_POST array value

Please check on the last part of code where input type="submit" name="save_changes_submit". This form not assigning in $_POST. Whats wrong I am doing here? Is there any html mistake I am doing here? How can I achieve $_POST value form this submit?

echo '<form action="" method="post">ID Number:<input type="number" name="id_number" value="number"><br><br><input type="submit" value="Search Product" name="id_number_submit">';

if (isset($_POST['id_number_submit'])) {
$id_number = $_POST['id_number'];

$q = mysqli_query($conn, "SELECT * FROM product WHERE id='$id_number'");

$row = mysqli_fetch_array($q);

if (empty($row)) {
echo "Error: Invalid product id";
}else{

$product_name = $row['product_name'];
$product_des = $row['product_des'];
$product_price = $row['product_price'];


echo '<br><br><input type="number" name="product_price" value="'.$product_price.'">';
echo '<br><br><input type="text" name="product_name" value="'.$product_name.'">';
echo '<br><br><textarea rows="8" cols="100" name="product_des">'.$product_des.'</textarea><br><input type="submit" value="Save Changes" name="save_changes_submit">';

if (isset($_POST['save_changes_submit'])) {
echo $c_product_price = $_POST['product_price'];
echo $c_product_name = $_POST['product_name'];
echo $c_product_des = $_POST['product_des'];
}
}
}

echo '</form>';

Answer

The if condition if(isset($_POST['save_changes_submit'])) was inside if(isset($_POST['id_number_submit'])). And that was the problem. Here it is.

<?php
echo '<form action="" method="post">ID Number:<input type="number" name="id_number" value="number"><br><br><input type="submit" value="Search Product" name="id_number_submit">';

    if(isset($_POST['id_number_submit'])) {
        $id_number = $_POST['id_number'];

        $q = mysqli_query($conn, "SELECT * FROM product WHERE id='$id_number'");

        $row = mysqli_fetch_array($q);

        if (empty($row)){
            echo "Error: Invalid product id";
        }else{
            $product_name = $row['product_name'];
            $product_des = $row['product_des'];
            $product_price = $row['product_price'];


        echo '<br><br><input type="number" name="product_price" value="'.$product_price.'">';
        echo '<br><br><input type="text" name="product_name" value="'.$product_name.'">';
        echo '<br><br><textarea rows="8" cols="100" name="product_des">'.$product_des.'</textarea><br><input type="submit" value="Save Changes" name="save_changes_submit">';
        }
    }
    if(isset($_POST['save_changes_submit'])) {
        echo $c_product_price = $_POST['product_price'];
        echo $c_product_name = $_POST['product_name'];
        echo $c_product_des = $_POST['product_des'];
    }
    echo '</form>';
?>
Comments