简体   繁体   中英

mysqli_fetch_assoc() weirdest error

I am stuck with this mysqli_fetch_assoc() error and it's driving me insane. Problem is that I am trying to check if a row matching integer ID exists in the database. If it exists, the record should be deleted, else, it should display a warning that record wasn't found. The thing is that it is displaying the warning that the record wasn't found but at the same time it deletes the row from the database. I have double checked hundreds of times directly in the table and record exists. I have tried to do it with pdo instead of mysqli but I am getting the same shit.

$id = intval($_GET['id']);
$qp = $dbh->query("SELECT * FROM Pictures WHERE ID = " . $id) or die ('SQL error on line ' . __LINE__ . ' in ' . __FILE__);
$rp = $qp->fetch_assoc();
if (is_null($rp)) {
    display_alert('danger', 'Photo not found!');
} else {
    $horse = get_horse_info($rp['HorseID']);
    if (is_null($horse)) {
        display_alert('danger', 'Horse not found!');
    } else {
        $ownership = is_owner($horse['ID'], $user['ID']);
        if ($ownership < 1) {
            display_alert('danger', 'Permission denied! Only horse owners can delete photos!');
        } else {
            if (file_exists(ABS_PATH . '/upload/' . $rp['Image'])) {
                @unlink(ABS_PATH . '/upload/' . $rp['Image']);
            }
            $dbh->query("DELETE FROM Pictures WHERE ID = " . $rp['ID']) or die ('SQL error on line ' . __LINE__ . ' in ' . __FILE__);
            display_alert('success', 'Success: photo deleted!');
            redirect_page('/?path=horses&action=display&id=' . $horse['ID'], 2);
        }
    }       
}

OR (counting rows first)

$id = intval($_GET['id']);
$qp = $dbh->query("SELECT * FROM Pictures WHERE ID = " . $id) or die ('SQL error on line ' . __LINE__ . ' in ' . __FILE__);
$cp = $qp->num_rows;
if ($cp == 0) {
    display_alert('danger', 'Photo not found!');
} else {
    $rp = $qp->fetch_assoc();
    $horse = get_horse_info($rp['HorseID']);
    if (is_null($horse)) {
        display_alert('danger', 'Horse not found!');
    } else {
        $ownership = is_owner($horse['ID'], $user['ID']);
        if ($ownership < 1) {
            display_alert('danger', 'Permission denied! Only horse owners can delete photos!');
        } else {
            if (file_exists(ABS_PATH . '/upload/' . $rp['Image'])) {
                @unlink(ABS_PATH . '/upload/' . $rp['Image']);
            }
            $dbh->query("DELETE FROM Pictures WHERE ID = " . $rp['ID']) or die ('SQL error on line ' . __LINE__ . ' in ' . __FILE__);
            display_alert('success', 'Success: photo deleted!');
            redirect_page('/?path=horses&action=display&id=' . $horse['ID'], 2);
        }
    }       
}

They are both doing the same: delete the record but display the alert (which says "Photo not found")

Please help, it's driving me insane and I can't focus on anything else. I'm stuck with this. Hundreds of beers coming your way! Cheers!

PS: I am usually working with mysqli but I tried PDO this time and the crzy thing is the same happens with PDO.

$id = intval($_GET['id']);
$qp = $pdo->prepare("SELECT * FROM Pictures WHERE ID = " . $id);
$qp->execute();
$rp = $qp->fetch(PDO::FETCH_ASSOC);
if (is_null($rp)) {
    display_alert('danger', 'Photo not found!');
} else {
    $horse = get_horse_info($rp['HorseID']);
    if (is_null($horse)) {
        display_alert('danger', 'Horse not found!');
    } else {
        $ownership = is_owner($horse['ID'], $user['ID']);
        if ($ownership < 1) {
            display_alert('danger', 'Permission denied! Only horse owners can delete photos!');
        } else {
            if (file_exists(ABS_PATH . '/upload/' . $rp['Image'])) {
                @unlink(ABS_PATH . '/upload/' . $rp['Image']);
            }
            $del->prepare("DELETE FROM Pictures WHERE ID = " . $rp['ID']);
            $del->execute();
            display_alert('success', 'Success: photo deleted!');
            redirect_page('/?path=horses&action=display&id=' . $horse['ID'], 2);
        }
    }       
}

The most likely reason is a double HTTP request (you are somehow sending two HTTP requests instead of one). Check the server logs, and you'll likely find two entries for the form submission.

It may be a bug in JavaScript, or even the mouse is sending a double click instead of single click, or just double clicking.

So you should prevent double clicking. Refer to this post, for instance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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