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