删除按钮不起作用,必须刷新页面才能看到更新的数据库结果

时间:2015-10-19 01:49:42

标签: php refresh

因此,每当我单击添加按钮并更新数据库时,我必须刷新页面以查看结果,而不是单击“更新数据库”后页面自动刷新。

此外,我的删除按钮根本不起作用。请帮助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)

2 个答案:

答案 0 :(得分: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");

答案 1 :(得分:0)

我通过将所有的php放在表单中来修复删除部分无效。我想php没有以表格

提交