在数据库中单独更新多个图像

时间:2016-11-10 04:39:29

标签: php html mysql

请帮帮我!我在几天的代码中遇到困难,我需要单独更新图像并将路径保存在数据库中。当我提交时,只上传了一张图片并更新了我的所有图片。这是代码:

<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>

抱歉我的英文。谢谢!

1 个答案:

答案 0 :(得分:0)

嗯,我在这里有点困惑。

我发现了一些错误的代码实现:

  1. 您的表单提交是POST方法,但您尝试从GET方法获取变量'id'值。
  2. 您有sql sytax来获取具有特定ID的所有产品,但您不检查ID是否已设置:

    $ sql = $ mysqli_conn-&gt; query(“SELECT * FROM products_code WHERE id ='”。$ _ GET ['id']。“'”);

  3. 我的建议是尝试为您的ID构建隐藏输入,然后通过调用$ _POST ['id']

    获取其值