I am trying to save a variable's value into a db using PDO, however I am getting Undefined variable
error.
The odd thing is that I can successfully echo the variable in the same block of code but still the error indicates that the variable is not defined on the next line (after the echo statement)!
Here is the code:
<?php
$sql = "SELECT po_number FROM po ORDER BY po_number DESC LIMIT 1 ;";
$STH = $pdo ->query($sql);
$row = $STH->fetch();
$myVar = $row['po_number'];
?>
<?php
if(isset($_POST['addProduct'])){
echo $myVar; //the variable can be echoed with the expected value here
$sql1 = "INSERT INTO po_parts (po_number, part_id, quantity) VALUES (?,?,?)";
$statement1->bindParam(1, $myVar);
//I am getting error that $myVar is undefined on the previous line
$statement1->bindParam(2, $_POST['part']);
$statement1->bindParam(3, $_POST['quantity']);
if($statement1->execute()){
$message = "Product was added successfully to the order number $newVar";
}else{
$message = "OP's was NOT added successfully.";
}
}
?>
Could anyone tell me what is the problem here?
Thank you.