Wordpress:PHP在foreach循环中删除数据库行

时间:2018-01-25 10:14:34

标签: php wordpress

大家好,我是PHP开发的新手。

我正在编写一个smiple脚本来从url获取并通过电子邮件发送perameter,循环访问数据库表中的某些行,如果找到则删除该行。

除了删除行外,我得到了所有内容。我真的很感激有人通过我的错误来帮助我理解。我认为错误在于WHERE条件。

我得到的错误信息是: 错误:无法执行DELETE FROM wp_email_address_db WHERE alaofolasade @ yahoo.com == alaofolasade @ yahoo.com。

    <?php

$path = $_SERVER['DOCUMENT_ROOT']; 
include_once $path . '/wp-load.php';

global $wpdb;

//sanatise and lowercase email addy from URL
$emailToRemove = filter_var ( strtolower($_GET["usermail"]), FILTER_SANITIZE_EMAIL);

//get DB table
$data = $wpdb->get_results("SELECT * FROM wp_email_address_db`");
    foreach ($data as $d) {
        //sanatise and lowercase email addy from DB
        $email = filter_var ( strtolower($d->email_address), FILTER_SANITIZE_EMAIL);

        //check if the email addy is in this row
        if($email == $emailToRemove){
            //get info
            echo 'true <br/>';
            echo 'id: '.$d->id.'<br/>';

            //I need to delete this row
            $sql = "DELETE FROM wp_email_address_db WHERE $email = $emailToRemove";
            if(mysqli_query($link, $sql)){
                echo "Records were deleted successfully.";
            } else{
                echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
            }

        } 
}


// $columnTitles = ['id','email_address','imported_via'];
?>

7 个答案:

答案 0 :(得分:1)

您的查询应该是:

$ sql =&#34; DELETE FROM wp_email_address_db WHERE email =&#39; $ emailToRemove&#39;&#34;;

答案 1 :(得分:1)

您在email中输入错误,且where子句中的值应在单引号内:

将其更改为:

        $sql = "DELETE FROM wp_email_address_db WHERE email = '$emailToRemove'";

答案 2 :(得分:0)

最好使用引号(单引号',双引号")来定义带有反向标记的列。

查询:

$sql = "DELETE FROM wp_email_address_db WHERE `email` = '".$emailToRemove."'";

您的主要错误是$email,其中应该是列名,后面有WHERE $email = $emailToRemove

答案 3 :(得分:0)

尝试替换

$sql = "DELETE FROM wp_email_address_db WHERE $email = $emailToRemove";

$sql = "DELETE FROM wp_email_address_db WHERE '$email' = '$emailToRemove'";

答案 4 :(得分:0)

不应该是像

这样的查询
DELETE FROM wp_email_address_db WHERE email_field_name = '$emailToRemove'

而不是

DELETE FROM wp_email_address_db WHERE $email = $emailToRemove

在您的查询中,您将$email的值作为字段名称传递,并假设字段alaofolasade@yahoo.com不存在

答案 5 :(得分:0)

我删除了foreach,您不需要从数据库中检索所有邮件。 此外,我使用准备好的语句来避免SQL注入

这种对数据库HAVE执行删除查询的脚本受到所有用户的保护。只允许管理员使用此脚本

<?php

$path = $_SERVER['DOCUMENT_ROOT']; 
include_once $path . '/wp-load.php';

// WARNING: You should check the identity of the user performing this action, MySql DELETE action is dangerous 

global $wpdb;

//sanatise and lowercase email addy from URL
$emailToRemove = filter_var ( strtolower($_GET["usermail"]), FILTER_SANITIZE_EMAIL);

$sqlDeleteQuery = '
    DELETE 
    FROM wp_email_address_db 
    WHERE email = ?
';

$statement = mysqli_prepare($link, $sqlDeleteQuery);

// Bind the email to your query (the ? ), this avoid the SQL injection attack
$statement->bind_param('s', $emailToRemove);

if (false !== mysqli_stmt_execute($statement)) {
    die('The MySql query failed ! Error : ' . mysqli_error($link));
}

$emailsDeleted = mysqli_stmt_affected_rows($statement);

echo sprintf('"%d" emails deleted', $emailsDeleted);

我无法尝试上述代码,如果出现问题,请告诉我们。)

答案 6 :(得分:0)

通过以下方式解决:

<?php
require_once($_SERVER["DOCUMENT_ROOT"]."/wp-load.php");
if (isset($_GET["usermail"])) {

    global $wpdb;

    $remove = filter_var(strtolower($_GET["usermail"]), FILTER_SANITIZE_EMAIL);

    $wpdb->query("DELETE FROM wp_email_address_db WHERE email_address='$remove'");

    echo "You have successfully been unsubscribed.";

} 
?>