Coldfire
Coldfire

Reputation: 11

Delete button doesn't work and have to refresh page to see updated database results

So, whenever I click the add button and update the database, I have to refresh the page to see the results instead of the page auto refreshing after I click "update database".

Also, my delete button doesn't work at all. Please help 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>

and here is the function file that i'm calling

<?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)

Upvotes: 1

Views: 819

Answers (2)

Anik Islam Abhi
Anik Islam Abhi

Reputation: 25352

You are using self page for printing showing etc.

If wanna to refresh page without reloading you have to use

else you can try like this

Javascript

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

This will reload the page.

Php

header("Refresh:0");

Upvotes: 1

Coldfire
Coldfire

Reputation: 11

I fixed the delete part not working by putting all of my php inside the form. I suppose that the php was not being submitted with the form

Upvotes: 0

Related Questions