Adding multiple records at ones

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tito85
Forum Contributor
Posts: 104
Joined: Sat Mar 13, 2010 11:26 am

Adding multiple records at ones

Post by tito85 »

Hi,

I have updated my database and hence I need to change the code. I had a table named movies linked to a table named genres. In the movies table I had a foreign key named GenreID which is the primary key in the genres table. This alloweded me to select and add a single genre to a movie.

Now I renamed the table genres to genre types and added a new table in the middle named genres. In the genres table I have to foreign keys, MovieID and GenreTypeID. This will allow me to add multiple genres to a movie.

Here is a small ERD of the new database
New ERD.jpg
New ERD.jpg (29.47 KiB) Viewed 494 times
Here is the code I used to add a new movie and select the genre before the database update.

Code: Select all

<?php
  session_start();
  require('dbconnect.php');
  include('securitycheck.php');
  if ($isAdmin) {
      if (isset($_POST['btnCancel'])) {
          header('Location: movies.php');
      } elseif (isset($_POST['btnAddMovie'])) {
          $title = $_POST['txtTitle'];
          $director1 = $_POST['txtDirector1'];
          $genre = $_POST['ddlGenre'];
          if (strlen(trim($title)) > 0 && strlen(trim($director1)) > 0 && $genre != "0") {
			  $director2 = $_POST['txtDirector2'];
              $moviereleasedate = explode("/", $_POST['txtmovieReleaseDate']);
              $dvdreleasedate = explode("/", $_POST['txtDVDReleaseDate']);
              $image = $_FILES['txtImage'];
              $rating = $_POST['ddlRating'];
              $review = $_POST['txtReview'];
              $moviereleaseday = $moviereleasedate[0];
              $moviereleasemonth = $moviereleasedate[1];
              $moviereleaseyear = $moviereleasedate[2];
              $dvdreleaseday = $dvdreleasedate[0];
              $dvdreleasemonth = $dvdreleasedate[1];
              $dvdreleaseyear = $dvdreleasedate[2];
              $moviereleasedate = date("Y-m-d", mktime(0, 0, 0, $moviereleasemonth, $moviereleaseday, $moviereleaseyear));
              $dvdreleasedate = date("Y-m-d", mktime(0, 0, 0, $dvdreleasemonth, $dvdreleaseday, $dvdreleaseyear));
              $filename = "";
              //checking if an image was uploaded
              if ($image) {
                  //checking if image is JPG
                  if ($image['type'] == "image/jpeg" || $image['type'] == "image/pjpeg") {
                      $filename = $image['name'];
                      //uploading the file
                      move_uploaded_file($image['tmp_name'], "Images/Movies/" . $image['name']);
                  } else {
                      $message = "Only .jpg images are allowed to be uploaded";
                  }
              }
              $insert = "INSERT INTO movies (Title, Review, Rating, MovieReleaseDate, DVDReleaseDate, Director1, Director2, GenreID, Filename) VALUES ('" . mysql_real_escape_string($title) . "', '" . mysql_real_escape_string($review) . "', '" . $rating . "', '" . $moviereleasedate . "', '" . $dvdreleasedate . "', '" . mysql_real_escape_string($director1) . "', '" . mysql_real_escape_string($director2) . "', '" . $genre . "', '" . $filename . "')";
              mysql_query($insert) or die(mysql_error());
              header('Location: movies.php');
          } else {
              $message = "Error: <b>Title</b>, <b>Director</b> and <b>Genre</b> are mandatory";
          }
      }
  } else {
      header('Location: index.php');
  }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="CSSWebStyles/style.css" />
<title>Add Movie</title>
</head>
<body>
<div id="website">
<?php
  //checking if the user is an administrator
  if (isset($_SESSION['superadmin'])) {
      if ($_SESSION['superadmin'])
          include('adminmenu.php');
  }
?>
<div id="content">
<center>
<br />
<h2>Add Movie</h2>
<br />
</center>
<div id="contentgeneral">
<?php
  if (isset($message)) {
      echo "<center>$message</center>";
      unset($message);
  }
?>
<form method="post" enctype="multipart/form-data">
<table>
<tr>
<td>
Title
</td>
<td>
<input type="text" style="width: 300px;" name="txtTitle" />*
</td>
</tr>
<tr>
<td>
Director 1
</td>
<td>
<input type="text" style="width: 300px;" name="txtDirector1" />*
</td>
</tr>
<tr>
<td>
Director 2
</td>
<td>
<input type="text" style="width: 300px;" name="txtDirector2" />
</td>
</tr>
<tr>
<td>
Genre
</td>
<td>
<select name="ddlGenre">
<option value="0">Please Select</option>
<?php
  $query = "SELECT * FROM genretypes ORDER BY Genre ASC";
  $result = mysql_query($query);
  while ($genre = mysql_fetch_array($result)) {
      echo "<option value=\"" . $genre['GenreID'] . "\">" . $genre['Genre'] . "</option>";
  }
?>
</select>*
</td>
</tr>
<tr>
<td valign="top">
Rating
</td>
<td>
<select name="ddlRating">
<option value="0">Please Select</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</td>
</tr>
<tr>
<td valign="top">
Movie Release Date
</td>
<td>
<input type="text" style="width: 100px;" name="txtmovieReleaseDate" /><small>Date Format: (dd/mm/yyyy)</small><br />
</td>
</tr>
<tr>
<td valign="top">
DVD Release Date
</td>
<td>
<input type="text" style="width: 100px;" name="txtDVDReleaseDate" /><small>Data Format: (dd/mm/yyyy)</small><br />
</td>
</tr>
<tr>
<td valign="top">
Review
</td>
<td>
<textarea name="txtReview" rows="10" cols="100"></textarea>
</td>
</tr>
<tr>
<td valign="top">
Image
</td>
<td>
<input type="file" name="txtImage" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="btnAddMovie" value="Add" />&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" name="btnCancel" value="Cancel" />
</td>
</tr>
</table>
</form>
</div>
</div>
</div>
</body>
</html>
I Need some help on how should I select and add multiple genres to a new movie.

Thanks in advanced for any help.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Adding multiple records at ones

Post by Eran »

In what format are the genres available? is it an array? and what are the values (ids, names)?
Brenden
Forum Newbie
Posts: 12
Joined: Fri May 28, 2010 2:12 am

Re: Adding multiple records at ones

Post by Brenden »

  • Change your genre <select name="ddlRating"> to something like <select name="ddlGenres" size="5" multiple>
  • When your movie is added, $genrearray = $_POST['dllGenres'];
  • After the movie is entered, use mysql_insert_id() to get the MovieID
  • Go through the array and make a genre table connection for each one
tito85
Forum Contributor
Posts: 104
Joined: Sat Mar 13, 2010 11:26 am

Re: Adding multiple records at ones

Post by tito85 »

Hi,

Thanks for your helping points.

Its not so strait forward for me.... I will try.

Can you give me some more help on the last 2 points please?

Thanks in advanced for your time!
tito85
Forum Contributor
Posts: 104
Joined: Sat Mar 13, 2010 11:26 am

Re: Adding multiple records at ones

Post by tito85 »

It like I managed to do it however only one genre is being added to the database.

Any Idea why?
Brenden
Forum Newbie
Posts: 12
Joined: Fri May 28, 2010 2:12 am

Re: Adding multiple records at ones

Post by Brenden »

$_POST['dllGenres'] is now an array, not a number, you need to loop through it, and for each one make a new record in the Genres table
tito85
Forum Contributor
Posts: 104
Joined: Sat Mar 13, 2010 11:26 am

Re: Adding multiple records at ones

Post by tito85 »

Never did that...

I think I need more help...
Post Reply