I want to update the image fields only if the user's input is not NULL. I'm trying to do it with coalesce but it doesn't work. I've tried to insert the query manually by replacing the $image and $nameimg fields with NULL and the other fields with random value and it worked. Can someone please help me?
Here's my code:
if(isset($_POST["mod_name"]))
{
$id_mod=$_POST["mod_id"];
$name=mysqli_real_escape_string($conn,$_POST["mod_name"]);
$description=mysqli_real_escape_string($conn,$_POST["mod_description"]);
$price=$_POST["mod_price"];
$category=mysqli_real_escape_string($conn,$_POST["mod_category"]);
if($_FILES['mod_image1']['size'] == 0)
{
$image1=addslashes(file_get_contents($_FILES["mod_image1"]["tmp_name"]));
$nameimg1=basename($_FILES["mod_image1"]["name"]);
}
elseif($_FILES['mod_image1']['size'] != 0)
{
$image1=NULL;
$nameimg1=NULL;
}
if($_FILES['mod_image2']['size'] == 0)
{
$image2=addslashes(file_get_contents($_FILES["mod_image2"]["tmp_name"]));
$nameimg2=basename($_FILES["mod_image2"]["name"]);
}
elseif($_FILES['mod_image2']['size'] != 0)
{
$image2=NULL;
$nameimg2=NULL;
}
if($_FILES['mod_image3']['size'] == 0)
{
$image3=addslashes(file_get_contents($_FILES["mod_image3"]["tmp_name"]));
$nameimg3=basename($_FILES["mod_image3"]["name"]);
}
elseif($_FILES['mod_image3']['size'] != 0)
{
$image3=NULL;
$nameimg3=NULL;
}
mysqli_query($conn,"UPDATE product SET Product = '$name', Description = '$description', Price = '$price', Category = '$category', Image = COALESCE($image1, Image), Image_name = COALESCE($nameimg1, Image_name), Image_2 = COALESCE($image2, Image_2), Image_name_2 = COALESCE($nameimg2, Image_name_2), Image_3 = COALESCE($image3, Image_3), Image_name_3 = COALESCE($nameimg3, Image_name_3) WHERE ID_product = '$id_mod'");
}
Thank you!