2015-10-19 19 views
1

因此,無論何時單擊添加按鈕並更新數據庫,我都必須刷新頁面才能看到結果,而不是頁面自動刷新後點擊「更新數據庫」。刪除按鈕不起作用,必須刷新頁面才能看到更新的數據庫結果

另外,我的刪除按鈕根本不起作用。請幫助d:

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset='utf-8' /> 
    <title>Movie Project</title> 
</head> 
<body> 
    <h1 style='text-align:center;'>Movie Collection Database</h1> 
    <form action="<?php echo $_SERVER['PHP_SELF'];?>" method='post'> 
     <table align='center' border='0' cellpadding='3'> 
      <tr> 
      <?php 
       $fieldsArray = array("Title", "Studio", "Rating", "Pub. Year", "IMDB Rating", "Run Time(min)", "Add"); 
       echo "<tr>"; 
       for($i=0;$i<7;$i++) 
        { 
         echo "<td>". $fieldsArray[$i]. "</td>"; 
        }//end for 
       echo "</tr>"; 
       ?> 
      </tr> 
     <tr> 
      <td colspan='1' style='text-align:center'> 
       <input type="text" name="title" value="Movie Title"> 
      </td> 
      <td> 
       <input type="text" name="studName" value="Studio Name"> 
      </td> 

      <td> 
       <select name="movieRating"> 
        <option value="G">G</option> 
        <option value="PG">PG</option> 
        <option value="PG-13">PG-13</option> 
        <option value="R">R</option> 
        <option value="NC-17">NC-17</option> 
        <option value="Not Rated">Not Rated</option> 
       </select> 
      </td> 
      <td> 
       <input type="text" name="pubYear" value="2015"> 
      </td> 

      <td> 
       <input type="number" name="IMDBRating" value="10.0"> 
      </td> 
      <td> 
        <input type="number" name="time" value="0"> 
      </td> 
      <td> 
        <input type="checkbox" name="addBox"> 
      </td> 
     </tr> 
     </table> 
     <table align='center'> 
      <tr> 
       <td> 
        <input type="submit" name="submit" value="Update Database"> 
       </td> 
      </tr> 
     </table> 
    </form> 

    <?php 
    echo "<table align = 'center', border = '1' >"; 
    try 
    { 
     $db = new PDO("mysql:host=localhost;dbname=buckel", "buckel", "12345"); 
    } 
    catch (PDOException $error) 
    { 
     die("Connection failed: ". $error->getMessage()); 
    } 
    try 
    { 
     $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
    } 
    catch (PDOException $error) 
    { 
     $db->rollback(); 

     echo "Transaction not completed: " . $error->getMessage(); 
    } 


    include_once("Function.php"); 
    if($_SERVER['REQUEST_METHOD']==="POST") 
    { 
     Delete($db); 
     $fieldsArray = array("Title", "Studio", "Rating", "Pub. Year", "IMDB Rating", "Run Time(min)", "Delete"); 
     echo "<tr>"; 
     for($i=0;$i<7;$i++) 
      { 
       echo "<td>". $fieldsArray[$i]. "</td>"; 
      }//end for 
     echo "</tr>"; 

     $query = "SELECT * FROM movie"; 
     $statement = $db->prepare($query); 
     $statement->execute(); 
     foreach(Prepare($db) as $row) 
     { 
      echo "<tr><td>" .$row['title'] . "</td>"; 
      echo "<td>" .$row['studio'] . "</td>"; 
      echo "<td>" .$row['rating'] . "</td>"; 
      echo "<td>" .$row['pub_year'] . "</td>"; 
      echo "<td>" .$row['imdb_rating'] . "</td>"; 
      echo "<td>" .$row['run_time'] . "</td>"; 
      echo "<td><input type='checkbox' name='". $row['id']. "' value='". $row['id']. "'>Delete</td></tr>"; 

     }//end foreach 

     echo "</table>"; 
     if(isset($_POST['addBox'])) 
     { 
      $title=$_POST['title']; 
      $studio=$_POST['studName']; 
      $year=$_POST['pubYear']; 
      $movieRating=$_POST['movieRating']; 
      $IMDBRating=$_POST['IMDBRating']; 
      $time=$_POST['time']; 
     try{ 


      $query2= $db->prepare("INSERT INTO movie (id, title, studio, rating, pub_year, imdb_rating, run_time) value (".'NULL'.", '$title','$studio','$movieRating','$year','$IMDBRating','$time')"); 
       $query2->execute(); 
      }//end try 
     catch(PDOException $error) 
      { 
      $db->rollback(); 
      echo "There was an error"; 
      $db=null; 

      die("Connection failed:" . $error->getMessage()); 
      }//end catch 

     }//end if 


     if(isset($db)) 
     $db=null; 
    }//end if for whole thing 
    ?> 
</body> 
</html> 

這裏是我打電話

<?php 
function Prepare($db) 
{ 
$q = $db->prepare("SELECT * FROM movie"); 
$q->execute(); 
return $q->fetchAll(); 
}//end Prepare(db object) 

function Delete($db) 
{ 
$q = $db->prepare("SELECT * FROM movie"); 
$q->execute(); 
foreach($q->fetchAll() as $row) 
{ 
    if(array_key_exists($row['id'], $_POST)) 
    { 
     $q = $db->prepare("DELETE FROM movie WHERE movie.id=" . $row['id']); 
     $q->execute(); 
    }//end if 

}//end foreach 
}//end Delete(db object) 
+0

提交表單將刷新頁面提交,可能是一個緩存的問題 – 2015-10-19 01:53:04

回答

1

您使用的是自網頁打印功能的文件顯示等

如果想刷新頁面無需重新加載你必須使用

其他你可以嘗試這樣的

的Javascript

window.location.reload(true); 
// false - Default. Reloads the current page from the cache. 
// true - Reloads the current page from the server 

這將刷新頁面。

header("Refresh:0"); 
+0

它給了我這個錯誤,當我剛插入,在我的代碼 致命錯誤的結尾:調用未定義的函數reload()在/home/buckel/public_html/Projects/MovieForm.php在線139 – Coldfire

+0

你必須提供onclick更新按鈕。這是一個JavaScript代碼。 –

+0

當我這樣做時,我也得到一個錯誤 警告:無法修改頭信息 - 在/ home/buckel/public_html /頭文件中已經發送的頭文件(輸出開始於/home/buckel/public_html/Projects/MovieForm.php:9)在線141 行141是我插入php – Coldfire

0

我固定刪除的部分不把所有我的PHP的形式內工作。我想的是,PHP沒有被使用的形式

相關問題