PHP删除产品/ ID

时间:2016-01-16 19:37:24

标签: php button

所以我有这个:

    echo "
        <form action='users/delete.php' method='post'>
          <input type='hidden' name='ProductID' value='".$row->ProductID."' />
          <input type='submit' value='Delete' />
        </form>
    ";

这包含在网上商店的每个项目下: Webshop

这样可行,但每当按下按钮时表格都没有被删除,这是我的delete.php

<?php

define('_HOST_NAME_', 'localhost');
define('_USER_NAME_', 'root');
define('_DB_PASSWORD', '#####');
define('_DATABASE_NAME_', 'ws_webshop');

//PDO Database Connection
try {
 $databaseConnection = new PDO('mysql:host='._HOST_NAME_.';dbname='._DATABASE_NAME_, _USER_NAME_, _DB_PASSWORD);
 $databaseConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
 echo 'ERROR: ' . $e->getMessage();
}


    // create PDO instance; assign it to $db variable
    $sql = "DELETE FROM `tbl_products` WHERE `ProductID` = :ProductID LIMIT 1";
    $smt = $databaseConnection->prepare($sql);
    $smt->bindParam(':ProductID', $_POST['ProductID'], PDO::PARAM_INT);
    $smt->execute();

header('Location: ../webshop.php');

但它没有删除它,有没有人看到问题? 谢谢!

2 个答案:

答案 0 :(得分:1)

您的$row用法是针对某个对象的,但您有一个数组。因此,要么将您的提取更改为:

->fetch(PDO::FETCH_OBJ);

- http://php.net/manual/en/pdostatement.fetch.php

或将您的使用情况更改为:

echo "
        <form action='users/delete.php' method='post'>
          <input type='hidden' name='ProductID' value='".$row['ProductID'] . "' />
          <input type='submit' value='Delete' />
        </form>
    ";

答案 1 :(得分:0)

在第

$sql = "DELETE FROM 'tbl_products' WHERE 'ProductID' = :ProductID LIMIT 1";

尝试更改为:

$sql = "DELETE FROM 'tbl_products' WHERE 'ProductID' = ProductID LIMIT 1";

没有&#34; :&#34;接近ProductID LIMIT 1。 在MySQL控制台中尝试一下。