Search code examples
phpdatabaseformsif-statementinput-type-file

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


Solution

  • Does this work?

    if($filename == '') $filename = $image[0];
    if($filename1 == '') $filename1 = $image[1];
    if($filename2 == '') $filename2 = $image[2];
    if($filename3 == '') $filename3 = $image[3];
    if($filename4 == '') $filename4 = $image[4];
    
    $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/$filename2/$filename3/$filename4' WHERE 
       id='$unit_id'";
    
    $save = mysqli_query($conn, $sql);
    

    And if you want it a bit shorter....

    for($x = 0; $x < 5; $x++)
    {
        eval("if(\$filename".(($x==0)?"":$x)." == '') \$filename".(($x==0)?"":$x)." = \$image".(($x==0)?"":$x)."[0];");
    }
    

    Not fully tested but seems to work..