title: Updating / editing images in a database using PHP & MySQL
body: <p>I'm creating backend pages for my site and I want to edit images that are already existing in my database using input control type "file".</p>
  
  <p>But when I choose a new image it isn't modified in the database and moving up in its folder.</p>
  
  <p>This is my code:</p>
  
  <pre><code>&lt;?php 
  $db = new MySQLi(server,user,password,database);
  //other function to show data 
  mysqli_query($db,'SET NAMES "utf8" COLLATE "utf8_general_ci"');
  if ($db-&gt;connect_errno &gt; 0) {
      die ("Failed to connect to database".$db-&gt;connect_error);
  } 
  
  $id = $_GET["id"];
  $news_select = "select news_id,title,content,image,news_date
      from news_slider 
      where news_id=$id";
  if (!$news_result=$db-&gt;query($news_select)) {
      die($db-&gt;error);
  }
  ?&gt;
  
  &lt;!--wrapper start --&gt;
  &lt;div id="wrapper"&gt;
      &lt;!-- u data --&gt;
      &lt;div id="u_data"&gt;
          &lt;?php 
          while ($row=$news_result-&gt;fetch_assoc()) { 
              ?&gt;
              &lt;img id="picture" src="../../images/slider_images/&lt;?= $row["image"]; ?&gt;"&gt;
              &lt;h3 class="id"&gt;News id:&lt;/h3&gt; 
              &lt;h3 class="id"&gt;Title:&lt;/h3&gt;      
              &lt;h3 class="id"&gt;Content:&lt;/h3&gt;    
              &lt;h3 class="id"&gt;News_date : &lt;/h3&gt;  
  
              &lt;ul id="data" &gt;
                  &lt;li class="data"&gt;&lt;h4&gt;&lt;?php echo $row["news_id"] ?&gt;&lt;/h4&gt; &lt;/li&gt;
                  &lt;li class="data"&gt;&lt;h4&gt;&lt;?php echo $row["title"] ?&gt;&lt;/h4&gt; &lt;/li&gt;
                  &lt;li class="data" style="height::10%"&gt;&lt;h4&gt;&lt;?php echo $row["content"] ?&gt;&lt;/h4&gt;&lt;/li&gt;
                  &lt;li class="data"&gt;&lt;h4&gt;&lt;?php echo $row["news_date"] ?&gt;&lt;/h4&gt; &lt;/li&gt;
              &lt;/ul&gt;
              &lt;?php 
          }
          ?&gt;
      &lt;/div&gt;
      &lt;!-- udata end  --&gt;
  </code></pre>
  
  <p>This my form to change and edit the database. I will display just the question-related part:</p>
  
  <pre><code>&lt;form name="edit_news" action="&lt;?php echo htmlspecialchars($_SERVER["PHP_SELF"])?&gt;" method="get" enctype="multipart/form-data"&gt;    
      &lt;h3&gt; change pic &lt;/h3&gt; &lt;input type="file" name="pic" id="pic" /&gt;
      &lt;input type="hidden" name="id" value="&lt;?php echo $id ;?&gt;" /&gt;
  
      &lt;!-- div submit start--&gt;
      &lt;div style="margin:10px 0 0 0" &gt;
          &lt;input type="submit" name="go" value="  GO  "&gt;
          &lt;input type="reset" bame="back" value=" BACK "&gt;
      &lt;/div&gt;
      &lt;!--div submit end --&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>This part of code changes the images in the database:</p>
  
  <pre><code>&lt;?php 
  if ($_SERVER["REQUEST_METHOD"]== 'GET') {
      //change imge file news
      if (isset($_FILES["pic"]) &amp;&amp; !empty($_FILES["pic"])) {
          //difneshion variables to file uploaded 
          //name of file + exetension 
          $imgtype=pathinfo($_FILES["pic"]["name"],PATHINFO_EXTENSION);
          //echo $imgtype."&lt;br /&gt;";
          //root directory for file 
          $path="../../images/slider_images/";
          //random name 
          $name=mt_rand(1000,1000000);
          $target=$path.$name.".".$imgtype;
          //image name to type it in DB
          $imageName=$name.".".$imgtype;
  
          if ($imgtype !='jpg' &amp;&amp; $imgtype !='png') {
              echo "invalid file type";
          } else if($_FILES["pic"]["size"] &gt; 2000000) {
              echo "invalid file size";
          }
          /* sles of transfer file uploaded start*/
          //transfer file to folder profile in site root and type the name in DB
          else {
              /* if statement of move file start */
              if (move_uploaded_file($_FILES["pic"]["tmp_name"],$target)) {
                  //update imge file
                  $imge_update="update news_slider set image='$imageName' where news_id=$id";
                  if(!$imge_result=$db-&gt;query($imge_update)) {
                      die($db-&gt;error); 
                  } else {
                  //header("location:../../bookstore/bookstore_en/news_control.php");
                       echo "done!!";   
                  }
              }//end of if move uploaded
          }//end of else
      }//end of change imge news
  }// end of if REQUEST
  </code></pre>