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>