POST, move_uploaded_file, sql query error in php form

110 Views Asked by At

So I'm working on a project for a relatively simple e-commerce website. I've been trying to implement a form that allows users to post products on the said site and attach images. I set a condition to check if it's empty and it works quite alright, but the moment that condition isn't met, the else clause just doesn't seem to execute

Here's the part code;

<?php
include('../Includes/connect.php');

    if(isset($_POST['insert_product'])){
    $prod_title=$_POST['product_title'];
    $desc=$_POST['description'];
    $kw=$_POST['product_keywords'];
    $prod_cat=$_POST['product_category'];
    $price=$_POST['product_price'];
    $prod_status='true';

    //images
    $p_img1=$_FILES['product_image1'] ['name'];
    $p_img2=$_FILES['product_image2'] ['name'];
    $p_img3=$_FILES['product_image3'] ['name'];
    
    //image temp name
    $tmp_img1=$_FILES['product_image1'] ['tmp_name'];
    $tmp_img2=$_FILES['product_image2'] ['tmp_name'];
    $tmp_img3=$_FILES['product_image3'] ['tmp_name'];

    //checking empty conditions
    if($prod_title=='' or $desc=='' or $kw=='' or $prod_cat=='' or $price=='' or $p_img1=='' or   $p_img2=='' ){
        echo "<script>('Please fill all the available fields')</script>";
        exit();   
    }else{
        
        move_uploaded_file($tmp_img1,"../Images/prod_imgs/$p_img1");
        move_uploaded_file($tmp_img2,"../Images/prod_imgs/$p_img2");
        move_uploaded_file($tmp_img3,"../Images/prod_imgs/$p_img3");

        //insert query
        $insert_product = "insert into products (prod_title,prod_desc,prod_keyword,cat_id,prod_img1,prod_img2,prod_img3,date,status,price) 
        values('$prod_title','$desc','$kw','$prod_cat','$p_img1','$p_img2','$p_img3',NOW(),'$prod_status','$price')";

        $result_query=mysqli_query($con, $insert_product);
        if($result_query){
            echo "<script>alert('Successfully inserted product')</script>";
        }
    }
}
?>

And my database: enter image description here

I tried placing an echo before the else clause and it works, it's only on the else where it won't execute. All it does is redirect the current window to a blank page without the pop up or an error

1

There are 1 best solutions below

2
On

Apparently, the syntax needed to be different in the If statement;

if($prod_title==" or $desc==" or $kw==" or $prod_cat==" or $price=="" or $p_img1==" or $p_img2=="){
  echo "<script>alert('Please fill all the available fields')</script>";
  exit();