PHP and MySQL
Hello,
I have a MySQL table called “weddings”, containing “id” – int, primary key, Auto_increment, “male_name”, “female_name”, “year” and “file” (PDF file name containing the wed certificate)
I have a form with checkboxes for each wed from the table. Each checkbox has the record id as “id” and the record’s filename(e.g. filename.pdf) as it’s value. The user selects which wed one wants to delete (delete the record from the table and the PDF file from the server – which is constructed like “filename_recordID.pdf” (for the record with id 5, the file name from DB and from server is filename_5.pdf)
What works:
The data from the form is retrieved ok. The delete is ok (for the record in the DB and for the file on the server).
What doesn’t work:
I want to display for the user the female_name and the male_name for the record that the file on the server had an error when deleting (I have 2 PDF that have errors and I use them as tests everytime for this). Aditionally, I want to display an error code: for the undeleted files from server, the error code will be “filename_without_extension_SRV”. (for the record with id 10, it will look like “wed_10_SRV”)
(I will do the same with records that cannot delete from DB, if case: display for the user the female_name and the male_name and the error code, for the record with id 10, it will look like “wed_10_DB”)
Each file (on the server and the name in the DB will be like: wed_<wed_id>.pdf (my php script modifies like this each uploaded file)
undeleted_weds_serv = Array containing the IDs of the records that were undeleted from the server (the corresponding PDF file didn’t delete on the server)
length_undeleted_serv = length of the Array
undeleted_weds_serv Array contains the records ids of the weds that couldn’t be erased from the server and contains 0 for the weds that deleted successfully from the server (the PDF file of the wed)
The Array and the length are constructed ok (I echoed the Array and it’s length and it’s constructed ok).
My code below:
//1. deleting the file from the server
// Path to the directory containing files
$directory_path = '../../Continut_site/Primaria/Birouri/Evidenta_persoane/publicatii_casatorie/2024_teste';
// Full path to the file
$file_path = $directory_path . '/' . $fisier;
// Check if the file exists and delete it
if (unlink($file_path)) {
//array-ul cu id-urile casatoriilor STERSE de pe server
$casatorii_Sterse_serv[] = $wed_id;
//array-ul cu id-urile casatoriilor nesterse de pe server
$undeleted_weds_serv[] = 0;
} else {
//array-ul cu id-urile casatoriilor nesterse de pe server
$undeleted_weds_serv[] = $wed_id;
}
//END deleting file from the server
//2. deleting the file from the DB
$sql = "DELETE FROM weddings WHERE id = $wed_id";
if (mysqli_query($con, $sql)) {
//echo "Record deleted successfully";
//array containing the ids of the deleted weds (from the DB)
$db_deleted_weds[] = $wed_id;
//array containing the ids of the UNDELETED weds (from the DB)
$db_undeleted_weds[] = 0;
} else {
//array containing the ids of the UNDELETED weds (from the DB)
$db_undeleted_weds[] = $wed_id;
}
//END deleting the file from the DB
}//end FOREACH (loop through $_GET array)
//length of the array containing the ids of the records that their corresponding files were deleted from server - OK
$length_undeleted_serv = count($undeleted_weds_serv);
//length of the array containing the ids of the records that their corresponding files were deleted from server - OK
//$length_deleted_db = count($db_undeleted_weds);
//echo "length_undeleted_serv: $length_undeleted_serv - OK";
//echo "length_deleted_serv: $length_deleted_serv - OK";
for($i=0; $i < $length_undeleted_serv; $i++){
//echo "(serv) $undeleted_weds_serv[$i]<br>";
if($undeleted_weds_serv[$i]){
echo "(nesters serv) ".$undeleted_weds_serv[$i]."<br>";
$sql_undeleted_weds_serv = "SELECT * FROM `weddings` WHERE `id` = '$undeleted_weds_serv[$i]'";
$result_undeleted_weds_serv = mysqli_query($con, $sql_undeleted_weds_serv);
//cauta in BD numele persoanelor nesterse pentru afisare
while($record = mysqli_fetch_array($result_undeleted_weds_serv)){
$male_name = $record['male_name'];
$female_name = $record['female_name'];
$db_file = $record['fis']; //file name from the BD (it exists on the server with the same name)
// Remove the extension of the file (use it's name as an error code displayed at the user)
$filename_without_ext = substr($db_file, 0, strrpos($db_file, "."));
//end remove file extension
echo "<br><br> ❌ An error occured in <i>".$male_name."</i> și <i>".$female_name."</i> <font color='#f26b1e'> ⇒ "." Error code: <b>".$filename_without_ext."_SRV"."</b></font>"."⚠️";
}//end WHILE
if (mysqli_num_rows($result_undeleted_weds_serv) > 0) {
// output data of each row
while ($row = mysqli_fetch_assoc($result_undeleted_weds_serv)) {
echo "id: " . $row["id"] . " - Name: " . $row["name"] . "<br>";
}
} else {
echo "0 results";
}
}else{
echo "<br> ✅ Was deleted <i>".$male_name."</i> și <i>".$female_name."</i> (".$db_file.") <br><br>";
}//end if_1
}//end FOR looping through the Array
6