duangan2307 2016-07-02 00:51
浏览 50
已采纳

PHP - 更新后编辑元素消失了

Goodmorning. My problem is after clicking the update button the edit elements are gone like the title, date, content and the image only the echo output is shown "successfully updated". What i want to do is after clicking the update button the elements will stay there and it will echo there.

Here is my edit2.php enter image description here

after i click the update button only the output "Successfully update" is shown the edit elements is gone enter image description here

here is the php code for edit2.php

<?php

include_once('connection.php');

 $newsid = $_GET['news_id'];

    if(isset($_POST['esubmit'])){
        /* create a prepared statement */
        if ($stmt = mysqli_prepare($con, "SELECT * FROM news WHERE news_id = ? LIMIT 1")) {
            /* bind parameters */
            mysqli_stmt_bind_param($stmt, "s", $newsid);

            /* execute query */
            mysqli_stmt_execute($stmt);

            /* get the result set */
            $result = mysqli_stmt_get_result($stmt);

            /* fetch row from the result set */
            $row = mysqli_fetch_array($result);
        }

    }


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


        if($_FILES['image']['error'] == 0) {
          $image= addslashes(file_get_contents($_FILES['image']['tmp_name']));
          $image_name = addslashes($_FILES['image']['name']);
          move_uploaded_file($_FILES["image"]["tmp_name"],"img/" . $_FILES["image"]["name"]);
          $newsimage="img/" . $_FILES["image"]["name"];

          $title = $_POST['titles'];
          $date = $_POST['dates'];
          $content = $_POST['contents'];

          $sql ="UPDATE news SET news_title ='$title', news_date ='$date', news_content = '$content', news_image ='$newsimage' WHERE news_id = '$newsid'";
          mysqli_query($con, $sql);
          echo "successfully updated";
        }

        else{
          $title = $_POST['titles'];
          $date = $_POST['dates'];
          $content = $_POST['contents'];
          $sql ="UPDATE news SET news_title ='$title', news_date ='$date', news_content = '$content' WHERE news_id = '$newsid'";
          mysqli_query($con, $sql);
          echo "successfully updated";
        }

    }
?>
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>

<?php

    if(isset($_POST['esubmit'])){
        ?>

        <form method="post" enctype="multipart/form-data" action ="edit2.php?news_id=<?php echo $row['news_id']; ?>" >
            Title<input type ="text" name ="titles" value="<?php echo $row['news_title']; ?>"/><br>
            Date<input type ="text" name="dates" value="<?php echo $row['news_date']; ?>" /><br>
            Content<textarea name="contents"><?php echo $row['news_content']; ?></textarea>
            <input class="form-control" id="image" name="image" type="file" accept="image/*" onchange='AlertFilesize();'/>
            <img id="blah" src="<?php echo $row['news_image']; ?>" alt="your image" style="width:200px; height:140px;"/>

            <input type="submit" name="update" value="Update" />
        </form>

        <?php
    }

?>

<script src="js/jquery-1.12.4.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script type="text/javascript">
    function readURL(input) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();

            reader.onload = function (e) {
                $('#blah').attr('src', e.target.result);
            }

            reader.readAsDataURL(input.files[0]);
        }
    }

    $("#image").change(function(){
        readURL(this);
    });
    </script>
</body>
</html>
  • 写回答

1条回答 默认 最新

  • donglengli0644 2016-07-02 01:04
    关注

    The problem is because of this if block,

    if(isset($_POST['esubmit'])){ ...
    

    When you submit the form, $_POST['esubmit'] will be not get set and hence, the form won't get displayed again. So your if block should be like this:

    if(isset($_POST['esubmit']) || isset($_POST['update'])){ ...
    

    Overall, you need to change your first and third if blocks in the following way,

    if(isset($_POST['esubmit'])){
        /* create a prepared statement */
        if ($stmt = mysqli_prepare($con, "SELECT * FROM news WHERE news_id = ? LIMIT 1")) {
            /* bind parameters */
            mysqli_stmt_bind_param($stmt, "s", $newsid);
    
            /* execute query */
            mysqli_stmt_execute($stmt);
    
            /* get the result set */
            $result = mysqli_stmt_get_result($stmt);
    
            /* fetch row from the result set */
            $row = mysqli_fetch_array($result);
    
            /* get all values */
            $title = $row['news_title'];
            $date = $row['news_date'];;
            $content = $row['news_content'];
            $newsimage = $row['news_image'];
        }
    
    }
    

    And

    if(isset($_POST['esubmit']) || isset($_POST['update'])){
        ?>
    
            <form method="post" enctype="multipart/form-data" action ="edit2.php?news_id=<?php echo $newsid; ?>" >
                Title<input type ="text" name ="titles" value="<?php if(isset($title)){ echo $title; } ?>"/><br>
                Date<input type ="text" name="dates" value="<?php if(isset($date)){ echo $date; } ?>" /><br>
                Content<textarea name="contents"><?php if(isset($content)){ echo $content; } ?></textarea>
                <input class="form-control" id="image" name="image" type="file" accept="image/*" onchange='AlertFilesize();'/>
                <img id="blah" src="<?php if(isset($newsimage)){ echo $newsimage; } ?>" alt="your image" style="width:200px; height:140px;"/>
    
                <input type="submit" name="update" value="Update" />
            </form>
    
        <?php
    }
    

    From the extended discussion,

    Since you're making image upload optional, you need to fetch image details in the else block where you process the form in case user doesn't upload an image, like this:

    if(isset($_POST['update'])){
        if($_FILES['image']['error'] == 0) {
    
            // your code
    
        }else{
            // your code
    
            /* get the image details*/
            if ($stmt = mysqli_prepare($con, "SELECT news_image FROM news WHERE news_id = ? LIMIT 1")) {
                mysqli_stmt_bind_param($stmt, "s", $newsid);
                mysqli_stmt_execute($stmt);
                $result = mysqli_stmt_get_result($stmt);
                $row = mysqli_fetch_array($result);
                $newsimage = $row['news_image'];
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 php 将rtmp协议转hls协议,无法播放
  • ¥15 miniconda安装不了
  • ¥20 python代码编写
  • ¥20 使用MPI广播数据遇到阻塞
  • ¥15 TinyMCE如何去掉自动弹出的“链接…”工具?
  • ¥15 微信支付转账凭证,如何解决
  • ¥15 在win10下使用指纹登录时,界面上的文字最后一个字产生换行现象
  • ¥20 使用AT89C51微控制器和MAX7219驱动器来实现0到99秒的秒表计数,有开始和暂停以及复位功能,下面有仿真图,请根据仿真图来设计c语言程序
  • ¥15 51单片机 双路ad同步采样
  • ¥15 使用xdocreport 生成word