使用_GET url链接从mysql数据库中删除记录 [英] using _GET url link to delete a record from mysql database

查看:215
本文介绍了使用_GET url链接从mysql数据库中删除记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑



感谢您的帮助。我编辑了我的帖子以反映下面提出的更改。我正在使用PDO进行数据库连接。我现在的代码如下:

HTML

 < a href =includes / delete-customer.php?userID ='。$ row-> customer_id。'> 

PHP

 <?php 

// MySQL数据库连接
include'includes / config.php';

//如果(isset($ _ GET ['userID'])&& is_numeric($ _ GET ['userID'])),则确认'id'变量已被设置为
) )
{
//从URL获取'id'变量
$ id = $ _GET ['userID'];
$ b $ *从客户表中删除一行* /
$ id = $ dbh-> exec(从客户那里删除customer_id ='$ id');

$ stmt-> execute();
}
?>

config.php

 <?php 

/ *** mysql hostname *** /
$ hostname ='localhost';

/ *** mysql用户名*** /
$ username ='user';

/ *** mysql密码*** /
$ password ='password';

尝试{
$ dbh = new PDO(mysql:host = $ hostname; dbname = testDB,$ username,$ password);

catch(PDOException $ e)
{
echo $ e-> getMessage();
}
?>

我很确定HTML现在是正确的,问题在于delete-customer.php文件。我目前收到以下错误:致命错误:调用一个非对象的成员函数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。


EDIT

Thanks for the help so far. I have edited my post to reflect the changes suggested below. I am using PDO for my database connection. The code I have now is as follows:

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();
    }
?>

I'm pretty sure the HTML is correct now and the issue lies with the delete-customer.php file. I am currently receiving the following error: Fatal error: Call to a member function exec() on a non-object

I'm not sure of how to implement the PDO query correctly. Any further advice is much appreciated.

解决方案

Your HTML section says:

<a href="includes/delete-customer.php?customer_id=$id['.$row->customer_id.']">

Is this your exact HTML syntax? This argument should be the actual numerical id, i.e. --

<a href="includes/delete-customer.php?customer_id=3">

-- either by echoing $row->customer_id (assuming it exists), or some other method of knowing that user id.

Your HTML only needs to send the actual data, not any sort of variable syntax. Your receiving PHP ($_GET['customer_id']) will interpret that for you and properly pass that to MySQL.

这篇关于使用_GET url链接从mysql数据库中删除记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆