gpt4 book ai didi

php - 用 PHP 删除 mySql 行

转载 作者:行者123 更新时间:2023-11-29 02:20:35 25 4
gpt4 key购买 nike

我有商店网站,希望能够删除我添加的内容,但似乎无法正常工作。

所有相关代码如下:

首页:

<?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 未定义,类(class)主席的对象无法转换为字符串。

最佳答案

好吧...您正在通过 $_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 注入(inject)的攻击。您应该考虑使用准备好的语句,而不是直接将变量连接到您的查询。

例如

$sql="DELETE FROM table_name_here WHERE id = ?";
$stmt -> $con = prepare($sql);
$stmt -> execute(array($chair -> id));

关于php - 用 PHP 删除 mySql 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32552917/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com