如何在输入字段中从数据库中提取数据时修复代码中的未定义变量问题[重复]

I am trying to fetch the data from database for a an input field product code and i need to use its value to update the rest of the column values in the database but instead it is creating a different record and in the value field of the input box 'named' code, it shows and undefined variable error, please help.

HTML code:
<div class="small-8 columns">
          <input type="text" id="right-label" placeholder="Product_code" 
 value="<?php echo "$pcode"?>" name="code">
        </div>
PHP Script:
 <?php
  $servername="localhost";
  $username="root";
  $password="";
  $dbname="bolt";
  try{
  $conn = new 
 PDO("mysql:host=$servername;dbname=$dbname",$username,$password);
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


 if(isset($_POST["submit"])){
 $pcode = ($_POST["code"]);
 $pname = ($_POST["Pname"]);
 $pdesc = ($_POST["desc"]);
 $pimg = $_FILES["Img_name"]["temp_name"];
 $imgExt = strtolower(pathinfo($pimg,PATHINFO_EXTENSION));
 $valid_extensions = array('jpeg','jpg','png','gif','pdf');     
 $pqty = ($_POST["Pqty"]);
 $pprice = ($_POST["Pprice"]);
 $sql="UPDATE  products SET product_name=$pname,product_desc=$pdesc,
  product_img_name=$pimg,qty=$pqty,price 
  =$pprice) WHERE product_code=$pcode";
  $stmt = $conn->exec($sql);
 $stmt->execute();
  echo $stmt->rowCount() . "new records added succesfully";    
  }
  }
 catch(PDOException $e){

echo $sql . "<br>" . $e->getMessage(); 

}
$conn = null;      


?>
</div>

$sql is declared within the if condition, if if(isset($_POST["submit"])){ is false, you will get this error because $sql is not within the scope. Declare it on above condition and initialize it.