我正在尝试使用PDO将变量的值保存到db中,但是却Undefined variable
出现错误。
奇怪的是,我可以在同一代码段中成功回显该变量,但是错误仍然表明该变量未在下一行(在echo语句之后)定义!
这是代码:
<?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.";
}
}
?>
谁能告诉我这里是什么问题?
谢谢你。
您必须先准备对帐单,然后才能使用它。
<?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 (?,?,?)";
// Inserted this row
$statement1 = $pdo->prepare($sql1);
$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.";
}
}
?>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句