Loading ...
Sorry, an error occurred while loading the content.

15684How to update image in mysql using php

Expand Messages
  • metysoriano
    Apr 23, 2014
      Hello experts I am uploading an image in my mysql, the problem I can not modify or change image. The uploading stage is success but in updating image is does not.

      below is my codes

      THIS WORKS (UPLOADING)..

      <input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input name="userfile" type="file" style="font-size:18px" />

      </font>

      <?php

      // check if a file was submitted
      if(!isset($_FILES['userfile']))
      {
          echo '<p>(Please select a file to upload)</p>';
      }
      else
      {
          try {
          $msg= upload();  //this will upload your image
          echo $msg;  //Message showing success or failure.
          }
          catch(Exception $e) {
          echo $e->getMessage();
          echo 'Sorry, could not upload file';
          }
      }

      // the upload function

      function upload() {

      $title = $_POST['title'];
      $barcode = $_POST['barcode'];
      $summary = $_POST['summary'];
      $type = $_POST['type'];
      $class_no = $_POST['class_no'];
      $access_no = $_POST['access_no'];
      $artist = $_POST['artist'];
      $pub_comp = $_POST['pub_comp'];
      $place_pub = $_POST['place_pub'];
      $copyright = $_POST['copyright'];
      $subject = $_POST['subject'];
      $date_acq = $_POST['date_acq'];
      $price = $_POST['price'];
      $status = 'AVAILABLE';




      mysql_select_db(avr,$mysqlconnect) or die ("Can not connect, Problem!");
      /*
      $metyquery = "SELECT * FROM news_acq WHERE course = 'BEED' ORDER BY rec_no DESC";
      */

          $maxsize = 10000000; //set to approx 10 MB

          //check associated error code
          if($_FILES['userfile']['error']==UPLOAD_ERR_OK) {

              //check whether file is uploaded with HTTP POST
              if(is_uploaded_file($_FILES['userfile']['tmp_name'])) {   

                  //checks size of uploaded image on server side
                  if( $_FILES['userfile']['size'] < $maxsize) { 
       
                     //checks whether uploaded file is of image type
                    //if(strpos(mime_content_type($_FILES['userfile']['tmp_name']),"image")===0) {
                     //  $finfo = finfo_open(FILEINFO_MIME_TYPE);
                     // if(strpos(finfo_file($finfo, $_FILES['userfile']['tmp_name']),"image")===0) {   

                          // prepare the image for insertion
                          $imgData = addslashes (file_get_contents($_FILES['userfile']['tmp_name']));
                          // database connection

      mysql_select_db(avr,$mysqlconnect) or die ("Can not connect, Problem!");

                          // our sql query
                          $sql = "INSERT INTO new_acq (barcode, image, title, summary, name, type, class_no, access_no, artist, pub_comp, place_pub, copyright, subject, date_acq, price, status)
                          VALUES ('$barcode','{$imgData}', '$title','$summary','{$_FILES['userfile']['name']}','$type',  '$class_no', '$access_no', '$artist', '$pub_comp', '$place_pub', '$copyright', '$subject', '$date_acq', '$price', '$status');";

                          // insert the image
                          mysql_query($sql) or die("Error in Query: " . mysql_error());
                          $msg='<p>Record Successfuly Save with ID # <strong> = '. mysql_insert_id().' </strong></p>';
                      }
                      else
                          $msg="<p>Uploaded file is not an image.</p>";
                  }
         
          return $msg;
      }


      ======================

      UPDATE DOES NOT WORK (UPDATING)


      I JUST CHANGE THE SQL STATEMENT INTO UPDATE COMMAND BUT IT DOES NOT WORK


      $open_record = ("UPDATE new_acq SET barcode = '$barcode', title = '$title', summary = '$summary', type = '$type', image = '{$imgData}', name = '{$_FILES['userfile']['name']}', class_no = '$class_no' WHERE rec_no = ".$_SESSION['rec_no']);


    • Show all 2 messages in this topic