[英]Delete mySql row with PHP
我有商店網站,希望能夠刪除我添加的內容,但似乎無法使其正常工作。
所有相關代碼如下:
主頁:
<?php
//external pages area
include_once('config/database.php');
include_once('object/chair.php');
//grabs info from the various pages.sql files
$database = new Database();
$conn = $database->getConnection();
$chair = new Chair($conn);
//connection made with sql file
$stmt = $chair->readAll();
//reading all the data in the sql file
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
?>
<div class="ProductActionAdd" style="display:;">
<a href="chair-details.php?detailsid=<?php echo $row['ID'];?>" class="btn">Buy me!</a>
</div>
詳情頁面:
<?php
include_once('config/database.php');
include_once('object/chair.php');
$database = new Database();
$conn = $database->getConnection();
$chair = new Chair($conn);
$chair->id = $_GET['detailsid'];
$stmt = $chair->readDetails();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
?>
<a href='delete-chair.php?detailsid=<?php echo $row['ID'];?>' class="deleteit">deleate</a>
刪除頁面:
<?php
include_once('config/database.php');
include_once('object/chair.php');
$database = new Database();
$conn = $database->getConnection();
$chair = new Chair($conn);
$chair->id = $_GET['detailsid'];
$stmt = $chair->readAll();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
?>
<div class="center_items">
<div class="all-items">
<?php
$sql="DELETE FROM office_chairs WHERE id ='{$chair->id}'";
}
?>
它指出detailsID
未定義,並且班級椅子的對象不能轉換為字符串。
嗯……您通過$_GET
接收detailsid
參數,然后將其指定給名為id
的屬性。
$chair->id = $_GET['detailsid'];
因此,調用名為 $detailsid 的變量是不可能的——因為不存在這樣的變量。 也可以使用您分配了值的屬性。
$chair 變量包含類 Chair 的一個新實例。 不知道你的意圖是什么? 數據庫表名是動態的嗎? 如果沒有,只寫表名。
更正您的查詢來自
$sql="DELETE FROM $chair WHERE id = '$chair->id'";
到:
$sql="DELETE FROM table_name_here WHERE id ='$detailsid'";
可能會解決您的問題,給出您想要的結果。 但是您的代碼容易受到 SQL 注入的影響。 您應該考慮使用准備好的語句,而不是直接將變量連接到您的查詢。
例如
$sql="DELETE FROM table_name_here WHERE id = ?";
$stmt -> $con = prepare($sql);
$stmt -> execute(array($chair -> id));
更新
$sql="DELETE FROM $chair WHERE id ='$detailsid'";
到
$sql="DELETE FROM chair WHERE id ='{$chair->id}'";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.