PHP - 无法使用获取的数据

时间:2016-07-01 03:49:26

标签: javascript php jquery html

你好编程的怪物,美好的一天!我想要做的是,当用户单击文件按钮并上传图像时它将更新,否则如果用户没有更改它只是回显该图像。但是当用户没有更改图像时,我有一个错误**Warning**: file_get_contents(): Filename cannot be empty in C:\xampp\htdocs\studentportal\edit2.php on line 27有人可以帮助我吗? if(isset($_FILES['image']))工作正常但else statement却没有。如果用户没有更改它,我该如何回显该图像?我是php新手并开始学习它,请给我一些想法。

这是错误导致else语句中的$newsimages = $row['news_image'];

else{
  $title = $_POST['titles'];
  $date = $_POST['dates'];
  $content = $_POST['contents'];
  $newsimages = $row['news_image'];
  $sql ="UPDATE news SET news_title ='$title', news_date ='$date', news_content = '$content', news_image ='$newsimages' WHERE news_id = '$newsid'";
  mysqli_query($con, $sql);
  echo "oh it worked again ";
}

这是所有的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(isset($_FILES['image'])){
          $file=$_FILES['image']['tmp_name'];
          $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 "oh it worked ";
        }
        else{
          $title = $_POST['titles'];
          $date = $_POST['dates'];
          $content = $_POST['contents'];
          $newsimages = $row['news_image'];
          $sql ="UPDATE news SET news_title ='$title', news_date ='$date', news_content = '$content', news_image ='$newsimages' WHERE news_id = '$newsid'";
          mysqli_query($con, $sql);
          echo "oh it worked again ";
        }

    }

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

<?php

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

        <form method="post" action ="edit2.php?news_id=<?php echo $row['news_id']; ?>" enctype="multipart/form-data">
            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 个答案:

答案 0 :(得分:1)

您可以执行的操作是从第一个表单中删除<img></img>,并将其放在单独的<form>单独的提交按钮中,并添加更多php代码以仅更新图片。您将有两种形式和两个更新,如

 $sql1 ="UPDATE news SET news_title ='$title', news_date ='$date', news_content = '$content' WHERE news_id = '$newsid'";
$sql2="update new SET new_image='$newsimages' WHERE new_id='$newsid'";

我希望你能不知道。我为我的一个网站做了同样的事情。我认为这是最好的解决方案。试试吧。如需进一步查询,你可以发表评论。