<?php
session_start();
if (!isset($_SESSION['username'])) {
    echo "<script>window.top.location.href='index.php';</script>"; // Redirect parent window to login page
    exit();
}

include 'connect.php';
$product_id=$_GET['updateid'];
$sql="select * from products_table where product_id=$product_id";
$result=mysqli_query($conn,$sql);
$row=mysqli_fetch_assoc($result);
$product_number=$row['product_number'];
$pr_Name=$row['pr_Name'];
$pr_Price=$row['pr_Price'];
$pr_Cost=$row['pr_Cost'];


if(isset($_POST['submit'])){
    $product_number=$_POST['product_number'];
    $pr_Name=$_POST['pr_Name'];
    $pr_Price=$_POST['pr_Price'];
    $pr_Cost=$_POST['pr_Cost'];
    

    $sql = "update products_table set product_number='$product_number', pr_Name='$pr_Name',pr_Price='$pr_Price', pr_Cost='$pr_Cost' where product_id=$product_id";
    //$sql = "insert into users (name, email, mobile, password) values ('$name','$email','$mobile','$password')";
    $result=mysqli_query($conn, $sql);
    if($result){
        //echo "Data updated succesfully";
       header('location: displayproducts.php');
    }else{
        die(mysqli_error($conn));
    }
}

?>

<!doctype html>
<html lang="en" dir="ltr">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap CSS -->
<!--<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.rtl.min.css">-->
<link rel="stylesheet" href="css/bootstrap.min.css">

    <title>CRUD APPLICATION</title>
  </head>
  <body>
    <div class="container my-5"> 
        <h3>Change the information filled in the textboxes to update the product</h3>

        <form method="POST">
            <div class="mb-3">
                <label for="formGroupExampleInput" class="form-label">Product Code:</label>
                <input type="text" class="form-control" name="product_number" autocomplete="off" value="<?php echo $product_number; ?>">
            </div>
            <div class="mb-3">
                <label for="formGroupExampleInput" class="form-label">Product Name:</label>
                <input type="text" class="form-control" name="pr_Name" autocomplete="off" value="<?php echo $pr_Name; ?>">
            </div>
            <div class="mb-3">
                <label for="formGroupExampleInput" class="form-label">Product Cost:</label>
                <input type="text" class="form-control" name="pr_Cost" autocomplete="off" value=<?php echo $pr_Cost; ?>>
            </div>
            <div class="mb-3">
                <label for="formGroupExampleInput" class="form-label">Product Price:</label>
                <input type="text" class="form-control" name="pr_Price" autocomplete="off" value=<?php echo $pr_Price; ?>>
            </div>
            
            
            <button type="submit" class="btn btn-primary" name="submit">Update</button>
        </form>    

    </div>

    <script src="js/jquery.min.js"></script>
<script src="js/bootstrap.bundle.min.js"></script>
  </body>
</html>