2017-08-25 90 views
0

我已經創建了一個網站,可以上傳文章,但是我無法更新已創建的文章。我已經成功地填寫了一個表格,其中填寫了預先填寫好的數據庫中的信息,但是當我提交任何更改後再轉到文章中時,它不會更新。無法更新PHP中的記錄

$ var_value是從上一頁傳遞的主鍵,用於確定要加載的文章&編輯。

這是我的表單來更新文章。

<?php 

     $var_value = $_POST['varname']; 

     $get_studies = "select * from news where `news`.`articleID` = $var_value"; 

     $run_studies = mysqli_query($con,$get_studies); 

     while($row_studies = mysqli_fetch_array($run_studies)){ 


      $newsTitle = $row_studies['title']; 
      $newsDate = $row_studies['date']; 
      $shortBio = $row_studies['shortBio']; 
      $longBio = $row_studies['longBio']; 
      $longBio2 = $row_studies['longBio2']; 
      $image = $row_studies['image']; 
     } 

       echo " 

        <div class='panelContent1' id='addNewsWraper'> 

         <h2>Dashboard</h2> 
         <h3>Update Article</h3> 

         <form method='post' enctype='multipart/form-data' onsubmit='alert('stop submit'); return false;' > 

          <div class='newsForm'> 

           <p>Article Title<br /><input type='text' value='$newsTitle' name='newsTitle' /></p> 
           <p>Short Description<br /><textarea name='newsShort' placeholder='Around a paragraph' />$shortBio</textarea> 
           <p>Image<br /><input type='file' name='newsImage' /></p> 
          </div> 
          <div class='newsForm'> 
           <p>Date<br /><input type='text' value='$newsDate' name='newsDate' placeholder='2017' /></p> 
           <p>Story<br /><textarea name='newsLong' placeholder='News article text' />$longBio</textarea> 
           <p>Story2<br /><textarea name='newsLong2' value='' placeholder='News article text' />$longBio2</textarea> 


           <button type='submit' name='updateNews'> 
            Update 
           </button> 
          </div> 
         </form> 


        </div> 

       "; 

      ?> 

這是我如何嘗試更新文章。我試圖根據主鍵更新記錄,這個變量正在傳遞給頁面,因爲它正在加載表單中的內容。

<?php 

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


    $newsTitle = $_POST['newsTitle']; 
    $newsDate = $_POST['newsDate']; 
    $newsShort = $_POST['newsShort']; 
    $newsLong = $_POST['newsLong']; 
    $newsLong2 = $_POST['newsLong2']; 
    $newsImage = $_POST['newsImage']; 



$insertNews = "UPDATE mods SET title='$newsTitle', date='$newsDate', shortBio='$newsShort', longBio='$newsLong', longBio2='$newsLong2', image='$newsImage' WHERE articleID='$var_value'"; 

$updateNews = mysqli_query($con,$insertNews); 

    if($updateNews){ 
     echo "<script>alert('Article updated.');</script>"; 
    } 

} 
?> 
+0

'$ newsImage = $ _POST ['newsImage'];'你正在使用錯誤的超全局。 –

+0

,不知道你是否連接到你的數據庫;你的代碼的第二部分沒有顯示任何證據。 –

+0

您好弗雷德,我已連接到數據庫抱歉,我剛剛添加了我用來嘗試更新文章的代碼。我應該爲此添加更多代碼嗎?數據庫連接工作正常,但頁面加載我想從數據庫的形式編輯的文章,只是不會允許我保存更改。 – Allan

回答

0

你說$var_value被傳遞給你的PHP更新腳本,但我看不出這是就是,也不是由一個POST拾起並轉移到一個局部變量。將它作爲<input type="hidden"傳遞,然後拿起使用POST

在第一個PHP腳本:

<p>Article Title<br /><input type='text' value='$newsTitle' name='newsTitle' /> 
<input type="hidden" value='$var_value' name='var_value' /> 
</p> 

在第二PHP腳本:

$var_value = $_POST['var_value']; 

這也將是很好看,通過使用參數化查詢保護您的腳本免受SQL注入。

+1

這是我最後修復它的方式。謝謝:) – Allan

0

沒有您查詢的是什麼

insertNews = "UPDATE mods SET title='$newsTitle' date='$newsDate' shortBio='$newsShort' longBio='$newsLong' longBio2='$newsLong2' image='$newsImage' WHERE articleID='$var_value'";

與您當前的查詢替換這一點,你會好到哪裏去。