1

Please help me! I am stuck from couple of days on a code where I need to update separately images and save the path in a database. When I submit only one image is uploaded and update all the images I have with this one. Here's the code:

<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
    <fieldset class="yui">
<?php
        include 'php/config.inc.php';

      if(isset($_POST['submit'])){

         $sql = $mysqli_conn->query("SELECT * FROM products_code WHERE id='".$_GET['id']."'");

           while($row = $sql->fetch_assoc()) {
           $code = $row["product_code"];

          for($i=0;$i<count($_FILES["fileUpload"]["name"]);$i++)  
           {  
           if(trim($_FILES["fileUpload"]["tmp_name"][$i]) != "")  
           { 

                  $imagename = $_FILES["fileUpload"]["name"][$i];
                  $source = $_FILES["fileUpload"]["tmp_name"][$i];



                  $imagepath = $imagename;
                  $save = "images/thmb/" . $imagepath; //This is the new file you saving
                  $orig = "images/" . $imagepath; //This is the original file
                  $result = move_uploaded_file($source, $orig);
                  $file = "images/" . $imagepath; //This is the original file

                  list($width, $height) = getimagesize($file);

                  if($width > $height){

                  $newwidth=600;
                  $newheight=($height/$width)*$newwidth;

                  $newwidth1=250;
                  $newheight1=($height/$width)*$newwidth1;

                  } else {


                 $newheight=800;
                 $newwidth=($width/$height)*$newheight;


                 $newheight1=250;
                 $newwidth1=($width/$height)*$newheight1;

                 }
       $tmp=imagecreatetruecolor($newwidth,$newheight);
       $tmp1=imagecreatetruecolor($newwidth1,$newheight1);
       $image = imagecreatefromjpeg($file) ;
       imagecopyresampled($tmp, $image, 0, 0, 0, 0, $newwidth, $newheight, $width, $height) ;
       imagejpeg($tmp, $file, 100) ;
       imagecopyresampled($tmp1, $image, 0, 0, 0, 0, $newwidth1, $newheight1, $width, $height) ;
       imagejpeg($tmp1, $save, 100) ;

        $sql3 = $mysqli_conn->query("SELECT * FROM photos WHERE product_code = '$code' LIMIT 0,9999");

          while($row3 = $sql3->fetch_assoc()) {

            $id = $row3["id"];
            $result = $mysqli_conn->query("UPDATE photos SET thumb = '$save', full = '$file' WHERE product_code = '$code' AND id = '$id'");

           }
              echo "<div class='form'>Mulţumesc! Datele au fost modificate!<br>Veţi fi redirecţionat în (4) secunde</div>";

              echo "<meta http-equiv=Refresh content=4;url=adminprod.php>";
           }
          }
         }
        }

  elseif(isset($_GET['id']))

        {
        $sql1 = $mysqli_conn->query("SELECT * FROM products_code WHERE id='".$_GET['id']."'");

          while($row1 = $sql1->fetch_assoc()) {
           $id = $row1["id"];
           $code=$row1["product_code"];

        $sql2 = $mysqli_conn->query("SELECT * FROM photos WHERE product_code = '$code' LIMIT 0,9999");
        $numRows2 = $sql2->num_rows;

           while($row2 = $sql2->fetch_assoc()) {
             $code = $row2['product_code']; 
             $thumb = $row2['thumb'];

        $divcount = 1; 
        if ($divcount == 1)  
          echo '<div><input type="file" name="fileUpload[]" class="image" value="'.$row2['full'].'" multiple/><img src="'.$row2['thumb'].'" style="width:auto; height:150px;" border=0/></a></div>'; // display as you like 
        if ($divcount == $numRows2) {  
          $divcount = 1; 
        } else { 
          $divcount++; 
        }
       }
      }
     }
?>

    <button name="submit" type="submit" class="button">Modificați</button>
    </fieldset>
    </form>

Sorry for my english. Thank you!

Sorin David
  • 19
  • 1
  • 3
  • Don't save files in a database. http://stackoverflow.com/a/38829952/267540 – e4c5 Nov 10 '16 at 04:41
  • I save only the path to images. Images themselves are stored on a folder. – Sorin David Nov 10 '16 at 04:45
  • Then perhaps your question ought to be changed? – e4c5 Nov 10 '16 at 04:47
  • "I need to update separately images in a database. " what does that tell us that you have images in a database – e4c5 Nov 10 '16 at 04:47
  • please indent your code (it's completely unreadable at present) and please explain what you want to achieve – e4c5 Nov 10 '16 at 04:48
  • I said that I'm sorry from my english. I tried to explain at the best but seems I fail to succeed. I have images in a folder on the server and store the path from them in a database. With this code I want to update multiple images witch are assigned to one product, but each one separately. In this stage of code when I upload an image, every images assigned to that product change to this one. I hope that, for now, I explained more clear. I don't know how to indent my code, I only post it the same way that I have in my editor. Sorry and thank you. – Sorin David Nov 10 '16 at 05:01
  • English is not my first language either. If you are clarifying the question. Please edit it instead of posting comments. Lack of knowledge of english isn't an excuse for not formatting your code. – e4c5 Nov 10 '16 at 05:03

2 Answers2

0

are you sure your syntax is ok? you should use something like this :

$sql3 = $mysqli_conn->query("SELECT * FROM photos WHERE product_code = '".$code."' LIMIT 0,9999");
farhadamjady
  • 982
  • 6
  • 14
  • are you sure the "$_FILES["fileUpload"]["name"]" return all images that you uploaded to the server? I think your loop rotate only one time – farhadamjady Nov 10 '16 at 05:44
  • And how can I change that? I don't know. – Sorin David Nov 10 '16 at 05:50
  • echo $_FILES["fileUpload"] and see what returns to server! I think you should remove name property in for loop – farhadamjady Nov 10 '16 at 05:59
  • I tried echo $_FILES["fileUpload"] and gives me response : Array. I don't know what it means. – Sorin David Nov 10 '16 at 06:14
  • use print_r($_FILES["fileUpload"]) function instead of echo – farhadamjady Nov 10 '16 at 06:17
  • It gives me something like this when I upload an image : Array ( [name] => Array ( [0] => [1] => Colorado_Premium_conuri.jpg [2] => ) [type] => Array ( [0] => [1] => image/jpeg [2] => ) [tmp_name] => Array ( [0] => [1] => C:\wamp\tmp\php1188.tmp [2] => ) [error] => Array ( [0] => 4 [1] => 0 [2] => 4 ) [size] => Array ( [0] => 0 [1] => 193382 [2] => 0 ) ). – Sorin David Nov 10 '16 at 06:32
0

Uhm, i have a little confused here.

There were some false code implementation i have found:

  1. Your form submission is POST Method, but you try to get variable 'id' value from GET method.
  2. You have sql sytax to get all products that have specific id, but you don't check the id is set or not:

    $sql = $mysqli_conn->query("SELECT * FROM products_code WHERE id='".$_GET['id']."'");

My suggestion is try to build hidden input for your id, then get its value by call $_POST['id']

  • I call GET method because I must to get id for the product from the previous edit product page witch has the product code to report and extract images that belong to them . – Sorin David Nov 10 '16 at 08:34
  • Yep, that's the problem you must concern. Your request submission is POST, so you must have variable id on your $_POST variable. – Hafidz Jazuli Luthfi Nov 11 '16 at 10:18