duanlan5320 2017-12-25 10:59
浏览 55

为什么图片在刷新页面上重复?

I am trying to make image show permanent on refreshing. The image should be constant. We can add images and text permanent and we can add more posts.

A database called image_upload and create a table called images with fields:

id - int(11)

image - varchar(100)

image_text - text

index.php

 <?php
  // Create database connection
 $db = mysqli_connect("localhost", "root", "", "image_upload");

      // Initialize message variable
      $msg = "";

    // If upload button is clicked ...
    if (isset($_POST['upload'])) {
        // Get image name
        $image = $_FILES['image']['name'];
        // Get text
        $image_text = mysqli_real_escape_string($db, $_POST['image_text']);

        // image file directory
    $target = "images/".basename($image);

    $sql = "INSERT INTO images (image, image_text) VALUES ('$image', '$image_text')";
        // execute query
        mysqli_query($db, $sql);

        if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
            $msg = "Image uploaded successfully";
        }else{
            $msg = "Failed to upload image";
        }
    }
    $result = mysqli_query($db, "SELECT * FROM images");
    ?>
    <!DOCTYPE html>
    <html>
    <head>
    <title>Image Upload</title>
    <style type="text/css">
        #content{
          width: 50%;
        margin: 20px auto;
        border: 1px solid #cbcbcb;
       }
       form{
        width: 50%;
        margin: 20px auto;
       }
       form div{
        margin-top: 5px;
       }
       #img_div{
        width: 80%;
        padding: 5px;
        margin: 15px auto;
        border: 1px solid #cbcbcb;
       }
       #img_div:after{
        content: "";
        display: block;
        clear: both;
       }
       img{
        float: left;
        margin: 5px;
        width: 300px;
        height: 140px;
       }
    </style>
    </head>
    <body>
    <div id="content">
      <?php
      while ($row = mysqli_fetch_array($result)) {
        echo "<div id='img_div'>";
            echo "<img src='images/".$row['image']."' >";
            echo "<p>".$row['image_text']."</p>";
        echo "</div>";
      }
    ?>
      <form method="POST" action="index.php" enctype="multipart/form-data">
        <input type="hidden" name="size" value="1000000">
        <div>
          <input type="file" name="image">
        </div>
        <div>
          <textarea 
            id="text" 
            cols="40" 
            rows="4" 
            name="image_text" 
            placeholder="Say something about this image..."></textarea>
        </div>
        <div>
            <button type="submit" name="upload">POST</button>
        </div>
      </form>
    </div>
    </body>
    </html>

Help me How to solve this problem. I have created database and uploading images and text. while refreshing page. The posts is repeating after and after.

  • 写回答

2条回答 默认 最新

  • doutui2016 2017-12-25 11:38
    关注

    It now works. Tested. For over two hours I was baffled why the page could not redirect after successful upload. Undoing some indentations at the top of the page solved the problem.

     <?php
     session_start();
    
     // Create database connection
     $db = mysqli_connect("localhost", "root", "", "image_upload");
    
     if(isset($_POST['image_text'])){
    
        // Get text
        $image_text = mysqli_real_escape_string($db, $_POST['image_text']);     
    
     }
    
     if(isset($_FILES['image'])){
        // Get image name
        $image = $_FILES['image']['name'];
        $tempath = $_FILES['image']['tmp_name'];     
     }
    
     // If upload button is clicked ...
     if(isset($_POST['upload'])){
    
         if(isset($image) && isset($tempath) && !empty($image)){
    
             $image = time()."_".$image;
             $image_folder = "images/";
    
             if(is_uploaded_file($tempath)){ 
    
                 if(move_uploaded_file($tempath, $image_folder.$image)) {
    
                     $image_uploaded = true;    
    
                     }
    
                }
            }else{
    
                $msg = "Failed to upload image";
    
            }
    
    
         if(isset($image_uploaded) && ($image_uploaded == true)){
    
                $sql = "INSERT INTO images (image, image_text) VALUES('$image', '$image_text')";
                // execute query
                mysqli_query($db, $sql);
    
                $sent = true;
    
                $_SESSION['sent'] = "Image uploaded successfully";
    
            }
        }
    
    
        if(isset($sent) && ($sent == true)){
    
                header("Location: index.php");
                exit();
    
        }
    
    
      $result = mysqli_query($db, "SELECT * FROM images");
    
    ?>
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <title>Image Upload</title>
    
    <style type="text/css">
        #content{
          width: 50%;
        margin: 20px auto;
        border: 1px solid #cbcbcb;
       }
       form{
        width: 50%;
        margin: 20px auto;
       }
       form div{
        margin-top: 5px;
       }
       #img_div{
        width: 80%;
        padding: 5px;
        margin: 15px auto;
        border: 1px solid #cbcbcb;
       }
       #img_div:after{
        content: "";
        display: block;
        clear: both;
       }
       img{
        float: left;
        margin: 5px;
        width: 300px;
        height: 140px;
       }
    </style>
    </head>
    <body>
    <div id="content">
      <?php
        if(isset($_SESSION['sent'])){
    
         echo "<p style='color:#006400;font-weight:bold;'>" . $_SESSION['sent'] . "</p>";
         unset($_SESSION["sent"]);      
    
        }
    
        if(isset($msg)){
    
        echo "<p style='color:#ff0000;font-weight:bold;'>" . $msg . "</p>";
    
        }
    
      if(mysqli_num_rows($result) > 0){
    
         while($row = mysqli_fetch_array($result)){
            echo "<div id='img_div'>";
            echo "<img src='images/".$row['image']."' >";
            echo "<p>".$row['image_text']."</p>";
            echo "</div>";
         }
    
      }else{
    
             echo "<h4>No data has been uploaded</h4>";
    
      }
    ?>
      <form method="POST" action="index.php" enctype="multipart/form-data">
        <input type="hidden" name="size" value="1000000">
        <div>
          <input type="file" name="image">
        </div>
        <div>
          <textarea 
            id="text" 
            cols="40" 
            rows="4" 
            name="image_text" 
            placeholder="Say something about this image..."></textarea>
        </div>
        <div>
            <button type="submit" name="upload">POST</button>
        </div>
      </form>
    </div>
    </body>
    </html>
    
    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题