I am trying to make a movie list for myself, I got everything working like add and delete but the edit gives me some headace.

I get all the values fine, id, movie title, director and year are all filled in, well not what category I added using radio buttons but everything else, I need to get a check working to see what radio button that was checked when adding the movie.

Thing is, when I hit update, it sends me to the index page but nothing is updated... If anyone would be so kind to glance att the code and see what I am missing it would be greatly appreciated.

Also I am not getting an error if category for the movie isn't selected.

If I remove what is filled into title/director or year it throws me this error:

Notice: Undefined index: id in I:_Misc\Programming\EasyPHP-12.1\www\records\movie.php on line 29

Notice: Undefined index: category in I:_Misc\Programming\EasyPHP-12.1\www\records\movie.php on line 30

Im sure I am missing something basic here and that I have been looking at this for far too long :(

<?php
//Connect to the database
include("connect.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'>
<head>
    <meta http-equiv='content-type' content='application/xhtml+xml; charset=UTF-8' />

    <title>Edit Movie</title>
    <!-- JAVASCRIPTS -->
    <!-- CSS FILES -->
</head>
<body>
<h1>Edit Movie</h1>

<?php
//If the 'id' variable is set in the URL, we know that we need to edit a record
if (isset($_GET['id']))
{
    //If the form's submit button is clicked, we need to process the form
    if (isset($_POST['submit']))
    {
        //Make sure the 'id' in the URL is valid
        if (is_numeric($_GET['id']))
        {
            //Get variables from the URL/form
            $id         = $_POST['id'];
            $category   = $_POST['category'];
            $title      = htmlspecialchars($_POST['title'], ENT_QUOTES);
            $director   = htmlspecialchars($_POST['director'], ENT_QUOTES);
            $year       = htmlspecialchars($_POST['year'], ENT_QUOTES);

            //Check that no fields are empty
            if($category == '' || $title == '' || $director == '' || $year == '')
            {
                //If they are empty, show an error message and display the form
                $error = 'ERROR: Please fill in all required fields!';
            }
            else
            {
                //If everything is fine, update the movie in the database
                if ($stmt = $db->prepare("UPDATE movies SET category = ?, title = ?, director = ?, year = ?
                                                WHERE id=?"))
                {
                    $stmt->bind_param("ssi", $category, $title, $director, $year, $id);
                    $stmt->execute();
                    $stmt->close();
                }
                //Show an error message if the query has an error
                else
                {
                    echo "ERROR: could not prepare SQL statement.";
                }

                //Redirect the user once the form is updated
                header("Location: index.php");
            }
        }
        //If the 'id' variable is not valid, show an error message
        else
        {
            echo "Error!";
        }
    }
    //If the form hasn't been submitted yet, get the info from the database and show the form
    else
    {
        //Make sure the 'id' value is valid
        if (is_numeric($_GET['id']) && $_GET['id'] > 0)
        {
            //Get 'id' from URL
            $id = $_GET['id'];

            //Get the movie from the database
            if($stmt = $db->prepare("SELECT * FROM movies WHERE id=?"))
            {
                $stmt->bind_param("i", $id);
                $stmt->execute();

                $stmt->bind_result($id, $title, $director, $year, $category);
                $stmt->fetch();

                $stmt->close();
            }
            //Show an error if the query has an error
            else
            {
                echo "Error: could not prepare SQL statement";
            }
        }
        //If the 'id' value is not valid, redirect the user back to the view.php page
        else
        {
            header("Location: index.php");
        }
    }
}
//Close the database connection
$db->close();
?>

<form action="" method="post">
    <div>
        <p>ID: <?php echo $id; ?></p>

        <form action="" method="post">
            <section>
                <tr>
                    <td>Choose a category:</td><td colspan="3">
                    <input type="radio" name="category" value="Action"  />Action
                    <input type="radio" name="category" value="Comedy" />Comedy
                    <input type="radio" name="category" value="Drama" />Drama
                    <input type="radio" name="category" value="Horror" />Horror
                    <input type="radio" name="category" value="Thriller" />Thriller
                </td>
                </tr>
                <tr>
                    <br /><br />
                    <td>Title:</td><br />
                    <td><input type="text" name="title" value="<?php echo $title; ?>" size="30" /></td><br />
                    <td>Director:</td><br />
                    <td><input type="text" name="director" value="<?php echo $director; ?>" size="30" /></td><br />
                    <td>Year:</td><br />
                    <td><input type="text" name="year" value="<?php echo $year; ?>" size="8" /></td><br /><br />
                </tr>
                <tr>
                    <td colspan="4">
                        <input type="Submit" name="submit" value="Edit Movie" /></td>
                </tr>
            </section>
    </div>
</form>
</body>
</html>

Recommended Answers

All 19 Replies

You can create radio buttons with PHP. Put categories in an array, cycle through it, add code for radio buttons and check if current category matches the one from the DB:

<?php
// array of categories (you can always edit it and the radio buttons will change)
$categoriesArr = array('Action', 'Comedy', 'Drama', 'Horror', 'Thriller');

// add radio buttons via php and add checked attribute for the selected categpry
foreach($categoriesArr as $cat) {

    echo '<input type="radio" name="category" value="' . $cat  . '"';

    if($cat == $category) {

        echo ' checked="checked"';
    }

    echo ' />' . $cat;
}
?>

The code above should replace the code on lines 112 to 116.

Member Avatar for LastMitch

Im sure I am missing something basic here and that I have been looking at this for far too long :(

Did you write this code?

Notice: Undefined index: id in I:_Misc\Programming\EasyPHP-12.1\www\records\movie.php on line 29

Notice: Undefined index: category in I:_Misc\Programming\EasyPHP-12.1\www\records\movie.php on line 30

The reason you got that error is because id & category doesn't exist.

Either you are not connected to the database or you didn't create a column for id & category in the database.

If you try to see if you are connected to the database then this error will disappear if it is connected.

If you check to see you have a column for id & category in the database if you don't have those column you need to create one.

Shouldn't you read the ID from URL ($_GET)?

$id         = $_POST['id'];

on line 26, should be

$id         = $_GET['id'];

Is category a required information? If category has not been chosen (user wants to se all categories) you have to provide for that situation:

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

    $category   = $_POST['category'];

} else {

    $category = 'All';
}

// handle the all categories situation
...
Member Avatar for LastMitch

Shouldn't you read the ID from URL ($_GET)?

He has 2 different type of id's running?

I thought it was just a db issue because of the errors.

I have the id & category, when I add movies I get ID, movie title, director and year. When I hit edit I get to a page where it diplays all that, well not category.... yet but I have them.

Have you tried the code form my first post (the one immediately after your post)?

You mean line 29 should be GET and not POST? I changed that and yes the error vanished. I did add the php for radio, worked like a charm adding movies but still can't get it for this edit page what one were selected during adding the movie.

Still get that error on line 30 for category because it's not pre-filled, that error will go away if I just can get it from the db :)

But still, can't update :/ Returns to the index and all but nothing is changed.

Let's see what gets POSTed over. Can you insert this code immediatelly after line 24 and post the result after submitting the form:

die(print_r($_POST, 1));

This will display values in $_POST and terminate the script.

Array ( [category] => Action checked= [title] => Equlibrium [director] => Kurt [year] => 2002 [submit] => Edit Movie )

Director still says: Kurt Willmer

Something is wrong in HTML. If you check the HTML code for the radio buttons. Can you have a look at the HTML code (right click in web browser and select View source or something similar) and post the code for radio buttons.

<form action="" method="post">
    <div>
        <p>ID: 1</p>

        <form action="" method="post">
            <section>
                <tr>
                    <td>Pick category:</td><td colspan="3">
                    <input type="radio" name="category" value="Action checked="checked"" />Action<input type="radio" name="category" value="Comedy" />Comedy<input type="radio" name="category" value="Drama" />Drama<input type="radio" name="category" value="Horror" />Horror<input type="radio" name="category" value="Thriller" />Thriller                    <hr />
                <tr>
                    <td>Title:</td><br />
                    <td><input type="text" name="title" value="Equlibrium" size="30" /></td><br />
                    <td>Director:</td><br />
                    <td><input type="text" name="director" value="Kurt Willmer" size="30" /></td><br />
                    <td>Year:</td><br />
                    <td><input type="text" name="year" value="2002" size="8" /></td><br />
                </tr>
                <tr>
                    <hr />
                    <td colspan="4">
                        <input type="Submit" name="submit" value="Edit Movie" /></td>
                </tr>
            </section>
    </div>

That's the form with all the fields on the edit page, the pre-filled values are there because they are grabbed from the db

Double quote is missing after the value attribute. Have you copied the code form my post correctly? It works in my browser, I have tested it.

No idea what happened, yes I copied but now I get

Array ( [category] => Action [title] => Equlibrium [director] => Kurt Willmer [year] => 2002 [submit] => Edit Movie )

So, I guess the update issue is all there is then...

No ID error if I leave any field blank, weird part is that it won't throw a error because I typed an if to check to see if any of the fields were empty.

Found my error message, line 39

$error = 'ERROR: Please fill in all required fields!';

Should be

echo "ERROR: Please fill in all required fields!";

Line 47:

$stmt->bind_param("ssi", $category, $title, $director, $year, $id);

You have declared only three types but you have five parameters. Try:

$stmt->bind_param("sssii", $category, $title, $director, $year, $id);

And change the line 54 to

die("ERROR: could not prepare SQL statement.");

so the script is stopped if error occurs (now you immediately get redirected to index).

You were right, three parameters to five really made the differance. Also when I changed my error message to echo if any field were empty fixed the category.

Everything works like a charm now, can't thank you enough!!!

I have a question though, what did you mean about the categories code? It's required yes but all categories are visible on the add page and edit page already.

Also, should I stop using echo ERROR and just do die from now on and why?

I have a question though, what did you mean about the categories code?

Just an idea. One possibility would be that a user wants to see all categories, so chosing a category would be only an option. The default would be that no category is chosen. But it is your preference if category is required.

Also, should I stop using echo ERROR and just do die from now on and why?

In your original code you would never see the error message displayed since immediatelly after echoing it you would be redirected to index page. If you use die, redirection does not happen. I would do it differently. If an error occurs (which I guess is not often) I would redirect to a special error page with simple explanation and links to other pages.

That error handling sounds like a good idea, thanks for the input and all the help.

You are welcome. Please mark this thread as solved. If you have more questions open a new thread. Happy coding.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.