[英]Error when Updating query using PDO
我已經設置了一個自定義函數來更新我的查詢,但是它似乎沒有讀取變量,我不確定我要去哪里。 這是我的職責
function update_product($productID, $productName, $productDescription, $productPrice, $categoryID){
global $conn;
$sql = "UPDATE product SET productName = :productName, productDescription = :productDescription, productPrice = :productPrice, categoryID = :categoryID WHERE productID = :productID";
$statement = $conn->prepare($sql);
$statement->bindValue(':productName', $productName);
$statement->bindValue(':productDescription', $productDescription);
$statement->bindValue(':productPrice', $productPrice);
$statement->bindValue(':categoryID', $categoryID);
$statement->bindValue(':productID', $productID);
$result = $statement->execute();
$statement->closeCursor();
return $result;
}
這是我在調用函數的地方
$result = update_product($productID, $productName, $productDescription, $productPrice, $categoryID);
我還將在獲取ID並加載輸入的地方添加我的函數...
function select_productspreparedGETID(){
global $conn;
$productID = $_GET['id'];
//prepared
$sql = "SELECT * FROM product WHERE productID = :productID";
$statement = $conn->prepare($sql);
$statement->bindValue(':productID', $productID);
$statement->execute();
$result = $statement->fetchAll();
$statement->closeCursor();
foreach($result as $row):
?>
<form method="post" action="../controller/product_update_process.php" enctype="multipart/form-data">
<div>
<label>Name* </label>
<input id="Product_Name" type="text" name="Product_Name" value="<?php echo $row['productName'] ?>" />
</div>
<div>
<label>Description* </label>
<textarea id="productDescription" name="productDescription" /><?php echo $row['productDescription'] ?></textarea>
</div>
<div>
<label>Price* </label>
<input id="Price" type="text" name="Price" value="<?php echo $row['productPrice'] ?>" />
</div>
<input type="submit" name="submit" value= "Update Cart">
</form>
<?php
endforeach;
}
對於如何改善我的結構或更好地克服此錯誤的任何建議,我將在過去的20個小時中一直停留在我的位置,深表感謝。
您正在定義$productID = $_GET['id'];
在一個函數內部,並在另一個函數中使用相同的變量。 $productID
顯然對其他功能不可用。 確保在任何功能之外定義它。
當前,變量僅可用於定義它的本地范圍。 例如,在任何函數之外定義$productID
:
$productID = $_GET['id'];
您可以將此變量作為參數發送到select_productspreparedGETID()
:
function select_productspreparedGETID($productID){
//Your code
}
然后,在update_product()
和select_productspreparedGETID()
函數中都具有$productID
因為update_product()
已經具有$productID
作為參數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.