I'm checking a project and I found this code that is suppose to delete records from MySQL
, despite this is not the best aproach to achieve this I have to make this works, but for me is fine, that's why I'm here, because I don't see the error or what is wrong with this code, is not displaying errors, warning, nothing I tested the code directly in PHPMyAdmin
and it works well, so this is the code...
$get_records = mysql_query('SELECT id_detail FROM my_table WHERE user_id = "'.$user_id.'" AND last_date BETWEEN "'.$date1.'" AND "'.$date2.'"') or die (mysql_error());
$array_details = array();
while ($details = mysql_fetch_array($get_records)) {
$array_details[] = $details['id_detail'];
}
$array_details = array_unique($array_details);
foreach ($array_details as $id_detail) {
$delete_detail = mysql_query("DELETE FROM my_table WHERE user_id = '".$user_id."' AND id_detail = '".$id_detail."'") or die (mysql_error());
}
I got two records in the same table with the same ID detail (this works that way), so If I get the ID correcly with the query inside the foreach
loop it should delete all the records with that id_detail
and user_id
doesn't matter if in the query I say between which dates get those IDs, it deletes only one, and that one is which has the date between that range
I have no idea why is not deleting both, some idea about what I'm missing?
NOTE: As I said before if I do that query directly in PHPMyAdmin
it works fine and if I do from PHP it delete only one.