[fixed] Having problems with my for loop using explode()

  • wick3dsentinel
  • Born
  • Born
  • wick3dsentinel
  • Posts: 2

Post 3+ Months Ago

Ok, I have this movie database where I can track movies I own, want, and/or are upcoming movies.
I am having a problem with the following piece of code. It takes the value of a textarea ($genre) which can have multiple items separated with "; ". It then explodes the string and then goes through the array and checks the "genres" table to see if that item exist. If it does exist it adds a relationship in the table "lookup_genres". However, if it doesn't exist, it creates a new row in the "genres" table and adds the relationship in the "lookup_genres" table.

The problem I am having is when the for loop goes through the second time, it fails at: (The query dies and kicks out the error message.)
Code: [ Select ]
$complete1 = mysql_query("INSERT INTO genres (id, genre) VALUES('$genre_id','$newGenre')") or die ("Couldn't execute query 2. $newGenre"); // insert new genre into genres table


Note: It works perfectly the first time through.

Any ideas to why this is happening?

Code: [ Select ]
 
if($genre != ''){// If genre is NOT blank do the following
            $genre_split = explode("; ", $genre);      
            $finish = count($genre_split);
            for($i = 0;$i < $finish;$i++){
              $newGenre = $genre_split[$i];
              $result = mysql_query("SELECT * FROM genres WHERE genre='$newGenre'");
              $count = mysql_num_rows($result);
              if($count != 0){
                $row = mysql_fetch_array($result);
                $genre_id = $row['id'];
                $complete = mysql_query("INSERT INTO lookup_genres (movie_id, genre_id) VALUES('$movieId','$genre_id')") or die ("Couldn't execute query 1. $newGenre");
                if(isset($complete)){
                  echo $genre_split[$i]."<br />";
                }
              }//END IF ($count != 0)
              else{
                $genre_id = date('ymdHis'); // create id for genre
                $complete1 = mysql_query("INSERT INTO genres (id, genre) VALUES('$genre_id','$newGenre')") or die ("Couldn't execute query 2. $newGenre"); // insert new genre into genres table
                if(isset($complete1)){
                  $complete2 = mysql_query("INSERT INTO lookup_genres (movie_id, genre_id) VALUES('$movieId','$genre_id')") or die ("Couldn't execute query 3. $newGenre");
                  unset($complete1);
                  if(isset($complete2)){
                    echo $genre_split[$i]."<br />";
                    unset($complete2);
                  }
                }
              }//END ELSE
            }      
               
          }//END IF ($genre != '')
          else {
            echo "<p class=\"alert\">Please fill in at least one genre or close this page.</p>";
          }
 
  1.  
  2. if($genre != ''){// If genre is NOT blank do the following
  3.             $genre_split = explode("; ", $genre);      
  4.             $finish = count($genre_split);
  5.             for($i = 0;$i < $finish;$i++){
  6.               $newGenre = $genre_split[$i];
  7.               $result = mysql_query("SELECT * FROM genres WHERE genre='$newGenre'");
  8.               $count = mysql_num_rows($result);
  9.               if($count != 0){
  10.                 $row = mysql_fetch_array($result);
  11.                 $genre_id = $row['id'];
  12.                 $complete = mysql_query("INSERT INTO lookup_genres (movie_id, genre_id) VALUES('$movieId','$genre_id')") or die ("Couldn't execute query 1. $newGenre");
  13.                 if(isset($complete)){
  14.                   echo $genre_split[$i]."<br />";
  15.                 }
  16.               }//END IF ($count != 0)
  17.               else{
  18.                 $genre_id = date('ymdHis'); // create id for genre
  19.                 $complete1 = mysql_query("INSERT INTO genres (id, genre) VALUES('$genre_id','$newGenre')") or die ("Couldn't execute query 2. $newGenre"); // insert new genre into genres table
  20.                 if(isset($complete1)){
  21.                   $complete2 = mysql_query("INSERT INTO lookup_genres (movie_id, genre_id) VALUES('$movieId','$genre_id')") or die ("Couldn't execute query 3. $newGenre");
  22.                   unset($complete1);
  23.                   if(isset($complete2)){
  24.                     echo $genre_split[$i]."<br />";
  25.                     unset($complete2);
  26.                   }
  27.                 }
  28.               }//END ELSE
  29.             }      
  30.                
  31.           }//END IF ($genre != '')
  32.           else {
  33.             echo "<p class=\"alert\">Please fill in at least one genre or close this page.</p>";
  34.           }
  35.  
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8397
  • Loc: USA

Post 3+ Months Ago

Which error message does it produce? "Couldn't execute query #. $newGenre" or "<p class=\"alert\">Please fill in at least one genre or close this page.</p>" or something else entirely?

How are you putting your stuff in the text area?

#1:
somethign1; something2; something3; something4; ...

or

#2:
something1;
something2;
something3;
seomthing4;
...

Because if it's #2 way, then the split isn't "; " but ":\n" (Just noting in case).

Also, the error could be from your first $result... add or die(mysql_error()); at the first mysql_query(); and for others, change the or die("..."); to or die(mysql_error());. That should be a bit more descriptive for you.
  • wick3dsentinel
  • Born
  • Born
  • wick3dsentinel
  • Posts: 2

Post 3+ Months Ago

Bogey wrote:
Which error message does it produce? "Couldn't execute query #. $newGenre" or "<p class=\"alert\">Please fill in at least one genre or close this page.</p>" or something else entirely?

How are you putting your stuff in the text area?

#1:
somethign1; something2; something3; something4; ...

or

#2:
something1;
something2;
something3;
seomthing4;
...

Because if it's #2 way, then the split isn't "; " but ":\n" (Just noting in case).

Also, the error could be from your first $result... add or die(mysql_error()); at the first mysql_query(); and for others, change the or die("..."); to or die(mysql_error());. That should be a bit more descriptive for you.


I'm putting it in like #1... I didnt even think to use mysql_error() which I normally use, it is giving me the answer to my problem. It says, "Duplicate entry '090227203538' for key 1" so I know what I need to do. Thanks for the help.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8397
  • Loc: USA

Post 3+ Months Ago

wick3dsentinel wrote:
Bogey wrote:
Which error message does it produce? "Couldn't execute query #. $newGenre" or "<p class=\"alert\">Please fill in at least one genre or close this page.</p>" or something else entirely?

How are you putting your stuff in the text area?

#1:
somethign1; something2; something3; something4; ...

or

#2:
something1;
something2;
something3;
seomthing4;
...

Because if it's #2 way, then the split isn't "; " but ":\n" (Just noting in case).

Also, the error could be from your first $result... add or die(mysql_error()); at the first mysql_query(); and for others, change the or die("..."); to or die(mysql_error());. That should be a bit more descriptive for you.


I'm putting it in like #1... I didnt even think to use mysql_error() which I normally use, it is giving me the answer to my problem. It says, "Duplicate entry '090227203538' for key 1" so I know what I need to do. Thanks for the help.

Alright... your welcome :D

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 150 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.