简体   繁体   English

PHP-无法使用提取的数据

[英]PHP - Can't use the fetched data

Hello monsters of programming, Good day! 编程的怪物,大家好! What i want to do is, when the user click the file button and upload the image it will update, else if the user didn't change it just echo that image. 我想做的是,当用户单击文件按钮并上传图像时,它将更新,否则,如果用户未更改,则只需回显该图像。 But when the user didn't change the image, im having an error **Warning**: file_get_contents(): Filename cannot be empty in C:\\xampp\\htdocs\\studentportal\\edit2.php on line 27 Can someone help me? 但是,当用户没有更改图像时,即时通讯会出现错误**Warning**: file_get_contents(): Filename cannot be empty in C:\\xampp\\htdocs\\studentportal\\edit2.php on line 27有人可以帮助我吗? The if(isset($_FILES['image'])) is working properly but the else statement is not. if(isset($_FILES['image']))可以正常工作,但else statement不能正常工作。 How can i just echo that image if the user didn't change it? 如果用户没有更改图像,我该怎么回声? Im new to php and starting to learn it please give me ideas. 我是php的新手,开始学习它时请给我一些想法。

this is cause of the error the $newsimages = $row['news_image']; 这是错误的原因。 $newsimages = $row['news_image']; in the else statement. 在else语句中。

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 ";
}

this is all of the php code 这是所有的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>

What you can do is remove <img></img> out of the first form and put it in separate <form> with separate submit button and add more php code to just update image only.You will have two forms and two updates like 您可以做的是从第一个表单中删除<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'";

I Hope You undestand.I have done same thing for one of my websites.I think this is the best solution.Try it.For further query you can comment. 希望您能理解。我为我的一个网站做了同样的事情。我认为这是最好的解决方案。尝试一下。有关进一步的查询,您可以发表评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM