Page 1 of 2
Array foreach loop proplem
Posted: Mon May 31, 2010 2:35 am
by tito85
Hi,
I have a drop down menu as multiple so multiple values can be selected. Now I need to loop trough an array so that for each selected value a new row/insert is done in MySQL.
I am trying the following but no success. Any help please?
Code: Select all
<select name="ddlGenres" size="5" multiple> //Drop Down name
$genrearray = $_POST['ddlGenres'];
$LastMovieID = mysql_insert_id();
$selectedgenre = array($genrearray);
foreach($_POST as $key => $selectedgenre)
{
$insert2 = "INSERT INTO genres (MovieID, GenreTypeID) VALUES ('" . mysql_real_escape_string($LastMovieID) . "', '" . mysql_real_escape_string($selectedgenre) . "')";
mysql_query($insert2) or die(mysql_error());
}
Re: Array foreach loop proplem
Posted: Mon May 31, 2010 2:54 am
by Brenden
edit: cancel that. I need more time to figure out how your code is meant to work
Re: Array foreach loop proplem
Posted: Mon May 31, 2010 5:15 am
by tito85
Thanks Brenden, If I can give other information let me know because I'm stuck and in need of help asap please.
Thanks again.
Re: Array foreach loop proplem
Posted: Mon May 31, 2010 10:36 am
by AbraCadaver
Something like this (you need to specify the select name as an array):
[text]
<select name="ddlGenres[]" size="5" multiple>[/text]
Code: Select all
$genrearray = $_POST['ddlGenres'];
$LastMovieID = mysql_insert_id();
//$selectedgenre = array($genrearray); //what is this?
foreach($genrearray as $selectedgenre)
{
$insert2 = "INSERT INTO genres (MovieID, GenreTypeID) VALUES ('" . mysql_real_escape_string($LastMovieID) . "', '" . mysql_real_escape_string($selectedgenre) . "')";
mysql_query($insert2) or die(mysql_error());
}
Re: Array foreach loop proplem
Posted: Tue Jun 01, 2010 1:21 am
by tito85
Hi,
If I just click the submit key without inserting anything in any field it is telling me;
Notice: Undefined index: ddlGenres in C:\wamp\www\movie\addmovie.php on line 11
Where line 11 is:
Code: Select all
$genrearray = $_POST['ddlGenres'];
Any help please?
Re: Array foreach loop proplem
Posted: Tue Jun 01, 2010 7:22 am
by AbraCadaver
Please Help Re: Array foreach loop proplem
Posted: Wed Jun 02, 2010 1:14 pm
by tito85
Hi,
I would need some furture help please. If I use the isset(), No genres are added to the movie. And another thing is that now if I only add data in tha mandatory fields many Notices and Warnings are coming out...
Notice: Undefined offset: 1 in C:\wamp\www\movie\addmovie.php on line 20
Notice: Undefined offset: 2 in C:\wamp\www\movie\addmovie.php on line 21
Notice: Undefined offset: 1 in C:\wamp\www\movie\addmovie.php on line 23
Notice: Undefined offset: 2 in C:\wamp\www\movie\addmovie.php on line 24
Warning: mktime() expects parameter 5 to be long, string given in C:\wamp\www\movie\addmovie.php on line 25
Warning: mktime() expects parameter 5 to be long, string given in C:\wamp\www\movie\addmovie.php on line 26
Warning: Invalid argument supplied for foreach() in C:\wamp\www\movie\addmovie.php on line 42
And the "Only .jpg images are allowed to be uploaded" is displayed too.
Here is all the page code maybe someone can spot the mistakes...
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'];
$genrearray = isset($_POST['ddlGenres']);
if (strlen(trim($title)) > 0 && strlen(trim($director1)) > 0 && $genrearray != "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 = "NoImage.jpg";
//Checking if an Image was Uploaded
if ($image) {
//Checking if the Image is .JPG
if ($image['type'] == "image/jpeg" || $image['type'] == "image/pjpeg") {
$filename = $image['name'];
//Uploading the Image 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, 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) . "', '" . $filename . "')";
mysql_query($insert) or die(mysql_error());
$LastMovieID = mysql_insert_id();
foreach ($genrearray as $selectedgenre) {
$insert2 = "INSERT INTO genres (MovieID, GenreTypeID) VALUES ('" . mysql_real_escape_string($LastMovieID) . "', '" . mysql_real_escape_string($selectedgenre) . "')";
mysql_query($insert2) or die(mysql_error());
}
} 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" />
<link rel="shortcut icon" href="Images/filmicon.ico"/>
<title>Add Movie</title>
</head>
<body>
<div id="website">
<?php
include('banner.php');
?>
<?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="ddlGenres[]" size="5" multiple>
<option value="0">Please Select One or More:</option>
<?php
$query = "SELECT * FROM genretypes ORDER BY Genre ASC";
$result = mysql_query($query);
while ($genre = mysql_fetch_array($result)) {
echo "<option value=\"" . $genre['GenreTypeID'] . "\">" . $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>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</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" /> <input type="submit" name="btnCancel" value="Cancel" />
</td>
</tr>
</table>
</form>
</div>
</div>
<?php
//will include the code from footer.php file
include('footer.php');
?>
</div>
</body>
</html>
Thanks for any help and for your time!
Re: Array foreach loop proplem
Posted: Wed Jun 02, 2010 10:55 pm
by Jonah Bron
Echo the values of $moviereleasedate and $dvdreleasedate immediately after they're defined.
As the error states,
mktime expects type integer arguments. It needs to be
Code: Select all
$moviereleasedate = date("Y-m-d", mktime(0, 0, 0, intval($moviereleasemonth), intval($moviereleaseday), intval($moviereleaseyear)));
Same with $dvdreleasedate.
Re: Array foreach loop proplem
Posted: Thu Jun 03, 2010 3:22 am
by tito85
Hi,
What do you mean by "Echo the values of $moviereleasedate and $dvdreleasedate immediately after they're defined."? These values will be inserted in the database.
Another problem is that if I use the isset() with:
Code: Select all
$genrearray = isset($_POST['ddlGenres']);
no Genres are added to the database. However if I don't use the isset(), genres will be added. But, if I just click the submit buttun a Notice is displayed:
Notice: Undefined index: Genres in C:\wamp\www\movie\addmovie.php on line 11 which is:
Code: Select all
$genrearray = $_POST['ddlGenres'];
Thanks for any help....

Re: Array foreach loop proplem
Posted: Thu Jun 03, 2010 9:11 am
by AbraCadaver
isset() returns true or false, so you are assigning true or false to $genrearray. Do this:
Code: Select all
if(isset($_POST['ddlGenres'])) {
$genrearray = $_POST['ddlGenres'];
} else {
$genrearray = array();
}
or
Code: Select all
$genrearray = isset($_POST['ddlGenres']) ? $_POST['ddlGenres'] : array();
Re: Array foreach loop proplem
Posted: Thu Jun 03, 2010 9:34 am
by tito85
Hi there,
Thanks for your help again.
Genres are now being added, however I still have some problems.
First of all I think that the below code is not working. This should check that the fields of Title, Director and Genre are filled as these are mandatory. However it seems it not working because if I try to add a movie without selecting any genre it still adds the movie to the database. And after it adds the movie the "Only .jpg Images can be uploaded" is displayed even if no images was uploaded.
Code: Select all
if (strlen(trim($title)) > 0 && strlen(trim($director1)) > 0 && $genrearray != "0") {
Any idea why please?
Thanks for your time!
Re: Array foreach loop proplem
Posted: Thu Jun 03, 2010 10:17 am
by AbraCadaver
Your array won't ever be equal to 0. Try:
Code: Select all
if (strlen(trim($title)) > 0 && strlen(trim($director1)) > 0 && !empty($genrearray)) {
Re: Array foreach loop proplem
Posted: Thu Jun 03, 2010 10:39 am
by tito85
That worked fine too.
The only problem left is that if no Movie Release date of DVD Release date is inserted the following Notices appears:
Notice: Undefined offset: 1 in C:\wamp\www\movie\addmovie.php on line 19
Notice: Undefined offset: 2 in C:\wamp\www\movie\addmovie.php on line 20
Code: Select all
$moviereleasemonth = $moviereleasedate[1];
$moviereleaseyear = $moviereleasedate[2];
Notice: Undefined offset: 1 in C:\wamp\www\movie\addmovie.php on line 22
Notice: Undefined offset: 2 in C:\wamp\www\movie\addmovie.php on line 23
Code: Select all
$dvdreleasemonth = $dvdreleasedate[1];
$dvdreleaseyear = $dvdreleasedate[2];
Apart from this, a date is being added to the database automatically. Not all times the data is the same. Sometimes is 1970-01-01 or 1999-11-30. I know 1970-01-01 is something related to mktime but other dates don't know about them...
Thanks for your time
Re: Array foreach loop proplem
Posted: Thu Jun 03, 2010 10:43 am
by AbraCadaver
That's because they don't exist because you exploded and empty value. So you need to check if the dates were submitted and if not, don't explode and don't try and use the date parts.
Re: Array foreach loop proplem
Posted: Thu Jun 03, 2010 10:49 am
by AbraCadaver
Rather than exploding and rebuilding you could do something like:
Code: Select all
if(!empty($_POST['txtmovieReleaseDate'])) {
$moviereleasedate = date("Y-m-d", strtotime($_POST['txtmovieReleaseDate']));
} else {
$moviereleasedate = '';
}