编辑
感谢迄今为止的帮助。我编辑了我的帖子以反射(reflect)下面建议的更改。我正在使用 PDO 进行数据库连接。我现在的代码如下:
HTML
<a href="includes/delete-customer.php?userID='.$row->customer_id.'">
PHP
<?php
//MySQL Database Connect
include 'includes/config.php';
// confirm that the 'id' variable has been set
if (isset($_GET['userID']) && is_numeric($_GET['userID']))
{
// get the 'id' variable from the URL
$id = $_GET['userID'];
/* Delete row from the customer table */
$id = $dbh->exec("DELETE FROM customer WHERE customer_id = '$id'");
$stmt->execute();
}
?>
config.php
<?php
/*** mysql hostname ***/
$hostname = 'localhost';
/*** mysql username ***/
$username = 'user';
/*** mysql password ***/
$password = 'password';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=testDB", $username, $password);
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
我非常确定 HTML 现在是正确的,问题出在 delete-customer.php 文件上。我当前收到以下错误: fatal error :在非对象上调用成员函数 exec()
我不确定如何正确实现 PDO 查询。非常感谢任何进一步的建议。
最佳答案
您的 HTML 部分显示:
<a href="includes/delete-customer.php?customer_id=$id['.$row->customer_id.']">
这是您确切的 HTML 语法吗?该参数应该是实际的数字 ID,即 --
<a href="includes/delete-customer.php?customer_id=3">
-- 通过回显 $row->customer_id
(假设它存在),或者通过其他一些方法来了解该用户 ID。
您的 HTML 只需发送实际数据,而不是任何类型的变量语法。您的接收 PHP ($_GET['customer_id']
) 将为您解释该信息并将其正确传递给 MySQL。
关于php - 使用_GET url链接从mysql数据库中删除一条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9595045/