0

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!

P.S.: 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);
        }
    }       
}
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Calin Rusu
  • 187
  • 1
  • 5
  • Please be more specific and provide a minimum code and minimum explanation regarding your problem... – Dekel Oct 22 '16 at 03:15
  • 1
    I guess, it is due to double request (you are somehow sending two HTTP requests instead of one), have you checked the webserver access logs? Maybe it is a bug in JavaScript, or maybe even the mouse is sending a double click instead of single click. Just check the logs. – Ruslan Osmanov Oct 22 '16 at 03:22
  • Ruslan Osmanov, indeed I am displaying a javascript prompt before loading the page but it should return false if user doesn't press OK. – Calin Rusu Oct 22 '16 at 03:26
  • Clayton Smith, I did echo the query first and it is what's supposed to be. The id is $_GET, script is accessed as mydomain.com/?path=delete_photo&id=117 – Calin Rusu Oct 22 '16 at 03:29
  • Thanks Ruslan Osmanov, it is weird, the javascript confirm() should work but it appears to be sending the http request before user clicks OK. I tested and if I commented out the javascript confirm() part it works as expected, no more error. I'll look into the javascript later, for now thank you very much, I owe you 100 hundred beers. How do you prefer to get them? Via $_POST or $_GET? Cheers mate! – Calin Rusu Oct 22 '16 at 03:35
  • @ClaytonSmith this will result in a serial upvote reversal. What you had to offer instead is to make a comment into an answer – Your Common Sense Oct 22 '16 at 08:19

1 Answers1

1

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.

Community
  • 1
  • 1
Ruslan Osmanov
  • 20,486
  • 7
  • 46
  • 60