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 组策略中的计算机配置策略无法下发
  • ¥15 机器学习简单问题解决
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写