Witam,

Potrzebuje pomocy w stworzeniu zapytania do bazy, poniżej kod. Obecnie kod dodaje do bazy kolejne zdjęcie, macie pomysł jak zmodyfikować zapytanie tak aby skrypt aktualizował rekord o konkretnym id.

  1. <form method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" enctype="multipart/form-data">
  2. Select Image File:
  3. <input type="file" name="userfile" size="40">
  4. <input type="hidden" name="MAX_FILE_SIZE" value="10000000">
  5. <select name="image_ctgy">
  6. <option value="animals">Animals</option>
  7. <option value="vegetables">Vegetables</option>
  8. <option value="minerals">Minerals</option>
  9. <br />
  10. <input type="submit" value="submit">
  11. </form>


  1. <?php
  2.  
  3. /*** check if a file was submitted ***/
  4. if(!isset($_FILES['userfile'], $_POST['image_ctgy']))
  5.    {
  6.    echo '<p>Please select a file</p>';
  7.    }
  8. else
  9.    {
  10.    try {
  11.        upload();
  12.        /*** give praise and thanks to the php gods ***/
  13.        echo '<p>Thank you for submitting</p>';
  14.        }
  15.    catch(PDOException $e)
  16.        {
  17.    echo '<h4>'.$e->getMessage().'</h4>';
  18.        }
  19.    catch(Exception $e)
  20.        {
  21.        echo '<h4>'.$e->getMessage().'</h4>';
  22.        }
  23.    }
  24.  
  25.  
  26. /**
  27.  *
  28.  * the upload function
  29.  *
  30.  * @access public
  31.  *
  32.  * @return void
  33.  *
  34.  */
  35. function upload(){
  36. /*** check if a file was uploaded ***/
  37. if(is_uploaded_file($_FILES['userfile']['tmp_name']) && getimagesize($_FILES['userfile']['tmp_name']) != false)
  38.    {
  39.    /*** an array of allowed categories ***/
  40.    $cat_array = array("animals", "vegetables", "minerals");
  41.    if(filter_has_var(INPUT_POST, "notset") !== false || in_array($_POST['image_ctgy'], $cat_array) !== false)
  42.        {
  43.        $image_ctgy = filter_input(INPUT_POST, "image_ctgy", FILTER_SANITIZE_STRING);
  44.        }
  45.    else
  46.        {
  47.        throw new Exception("Invalid Category");
  48.        }
  49.    /***  get the image info. ***/
  50.    $size = getimagesize($_FILES['userfile']['tmp_name']);
  51.  
  52.    /*** assign our variables ***/
  53.    $image_type   = $size['mime'];
  54.    $imgfp        = fopen($_FILES['userfile']['tmp_name'], 'rb');
  55.    $image_width  = $size[0];
  56.    $image_height = $size[1];
  57.    $image_size   = $size[3];
  58.    $image_name   = $_FILES['userfile']['name'];
  59.    $maxsize      = 99999999;
  60.  
  61.    /***  check the file is less than the maximum file size ***/
  62.    if($_FILES['userfile']['size'] < $maxsize )
  63.        {
  64.        /*** create a second variable for the thumbnail ***/
  65.        $thumb_data = $_FILES['userfile']['tmp_name'];
  66.  
  67.        /*** get the aspect ratio (height / width) ***/
  68.        $aspectRatio=(float)($size[0] / $size[1]);
  69.  
  70.        /*** the height of the thumbnail ***/
  71.        $thumb_height = 100;
  72.  
  73.        /*** the thumb width is the thumb height/aspectratio ***/
  74.        $thumb_width = $thumb_height * $aspectRatio;
  75.  
  76.        /***  get the image source ***/
  77.        $src = ImageCreateFromjpeg($thumb_data);
  78.  
  79.        /*** create the destination image ***/
  80.        $destImage = ImageCreateTrueColor($thumb_width, $thumb_height);
  81.  
  82.        /*** copy and resize the src image to the dest image ***/
  83.        ImageCopyResampled($destImage, $src, 0,0,0,0, $thumb_width, $thumb_height, $size[0], $size[1]);
  84.  
  85.        /*** start output buffering ***/
  86.        ob_start();
  87.  
  88.        /***  export the image ***/
  89.        imageJPEG($destImage);
  90.  
  91.        /*** stick the image content in a variable ***/
  92.        $image_thumb = ob_get_contents();
  93.  
  94.        /*** clean up a little ***/
  95.        ob_end_clean();
  96.  
  97.        /*** connect to db ***/
  98.        $dbh = new PDO("mysql:host=localhost;dbname=db", 'user', 'pass');
  99.  
  100.        /*** set the error mode ***/
  101.        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  102.  
  103.        /*** prepare the sql ***/
  104.        $stmt = $dbh->prepare("INSERT INTO tblob (image_type ,image, image_height, image_width, image_thumb, thumb_height, thumb_width, image_ctgy, image_name)
  105.        VALUES (? ,?, ?, ?, ?, ?, ?, ?, ?)");
  106.        $stmt->bindParam(1, $image_type);
  107.        $stmt->bindParam(2, $imgfp, PDO::PARAM_LOB);
  108.        $stmt->bindParam(3, $image_height, PDO::PARAM_INT);
  109.        $stmt->bindParam(4, $image_width,  PDO::PARAM_INT);
  110.        $stmt->bindParam(5, $image_thumb,  PDO::PARAM_LOB);
  111.        $stmt->bindParam(6, $thumb_height, PDO::PARAM_INT);
  112.        $stmt->bindParam(7, $thumb_width,  PDO::PARAM_INT);
  113.        $stmt->bindParam(8, $image_ctgy);
  114.        $stmt->bindParam(9, $image_name);
  115.  
  116.        /*** execute the query ***/
  117.        $stmt->execute();
  118.        }
  119.    else
  120.        {
  121.    /*** throw an exception is image is not of type ***/
  122.    throw new Exception("File Size Error");
  123.        }
  124.    }
  125. else
  126.    {
  127.    // if the file is not less than the maximum allowed, print an error
  128.    throw new Exception("Unsupported Image Format!");
  129.    }
  130. }
  131. ?>


Z góry dzięki za pomoc.