phpsqlmysqlsql-delete

Deleting row from table in mysql database


I am trying to delete a certain item from a database depending on conditions. Here is what I have:

while ($row = mysql_fetch_array($result)) {

$now = strtotime("now");
$dateArray = date_parse_from_format("n-j-Y", $row["date"]);
$event_date = strtotime($dateArray['year'].'-'.$dateArray['month'].'-'.$dateArray['day']);
    // temp user array
    $event = array();
    if($event_date > $now) {
//Event is in the future 
    $pid_check =$row["pid"];
    $event["pid"] = $row["pid"];
    $event["name"] = $row["name"];
    $event["longitude"] = $row["longitude"];
    $event["latitude"] = $row["latitude"];
    $event["pavement"] = $row["pavement"];
    $event["traffic"] = $row["traffic"];
    $event["environment"] = $row["environment"];
    $event["image_b64"] = $row["image_b64"];
    $event["date"] = $row["date"];
    $event["time"] = $row["time"];
    $event["type"] = $row["type"];
    // push single product into final response array
    array_push($response["events"], $event);
    } else {
    $result2 = mysql_query("DELETE FROM events WHERE pid = $pid_check");
}
}

But when I try this it comes up blank, when I comment out result2 it works but doesn't delete(duh). How can I get it to delete? Sorry if this is a simple question, my knowledge of the language is not much.


Solution

  • I think $pid_check is getting set only if the condition in your if is TRUE.

    It's not getting set for the else branch.

    One option is to relocate the assignment of $pid_check before the if test.