简体   繁体   English

用 PHP 删除 mySql 行

[英]Delete mySql row with PHP

I have shop site and want to be able delete what I add but can't seem to get it to work.我有商店网站,希望能够删除我添加的内容,但似乎无法使其正常工作。

All relevant code is below:所有相关代码如下:

Home page:主页:

<?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>

Details page:详情页面:

<?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>

Delete page:删除页面:

<?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}'";    
  }
        ?>

It states that detailsID is undefined and that Object of class chairs can't be converted into a string.它指出detailsID未定义,并且班级椅子的对象不能转换为字符串。

Well... you're receiving the detailsid parameter through $_GET and then you appoint it to a property called id .嗯……您通过$_GET接收detailsid参数,然后将其指定给名为id的属性。

$chair->id = $_GET['detailsid'];

Thus, calling a variable with the name $detailsid is not possible - cause no such variable exist.因此,调用名为 $detailsid 的变量是不可能的——因为不存在这样的变量。 Use the property you assigned the value too instead.也可以使用您分配了值的属性。

The $chair variable contains a new instance of the class Chair. $chair 变量包含类 Chair 的一个新实例。 I don't know what your intentions are?不知道你的意图是什么? Is the database table name dynamic?数据库表名是动态的吗? If not, just write the table name.如果没有,只写表名。

Correcting your query from更正您的查询来自

$sql="DELETE FROM $chair WHERE id = '$chair->id'";

to:到:

$sql="DELETE FROM table_name_here WHERE id ='$detailsid'";

might fix your problem giving the result you wish for.可能会解决您的问题,给出您想要的结果。 But your code is vulnerable to SQL injections.但是您的代码容易受到 SQL 注入的影响。 You should consider using prepared statments instead of directly connecting the variables to your query.您应该考虑使用准备好的语句,而不是直接将变量连接到您的查询。

For example例如

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

Update更新

$sql="DELETE FROM $chair WHERE id ='$detailsid'";

to

$sql="DELETE FROM chair WHERE id ='{$chair->id}'";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM