Form POST Problem
Posted by: Brian Oliver
Date: June 06, 2006 11:11PM
Date: June 06, 2006 11:11PM
With the help of a tutorial book, I've created a form that I can enter the information (movies) and it will post to the appropriate tables. I also created a basic page to make sure it did post correctly, including a page to show only movie reviews with "action" as the genre. While my first entry showed up, the second did not... until I used the "edit" form (which everything shows up fine) and I click the "Submit" button, refresh the test page and the entry shows up now. Now, it has something to do with the 'genre' and 'critic' parts of the code because I also tested to see if the title and other areas posted right (and they did). Once I added the critic or genre elements, the new entry did not show up.
The two areas of critic and genre are selected via a dropdown box and checkbox (as one movie will have more than one genre) respectively. The rest of the entries are submitted via the standard text box...
Here's the code for the new_reviews page:
<?php
include '../includes/connection.php';
if (isset($_POST['intro'])):
// A new review has been entered
// using the form.
$id = $_POST['id'];
$keywords = $_POST['keywords'];
$title = $_POST['title'];
$listtitle = $_POST['listtitle'];
$year = $_POST['year'];
$releasedate = $_POST['releasedate'];
$intro = $_POST['intro'];
$reviewbody1 = $_POST['reviewbody1'];
$reviewbody2 = $_POST['reviewbody2'];
$image1 = $_POST['image1'];
$image2 = $_POST['image2'];
$poster = $_POST['poster'];
$mpaa_rating = $_POST['mpaa_rating'];
$mpaa_reason = $_POST['mpaa_reason'];
$runtime = $_POST['runtime'];
$star_rating = $_POST['star_rating'];
$cid = $_POST['cid'];
$genres = $_POST['genres'];
if ($cid == '') {
exit('<p>You must choose a critic for this review. Click "Back" and try again.</p>');
}
$sql = "INSERT INTO movie_reviews SET
id='$id',
title='$title',
year='$year',
reviewdate=CURDATE(),
releasedate='$releasedate',
intro='$intro',
reviewbody1='$reviewbody1',
reviewbody2='$reviewbody2',
image1='$image1',
image2='$image2',
mpaa_rating='$mpaa_rating',
mpaa_reason='$mpaa_reason',
runtime='$runtime',
star_rating='$star_rating',
criticid='$cid',
poster='$poster',
keywords='$keywords',
listtitle='$listtitle'";
if (@mysql_query($sql)) {
echo '<p>New review added</p>';
} else {
exit('<p>Error adding new review: ' . mysql_error() . '</p>');
}
if (isset($_POST['genres'])) {
$genres = $_POST['genres'];
} else {
$genres = array();
}
$numGenres = 0;
foreach ($genres as $genreID) {
$sql = "INSERT IGNORE INTO moviegenre
SET movieid='$id', genreid=$genreID";
$ok = @mysql_query($sql);
if ($ok) {
$numGenres = $numGenres + 1;
} else {
echo "<p>Error inserting review into category $genreID: " .
mysql_error() . '</p>';
}
}
?>
<p>Review was added to <?php echo $numGenre; ?> categories.</p>
<p><a href="<?php echo $_SERVER['PHP_SELF']; ?>">Add another movie review</a></p>
<p><a href="../index.html">INDEX PAGE</a></p>
<?php
else:
$critics = @mysql_query('SELECT id, name FROM critic');
if (!$critics) {
exit('<p>Unable to obtain critic list from the database.</p>');
}
$genres = @mysql_query('SELECT id, category FROM genre');
if (!$genres) {
exit('<p>Unable to obtain genre list from the database.</p>');
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<p>Movie ID:<br />
<textarea name="id" rows="1" cols="30">
</textarea></p>
<p>Keywords:<br />
<textarea name="keywords" rows="4" cols="45">
</textarea></p>
<p>Movie Title:<br />
<textarea name="title" rows="1" cols="45">
</textarea></p>
<p>Listing Title: (title that will appear on review index pages)<br />
<textarea name="listtitle" rows="1" cols="45">
</textarea></p>
<p>Year:<br />
<textarea name="year" rows="1" cols="8">
</textarea></p>
<p>Release Date:<br />
<textarea name="releasedate" rows="1" cols="30">
</textarea></p>
<p>Introduction:<br />
<textarea name="intro" rows="8" cols="70">
</textarea></p>
<p>Review Part 1:<br />
<textarea name="reviewbody1" rows="15" cols="70">
</textarea></p>
<p>Review Part 2:<br />
<textarea name="reviewbody2" rows="15" cols="70">
</textarea></p>
<p>Image #1:<br />
<textarea name="image1" rows="1" cols="45">
</textarea></p>
<p>Image #2:<br />
<textarea name="image2" rows="1" cols="45">
</textarea></p>
<p>Poster:<br />
<textarea name="poster" rows="1" cols="45">
</textarea></p>
<P>MPAA Rating:
<select name="mpaa_rating" size="1">
<option selected value="">Select One</option>
<option value="">--------------------</option>
<option value="g">G</option>
<option value="pg">PG</option>
<option value="pg13">PG-13</option>
<option value="r">R</option>
<option value="nc17">NC-17</option>
<option value="nr">Not Rated</option>
</select></p>
<p>MPAA Reason:<br />
<textarea name="mpaa_reason" rows="3" cols="45">
</textarea></p>
<p>Runtime:<br />
<textarea name="runtime" rows="1" cols="8">
</textarea></p>
<P>Star Rating:
<select name="star_rating" size="1">
<option selected value="">Select One</option>
<option value="">----------------</option>
<option value="1star">1 STAR</option>
<option value="1.25stars">1.25 STARS</option>
<option value="1.5stars">1.5 STARS</option>
<option value="1.75stars">1.75 STARS</option>
<option value="2stars">2 STARS</option>
<option value="2.25stars">2.25 STARS</option>
<option value="2.5stars">2.5 STARS</option>
<option value="2.75stars">2.75 STARS</option>
<option value="3stars">3 STARS</option>
<option value="3.25stars">3.25 STARS</option>
<option value="3.5stars">3.5 STARS</option>
<option value="3.75stars">3.75 STARS</option>
<option value="4stars">4 STARS</option>
<option value="4.25stars">4.25 STARS</option>
<option value="4.5stars">4.5 STARS</option>
<option value="4.75stars">4.75 STARS</option>
<option value="5stars">5 STARS</option>
</select></p>
<P>Critic:
<select name="cid" size="1">
<option selected value="">Select One</option>
<option value="">-----------</option>
<?php
while ($critic = mysql_fetch_array($critics)) {
$cid = $critic['id'];
$cname = htmlspecialchars($critic['name']);
echo "<option value='cid'>$cname</option>\n";
}
?>
</select></p>
<p>Place in Genre(s):<br />
<?php
while ($genre = mysql_fetch_array($genres)) {
$gid = $genre['id'];
$gname = htmlspecialchars($genre['category']);
echo "<label><input type='checkbox' name='genres[]' " .
"value='$gid' />$gname</label><br />\n";
}
?>
</p>
I hope this wasn't too confusing but I didn't know how else to explain it. For the time being, I could just enter the data through the newreview page and then use the edit page (sort of as a last check to make sure everything is in order).
Edited 1 time(s). Last edit at 06/06/2006 11:12PM by Brian Oliver.
The two areas of critic and genre are selected via a dropdown box and checkbox (as one movie will have more than one genre) respectively. The rest of the entries are submitted via the standard text box...
Here's the code for the new_reviews page:
<?php
include '../includes/connection.php';
if (isset($_POST['intro'])):
// A new review has been entered
// using the form.
$id = $_POST['id'];
$keywords = $_POST['keywords'];
$title = $_POST['title'];
$listtitle = $_POST['listtitle'];
$year = $_POST['year'];
$releasedate = $_POST['releasedate'];
$intro = $_POST['intro'];
$reviewbody1 = $_POST['reviewbody1'];
$reviewbody2 = $_POST['reviewbody2'];
$image1 = $_POST['image1'];
$image2 = $_POST['image2'];
$poster = $_POST['poster'];
$mpaa_rating = $_POST['mpaa_rating'];
$mpaa_reason = $_POST['mpaa_reason'];
$runtime = $_POST['runtime'];
$star_rating = $_POST['star_rating'];
$cid = $_POST['cid'];
$genres = $_POST['genres'];
if ($cid == '') {
exit('<p>You must choose a critic for this review. Click "Back" and try again.</p>');
}
$sql = "INSERT INTO movie_reviews SET
id='$id',
title='$title',
year='$year',
reviewdate=CURDATE(),
releasedate='$releasedate',
intro='$intro',
reviewbody1='$reviewbody1',
reviewbody2='$reviewbody2',
image1='$image1',
image2='$image2',
mpaa_rating='$mpaa_rating',
mpaa_reason='$mpaa_reason',
runtime='$runtime',
star_rating='$star_rating',
criticid='$cid',
poster='$poster',
keywords='$keywords',
listtitle='$listtitle'";
if (@mysql_query($sql)) {
echo '<p>New review added</p>';
} else {
exit('<p>Error adding new review: ' . mysql_error() . '</p>');
}
if (isset($_POST['genres'])) {
$genres = $_POST['genres'];
} else {
$genres = array();
}
$numGenres = 0;
foreach ($genres as $genreID) {
$sql = "INSERT IGNORE INTO moviegenre
SET movieid='$id', genreid=$genreID";
$ok = @mysql_query($sql);
if ($ok) {
$numGenres = $numGenres + 1;
} else {
echo "<p>Error inserting review into category $genreID: " .
mysql_error() . '</p>';
}
}
?>
<p>Review was added to <?php echo $numGenre; ?> categories.</p>
<p><a href="<?php echo $_SERVER['PHP_SELF']; ?>">Add another movie review</a></p>
<p><a href="../index.html">INDEX PAGE</a></p>
<?php
else:
$critics = @mysql_query('SELECT id, name FROM critic');
if (!$critics) {
exit('<p>Unable to obtain critic list from the database.</p>');
}
$genres = @mysql_query('SELECT id, category FROM genre');
if (!$genres) {
exit('<p>Unable to obtain genre list from the database.</p>');
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<p>Movie ID:<br />
<textarea name="id" rows="1" cols="30">
</textarea></p>
<p>Keywords:<br />
<textarea name="keywords" rows="4" cols="45">
</textarea></p>
<p>Movie Title:<br />
<textarea name="title" rows="1" cols="45">
</textarea></p>
<p>Listing Title: (title that will appear on review index pages)<br />
<textarea name="listtitle" rows="1" cols="45">
</textarea></p>
<p>Year:<br />
<textarea name="year" rows="1" cols="8">
</textarea></p>
<p>Release Date:<br />
<textarea name="releasedate" rows="1" cols="30">
</textarea></p>
<p>Introduction:<br />
<textarea name="intro" rows="8" cols="70">
</textarea></p>
<p>Review Part 1:<br />
<textarea name="reviewbody1" rows="15" cols="70">
</textarea></p>
<p>Review Part 2:<br />
<textarea name="reviewbody2" rows="15" cols="70">
</textarea></p>
<p>Image #1:<br />
<textarea name="image1" rows="1" cols="45">
</textarea></p>
<p>Image #2:<br />
<textarea name="image2" rows="1" cols="45">
</textarea></p>
<p>Poster:<br />
<textarea name="poster" rows="1" cols="45">
</textarea></p>
<P>MPAA Rating:
<select name="mpaa_rating" size="1">
<option selected value="">Select One</option>
<option value="">--------------------</option>
<option value="g">G</option>
<option value="pg">PG</option>
<option value="pg13">PG-13</option>
<option value="r">R</option>
<option value="nc17">NC-17</option>
<option value="nr">Not Rated</option>
</select></p>
<p>MPAA Reason:<br />
<textarea name="mpaa_reason" rows="3" cols="45">
</textarea></p>
<p>Runtime:<br />
<textarea name="runtime" rows="1" cols="8">
</textarea></p>
<P>Star Rating:
<select name="star_rating" size="1">
<option selected value="">Select One</option>
<option value="">----------------</option>
<option value="1star">1 STAR</option>
<option value="1.25stars">1.25 STARS</option>
<option value="1.5stars">1.5 STARS</option>
<option value="1.75stars">1.75 STARS</option>
<option value="2stars">2 STARS</option>
<option value="2.25stars">2.25 STARS</option>
<option value="2.5stars">2.5 STARS</option>
<option value="2.75stars">2.75 STARS</option>
<option value="3stars">3 STARS</option>
<option value="3.25stars">3.25 STARS</option>
<option value="3.5stars">3.5 STARS</option>
<option value="3.75stars">3.75 STARS</option>
<option value="4stars">4 STARS</option>
<option value="4.25stars">4.25 STARS</option>
<option value="4.5stars">4.5 STARS</option>
<option value="4.75stars">4.75 STARS</option>
<option value="5stars">5 STARS</option>
</select></p>
<P>Critic:
<select name="cid" size="1">
<option selected value="">Select One</option>
<option value="">-----------</option>
<?php
while ($critic = mysql_fetch_array($critics)) {
$cid = $critic['id'];
$cname = htmlspecialchars($critic['name']);
echo "<option value='cid'>$cname</option>\n";
}
?>
</select></p>
<p>Place in Genre(s):<br />
<?php
while ($genre = mysql_fetch_array($genres)) {
$gid = $genre['id'];
$gname = htmlspecialchars($genre['category']);
echo "<label><input type='checkbox' name='genres[]' " .
"value='$gid' />$gname</label><br />\n";
}
?>
</p>
I hope this wasn't too confusing but I didn't know how else to explain it. For the time being, I could just enter the data through the newreview page and then use the edit page (sort of as a last check to make sure everything is in order).
Edited 1 time(s). Last edit at 06/06/2006 11:12PM by Brian Oliver.
Subject
Written By
Posted
Sorry, you can't reply to this topic. It has been closed.
Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.