mercredi 24 janvier 2018

Extecuting Query With The Condition in PHP

I have a page in which user can update their posts.

I need to update the database with different query(for different Conditions).

But every time I run update using this code, image filename changes automatically (even if I have a condition). Am I doing something wrong?


if(empty($up_image)){
          $update_query = "UPDATE posts  SET title = '$up_title', image = '$up_image', categories = '$up_categories', tags = '$up_tags', post_data = '$up_post_data', status = '$up_status' WHERE id = $edit_id";
          if(mysqli_query($con, $update_query)){
              $msg = "Post has been Updated";
              $path = "img/$up_image";
              header("refresh:1;edit-post.php?edit=$edit_id");



          }
          else{
              $error = "Unable to Update Post";
          }

          }

           if(!empty($up_image)){
             $up_image = preg_replace('/\s+/','',$up_image); 

           $image_size = $_FILES['image']['size'];
                $allowed_img_ext = array("jpg", "jpeg", "png", "bmp");
                $ext = pathinfo($up_image, PATHINFO_EXTENSION);
                $trimed_img_name = pathinfo($up_image, PATHINFO_FILENAME);

     if(in_array($ext, $allowed_img_ext))//check valid file extension
     {
        if($image_size < 2097152) {
         $ren_image = substr($trimed_img_name,0,3)."".substr($title,0,11)."_".date("mj")."_".date("Y")."_".date("His").".".$ext;
            $path = "img/".$ren_image;  
          $update_query = "UPDATE posts  SET title = '$up_title', image = '$ren_image', categories = '$up_categories', tags = '$up_tags', post_data = '$up_post_data', status = '$up_status' WHERE id = $edit_id";
                      }
         else{
             $img_error = "Please Upload the Image File Size Less than 2 MB"; 
         }
     }
              else{
                  $img_error = "Invalid Image File";
              }


              if(mysqli_query($con, $update_query)){

              $msg = "Post has been Updated";
              header("refresh:1;edit-post.php?edit=$edit_id");
                    if(move_uploaded_file($up_tmp_name, $path)){
               copy($path, "../$path");
            }

          }
          else{
              $error = "Unable to Update Post";
          }  



          } //End - not empty section inelse

Aucun commentaire:

Enregistrer un commentaire