2016-07-01 55 views
0

你好編程的怪物,美好的一天!我想要做的是,當用戶點擊文件按鈕並上傳圖片時,它會更新,否則,如果用戶沒有更改它只是回顯該圖片。但是,當用戶沒有改變圖像,即時通訊有錯誤**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,並開始學習它,請給我的想法。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> 
+0

你不想檢查'isset($ _ FILES ['image'])'因爲默認情況下當你提交表單時它將被設置。您可能希望檢查數組$ _FILES ['image'] ['error']'中的值的狀態。 – Rasclatt

+0

也不要這樣做,''更新新聞SET news_title ='$ title',news_date ='$ date',news_content ='$ content',news_image ='$ newsimage'WHERE news_id ='$ newsid'「'it很容易發生SQL注入。任何包含在'$ _POST'中的字符都可能會讓你的sql語句變得糟糕。處理用戶提交的輸入時使用綁定參數。 – Rasclatt

+0

注意,如果你在'if'和'else'中都有相同的行,只需刪除一個集合,並將其他集合移出'if'和'else',因爲事件發生在兩種情況下。 'if'和'else'中只有不同的/改變的元素。保存幾行。 – Rasclatt

回答

1

你可以做的是去除<img></img>出第一種形式,並把它放在單獨的<form>有獨立的提交按鈕,添加更多php代碼只是更新圖像only.You將有兩種形式和兩個更新像

$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'"; 

我希望你undestand.I做了同樣的事情之一我的網站。我認爲這是最好的解決方案。試試吧。如需進一步查詢,您可以發表評論。