更新标题和数据库中的其他内容图像时变为空

时间:2017-12-16 12:00:54

标签: php

require('connection.php');
session_start();
if(isset($_POST['submit'])) {

  if(!empty($_FILES["image"]["name"])) {

  $_FILES["image"]["name"];
  $imagename = $_FILES["image"]["tmp_name"];
  move_uploaded_file($imagename ,"uploads/" . $_FILES["image"]["name"]);    
  $image= $_FILES['image']['name'];
  $name1=$_POST['name'];
  $description=$_POST['description'];

  } else {

  $image= $_FILES['image']['name'];
  $name1=$_POST['name'];
  $description=$_POST['description'];
  }

$sql=mysqli_query($con,"UPDATE `service` SET `name` ='$name1' ,`description` ='$description',`image`='".$image."'  WHERE `service_id`= '". $_SESSION["id"]."' ");

}

if($sql>0)
{
  echo "<script>alert('Successfully Entered!!!'); 
  window.location='service_view.php'</script>";
}
?>

上下文: 这是代码用于更新数据库中的内容(标题,描述和图像)。代码在选择所有字段时工作。编辑由模态框编辑完成。代码在分别选择字段时工作。例如,如果我选择所有三个字段代码正常工作并且每个内容都更新。但是当我选择标题字段时只更新存储在数据库中的图像变为空。

1 个答案:

答案 0 :(得分:0)

最简单的方法是在每个条件上创建单独的查询:

...
if(isset($_POST['submit'])) 
{   
    if(!empty($_FILES["image"]["name"]))
    {
        $_FILES["image"]["name"];
        $imagename = $_FILES["image"]["tmp_name"];
        move_uploaded_file($imagename ,"uploads/" . $_FILES["image"]["name"]);  
        $image= $_FILES['image']['name'];
        $name1=$_POST['name'];
        $description=$_POST['description'];
        $sql=mysqli_query($con,"UPDATE `service` SET `name` ='$name1' ,`description` ='$description',`image`='".$image."'  WHERE `service_id`= '". $_SESSION["id"]."' ");
    }
    else
    {
        $name1=$_POST['name'];
        $description=$_POST['description'];
        $sql=mysqli_query($con,"UPDATE `service` SET `name` ='$name1' ,`description` ='$description'  WHERE `service_id`= '". $_SESSION["id"]."' ");
    }
}
...