dimanche 27 janvier 2019

Is there any shortcut for a 120 if statements for updating database with a blank file-input type in php?

I have a 5 file input type inside a form in php. I want to save / update my database not replacing the database an empty one if one or more than one of my file input type left empty. I computed the possible if statements using permutation and it gave me 120 if statements to be exact to complete the needed conditions.

I tried the 120 if statements and it worked.

if($filename == '' && $filename1 != '' && $filename2 != '' && $filename3 != '' && $filename4 != ''){
$sql = "UPDATE units SET unit_name='$unit_name', barangay='$barangay', address='$address', owner_name='$owner_name',
    rate='$rate', rooms='$rooms', contact_num='$contact_num', facilities='$facilities', maintenance='$maintenance',
    rules_reg='$rules_reg', images='$image[0]/$filename1/$filename2/$filename3/$filename4' WHERE id='$unit_id'";
    $save = mysqli_query($conn, $sql);
  }else if($filename != '' && $filename1 == '' && $filename2 != '' && $filename3 != '' && $filename4 != ''){
    $sql = "UPDATE units SET unit_name='$unit_name', barangay='$barangay', address='$address', owner_name='$owner_name',
    rate='$rate', rooms='$rooms', contact_num='$contact_num', facilities='$facilities', maintenance='$maintenance',
    rules_reg='$rules_reg', images='$filename/$image[1]/$filename2/$filename3/$filename4' WHERE id='$unit_id'";
    $save = mysqli_query($conn, $sql);
  }else if($filename != '' && $filename1 != '' && $filename2 == '' && $filename3 != '' && $filename4 != ''){
    $sql = "UPDATE units SET unit_name='$unit_name', barangay='$barangay', address='$address', owner_name='$owner_name',
    rate='$rate', rooms='$rooms', contact_num='$contact_num', facilities='$facilities', maintenance='$maintenance',
    rules_reg='$rules_reg', images='$filename/$filename1/$image[2]/$filename3/$filename4' WHERE id='$unit_id'";
    $save = mysqli_query($conn, $sql);
  }

and so on...

It successfully updates the database but I want the code to shorten more

Aucun commentaire:

Enregistrer un commentaire