I am trying to update and insert using one file. Following code can insert the data but when i try to update it is not working but data is also going to else part when i try to update it.

**Flow Homepage -> Insert/Update UI -> Insert/Update Operation**

Homepage which display all the data. There is update and delete link button. Now you got idea that id is already passing.

```
<!DOCTYPE>
<?php
    session_start();
    /*if(!isset($_SESSION["isLogin"]))
    {
        header("location:index.php");
    }*/
?>
<html>
    <head>

        <title></title>

        <?php 
            include_once("include.html");
            include_once("dbconfig.php");
        ?>

    </head>

    <body>
        <?php include_once("navbar.html"); ?>
        <div class="main mainContentMargin">
            <div class="row">
                <?php
                    $sth = $pdo->prepare("SELECT * FROM product_master where isActive='y' order by id desc");
                    $sth->execute();

                ?>
                <div class="card col s12">
                    <table class="responsive-table centered striped">
                        <thead>
                            <tr>
                                <th style="width: 15%">Product Name</th>
                                <th>Description</th>
                                <th style="width: 15%">Price</th>
                                <th>Update</th>
                                <th>Delete</th>
                            </tr>
                        </thead>
                        <tbody>
                    <?php
                         while ($row = $sth->fetch(PDO::FETCH_ASSOC)) 
                         {
                    ?>
                            <tr>
                                <td style="width: 15%"><?php echo $row["p_name"] ?></td>
                                <td ><?php echo $row["description"] ?></td>
                                <td style="width: 15%"><?php echo $row["price"]." Rs./".$row["unit"] ?></td>
                                <td style="width:5%"><a href="product.php?id=<?php echo $row["id"] ?>"><i class="material-icons">mode_edit</i></a></td>
                                <td style="width:5%"><a href="delete.php?id=<?php echo $row["id"] ?>"><i class="material-icons">mode_delete</i></a></td>
                            </tr>
                    <?php
                         }
                    ?>
                        </tbody>
                    </table>
                </div>

            </div>
        </div>
        <?php include_once("footer.html");?>
    </body>
</html>

```

Insert / Update UI

```
<?php
    session_start();
    /*if(!isset($_SESSION["isLogin"]))
    {
        header("location:index.php");
    }*/
?>
<html>
    <head>

        <title></title>

        <?php 
            include_once("include.html");
            include_once("dbconfig.php");
        ?>

    </head>

    <body>
        <?php include_once("navbar.html"); ?>
        <?php
            $product="";
            $descritpion="";
            $price="";
            $unit="";
            $ins_up="Insert";
            if(isset($_REQUEST["id"]))
            {
                $sth = $pdo->prepare("SELECT * FROM product_master where id=".$_REQUEST["id"]);
                $sth->execute();
                while ($row = $sth->fetch(PDO::FETCH_ASSOC)) 
                {
                    $product=$row["p_name"];
                    $descritpion=$row["description"];
                    $price=$row["price"];
                    $unit=$row["unit"];
                    $ins_up="Update";
                }
            }
        ?>
        <div class="main mainContentMargin">
            <div class="row">
                <form method="post" action="insertProduct.php">
                    <div class="card col s12">
                        <div class="card-content">
                            <div class="input-field">
                                <input type="text" name="txtProductname" id="txtProductname" value="<?php echo $product ?>">
                                <label for="txtProductname">Product Name</label>
                            </div>
                            <div class="input-field">
                                <textarea name="txtDesc" id="txtDesc" class="materialize-textarea" value="<?php echo $descritpion ?>"></textarea>
                                <label for="txtDesc">Description</label>
                                <script>
                                        $(document).ready(function($) {
                                            $('#txtDesc').val("<?php echo $descritpion ?>");    
                                        });
                                </script>
                            </div>
                            <div class="input-field">
                                <input type="number" name="txtProductprice" id="txtProductprice" value="<?php echo $price ?>">
                                <label for="txtProductprice">Price</label>
                            </div>
                            <div>
                                <?php 
                                    if($unit=="pcs" || $unit=="")
                                    {
                                ?>

                                        <input name="group1" type="radio" id="pcsUnit" value="pcs" checked />
                                        <label for="pcsUnit">Pcs.</label>

                                        <input name="group1" type="radio" id="pcsKg" value="kg" />
                                        <label for="pcsKg">KG.</label>
                                <?php
                                    }
                                    else
                                    {
                                ?>
                                        <input name="group1" type="radio" id="pcsUnit" value="pcs" />
                                        <label for="pcsUnit">Pcs.</label>

                                        <input name="group1" type="radio" id="pcsKg" value="kg" checked />
                                        <label for="pcsKg">KG.</label>
                                <?php
                                    }
                                ?>
                            </div>  
                        </div>
                        <div class="card-action">
                            <div class="input-field">
                                <input type="submit" class="btn" name="btnInsert" id="btnInsert" value="<?php echo $ins_up ?>"></td>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
        <?php include_once("footer.html");?>
    </body>
</html>

```

Insert / Update Operation File

```
<?php
    include("dbconfig.php");

    if(isset($_REQUEST["id"]))  
        $id=$_REQUEST["id"];

    $name=$_REQUEST["txtProductname"];
    $description=$_REQUEST["txtDesc"];
    $price=$_REQUEST["txtProductprice"];
    $unit=$_REQUEST["group1"];

    if($_REQUEST["btnInsert"]!="Update")
    {
        $stmt=$pdo->prepare("INSERT INTO product_master (p_name, description, price,unit,isActive) 
        VALUES (:p_name, :description, :price,:unit,:isActive)");
        $isActive='y';
        $stmt->bindParam(':isActive', $isActive);
    }
    else
    {
        $stmt=$pdo->prepare("update product_master SET p_name=:p_name , description=:description , price=:price , unit=:unit where id=:id");
        $stmt->bindParam(":id",$id);
    }
    $stmt->bindParam(':p_name', $name);
    $stmt->bindParam(':description', $description);
    $stmt->bindParam(':price', $price);
    $stmt->bindParam(':unit', $unit);

    $stmt->execute();
    if($stmt->rowCount()) {
       echo 'success';
     } else {
       echo 'update failed';
     }
    //header('Location: home.php');
?>

```

DBConfig

```
<?php
    $pdo = new PDO("mysql:host=localhost; dbname=db_inventory;","root","");
    $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdo->exec("set names utf8");
?>

```
['https://Stackoverflow.com/questions/43751628', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4995030/']
`$id` is not defined when you run your `UPDATE`.  

While you have defined `id` while you're on on the GUI page, the value is not passed with the next request to the script that actually queries your database.  

Add the following line to your form:

```
<input type="hidden" name="id" value="<?php echo htmlentities($_GET['id']); ?>" />

```
Make user your id is retrieved properly. In your form i didn't any input element for the **id**. So when you try to get from the request it always comes empty.  

If you server error logs are on, you might get the error **undefined variable $id...**