Adding movies into a database

Posted on

Problem

I made a site where you can add movies into the database. You can use up to 3 different hosts.

Everything works well, but it is very long. I would love to shorten it!

Code to enter the 3 hosts + the links to the host:

<TABLE>     
    <TR><TD width=120>Hoster*:</TD>
        <TD>
            <SELECT class="interfaceforms" name="hoster"><OPTION value="">Please select</OPTION>
            <?PHP
                foreach($hoster_list AS $aKey => $aValue) {
                echo'<option value="'.$aKey.'">'.$aValue.'</option>';
                }
            ?>
            </SELECT>
        </TD>
    </TR>
    <TR>
        <TD>&nbsp;
            <BR><a href="#" onClick="document.getElementById('addhoster2').style.display='';return false;">>Add hoster<</a>
        </TD>
        <TD>
            Part 1: <INPUT class="interfaceforms" type="text" name="part1"> link or embed code<BR>
            Part 2: <INPUT class="interfaceforms" type="text" name="part2"><BR>
        </TD>
    </TR>
</TABLE>

<TABLE id="addhoster2" style="display:none;">   
    <TR>
    <TD width=120>Hoster*:</TD>
        <TD>
            <SELECT class="interfaceforms" name="hoster2"><OPTION value="">Please select</OPTION>
            <?PHP
                foreach($hoster_list AS $aKey => $aValue) {
                echo'<option value="'.$aKey.'">'.$aValue.'</option>';
                }
            ?>
            </SELECT>
        </TD>
    </TR>
    <TR>
        <TD>&nbsp;
            <BR><a href="#" onClick="document.getElementById('addhoster3').style.display='';return false;">>Add hoster<</a>
        </TD>
        <TD>
            Part 1: <INPUT class="interfaceforms" type="text" name="part21"> link or embed code<BR>
            Part 2: <INPUT class="interfaceforms" type="text" name="part22"><BR>
        </TD>
    </TR>
</TABLE>

<TABLE id="addhoster3" style="display:none;">   
    <TR>
    <TD width=120>Hoster*:</TD>
        <TD>
            <SELECT class="interfaceforms" name="hoster3"><OPTION value="">Please select</OPTION>
            <?PHP
                foreach($hoster_list AS $aKey => $aValue) {
                echo'<option value="'.$aKey.'">'.$aValue.'</option>';
                }
            ?>
            </SELECT>
        </TD>
    </TR>
    <TR>
        <TD>&nbsp;
            <BR><a href="#" onClick="document.getElementById('addhoster4').style.display='';return false;">>Add hoster<</a>
        </TD>
        <TD>
            Part 1: <INPUT class="interfaceforms" type="text" name="part31"> link or embed code<BR>
            Part 2: <INPUT class="interfaceforms" type="text" name="part32"><BR>
        </TD>
    </TR>
</TABLE>

Now here is the function to add the input into the database:

  if(!empty($_POST['title']) && !empty($_POST['language']) && !empty($_POST['hoster']) && !empty($_POST['part1']) && !empty($_POST['fsk']) && !empty($_POST['description']) && checkimdbID($_POST['imdbID']) ){

  $movie = $imdb->find_by_id($imdbID);

  $inTitle = mysql_real_escape_string($_POST['title']);
  $inName = TitleToDbEncode($movie->title);
  $inDescription = mysql_real_escape_string($_POST['description']);
  $part_1 = mysql_real_escape_string($_POST['part1']);
  $part_2 = mysql_real_escape_string($_POST['part2']);
  $actors = myEncode4($movie->actors);
  $director = myEncode4($movie->director);



    $sqlCmd="INSERT INTO topmovies.movies 
      (imdb_id,username,title,name,language,hoster,part_1,part_2,picturequality,soundquality,genre,cover,description,duration,fsk,countryyear,director,actors,status,cinema,rating)
      VALUES
      ('".$imdbID."','".$_SESSION['user_username']."','".$inTitle."','".$inName."','".$_POST['language']."','".$_POST['hoster']."','".$part_1."','".$part_2."','".$_POST['picturequality']."','".$_POST['soundquality']."','".$movie->genre."','".$bildDatei."','".$inDescription."','".$movie->runtime."','".$_POST['fsk']."','".$movie->year."','".$director."','".$actors."','queued','".$_POST['cinema']."','".$movie->rating."')";
    $sqlQry = mysql_query($sqlCmd,$sqlHp) or die(mysql_error());
    if($sqlQry) {
      echo'Film erfolgreich hinzugef&uumlgt.';
      echo'<meta http-equiv="refresh" content="1; URL=index.php"> ';
    }else { echo'Es ist ein fehler aufgetaucht. Bitte alle felder richtig ausf&uumlllen.'; }
  }else { echo'Es ist ein fehler aufgetaucht. Bitte alle felder richtig ausf&uumlllen.'; }

  if(!empty($_POST['title']) && !empty($_POST['language']) && !empty($_POST['hoster2']) && !empty($_POST['part21']) && !empty($_POST['fsk']) && !empty($_POST['description']) && checkimdbID($_POST['imdbID']) ){

    $inTitle = mysql_real_escape_string($_POST['title']);
  $inName = TitleToDbEncode($movie->title);
  $inDescription = mysql_real_escape_string($_POST['description']);
  $part_1 = mysql_real_escape_string($_POST['part21']);
  $part_2 = mysql_real_escape_string($_POST['part22']);
  $actors = myEncode4($movie->actors);
  $director = myEncode4($movie->director);


    $sqlCmd="INSERT INTO topmovies.movies 
      (imdb_id,username,title,name,language,hoster,part_1,part_2,picturequality,soundquality,genre,cover,description,duration,fsk,countryyear,director,actors,status,cinema,rating)
      VALUES
      ('".$imdbID."','".$_SESSION['user_username']."','".$inTitle."','".$inName."','".$_POST['language']."','".$_POST['hoster2']."','".$part_1."','".$part_2."','".$_POST['picturequality']."','".$_POST['soundquality']."','".$movie->genre."','".$bildDatei."','".$inDescription."','".$movie->runtime."','".$_POST['fsk']."','".$movie->year."','".$director."','".$actors."','queued','".$_POST['cinema']."','".$movie->rating."')";
    $sqlQry = mysql_query($sqlCmd,$sqlHp) or die(mysql_error());
    }

  if(!empty($_POST['title']) && !empty($_POST['language']) && !empty($_POST['hoster3']) && !empty($_POST['part31']) && !empty($_POST['fsk']) && !empty($_POST['description']) && checkimdbID($_POST['imdbID']) ){

    $inTitle = mysql_real_escape_string($_POST['title']);
  $inName = TitleToDbEncode($movie->title);
  $inDescription = mysql_real_escape_string($_POST['description']);
  $part_1 = mysql_real_escape_string($_POST['part31']);
  $part_2 = mysql_real_escape_string($_POST['part32']);
  $actors = myEncode4($movie->actors);
  $director = myEncode4($movie->director);


    $sqlCmd="INSERT INTO topmovies.movies 
      (imdb_id,username,title,name,language,hoster,part_1,part_2,picturequality,soundquality,genre,cover,description,duration,fsk,countryyear,director,actors,status,cinema,rating)
      VALUES
      ('".$imdbID."','".$_SESSION['user_username']."','".$inTitle."','".$inName."','".$_POST['language']."','".$_POST['hoster3']."','".$part_1."','".$part_2."','".$_POST['picturequality']."','".$_POST['soundquality']."','".$movie->genre."','".$bildDatei."','".$inDescription."','".$movie->runtime."','".$_POST['fsk']."','".$movie->year."','".$director."','".$actors."','queued','".$_POST['cinema']."','".$movie->rating."')";
    $sqlQry = mysql_query($sqlCmd,$sqlHp) or die(mysql_error());
    }

I want to shorten the function if possible. I think I could do something like this:

<SELECT class="interfaceforms" name="hoster[1]"><OPTION value="">Please select</OPTION>
Part 1: <INPUT class="interfaceforms" type="text" name="part[1][0]"><BR>
Part 2: <INPUT class="interfaceforms" type="text" name="part[1][1]"><BR>

Host 2 would be:

<SELECT class="interfaceforms" name="hoster[2]"><OPTION value="">Please select</OPTION>
Part 1: <INPUT class="interfaceforms" type="text" name="part[2][0]"><BR>
Part 2: <INPUT class="interfaceforms" type="text" name="part[2][1]"><BR>

Solution

Use a loop to shorten your html:

<?php for ($i = 0; $i <= 2; ++$i): ?>
    <TABLE id="addhoster<?php echo $i + 1; ?>" <?php if ($i > 0) echo 'style="display: none"'; ?>>     
        <TR><TD width=120>Hoster*:</TD>
            <TD>
                <SELECT class="interfaceforms" name="addmovie[<?php echo $i; ?>][hoster]"><OPTION value="">Please select</OPTION>
                <?PHP
                    foreach($hoster_list AS $aKey => $aValue) {
                    echo'<option value="'.$aKey.'">'.$aValue.'</option>';
                    }
                ?>
                </SELECT>
            </TD>
        </TR>
        <TR>
            <TD>&nbsp;
                <BR><a href="#" onClick="document.getElementById('addhoster<?php echo $i + 2; ?>').style.display='';return false;">>Add hoster<</a>
            </TD>
            <TD>
                Part 1: <INPUT class="interfaceforms" type="text" name="addmovie[<?php echo $i; ?>][part]"> link or embed code<BR>
                Part 2: <INPUT class="interfaceforms" type="text" name="addmovie[<?php echo $i; ?>][part2]"><BR>
            </TD>
        </TR>
    </TABLE>
<?php endfor; ?>

Then stop empty hosts inserting by checking if addMovie[key][hoster] is set:

$result = $_POST["addmovie"];

if (!empty($result))
{
    $new = array();

    foreach ($result as $key)
    {
        if ($key['hoster'] != '')
        {
            $new[] = "('" . $key["hoster"] . "', '" . $key["part"] . "', '" . $key["part2"] . "')";
        }
    }

    $query = mysql_query("INSERT INTO topmovies.movies2 (hoster, part_1, part_2) VALUES " . implode(', ', $new));

    if ($query)
    {
        echo 'SUCCESS';
        print_r($new);
        echo '<BR>';
    }
    else
    {
        echo 'FAILED';
    }
}

Here is one way of shortening your HTML. A lot of it is identifying the areas that repeat themselves, and then looping it around the size of your host array, for future proofing if you increase the amount of hosts the tables produced will naturally increase with it.

The below code though will change your expected post results of $_POST['part1'] to $_POST['part11'].

<?php
//Setup a bogus array of hosts
$hoster_list[] = "host1";
$hoster_list[] = "host2";
$hoster_list[] = "host3";

$i=1;
while($i<=sizeof($hoster_list)) {
    //If we are the 1st host then produce a visible table else produce the incremented table
    echo ($i == 1) ? "<TABLE>" : "<TABLE id='addhoster".$i."' style='display:none;'>";

    //For each loop increment the same html
    echo " 
            <TR><TD width=120>Hoster*:</TD>
                <TD>
                    <SELECT class='interfaceforms' name='hoster".$i."'><OPTION value=''>Please select</OPTION>
                    ";
                        //Perform option dump
                        foreach($hoster_list AS $aKey => $aValue) {
                            echo'<option value="'.$aKey.'">'.$aValue.'</option>';
                        }
                    echo "
                    </SELECT>
                </TD>
            </TR>
            <TR>
                <TD>
                    <BR><a href='#' onClick='document.getElementById('addhoster".$i."').style.display='';return false;'>>Add hoster<</a>
                </TD>
                <TD>
                    Part 1: <INPUT class='interfaceforms' type='text' name='part".$i."1'> link or embed code<BR>
                    Part 2: <INPUT class='interfaceforms' type='text' name='part".$i."2'><BR>
                </TD>
            </TR>
        </TABLE>
    ";

    $i++;
}
?>  

Working example of this part.

I fixed it up a little bit. The problem now, is that he keeps doing 3 queries even if I have only selected 1 or 2 hosts.

<TABLE>     
    <TR>
        <TD width=120>Hoster*:</TD>
        <TD>
            <SELECT class="interfaceforms" name="addmovie[0][hoster]">
                <OPTION value="">Please select</OPTION>
                <?PHP
                foreach($hoster_list AS $aKey => $aValue) {
                    echo'<option value="'.$aKey.'">'.$aValue.'</option>';
                }
                ?>
        </SELECT>
        </TD>
    </TR>
    <TR>
        <TD>&nbsp;
        <BR><a href="#" onClick="document.getElementById('addhoster2').style.display='';return false;">>Add hoster<</a>
        </TD>
        <TD>
            Part 1: <INPUT class="interfaceforms" type="text" name="addmovie[0][part]"> link or embed code<BR>
            Part 2: <INPUT class="interfaceforms" type="text" name="addmovie[0][part2]"><BR>
        </TD>
    </TR>
</TABLE>
<TABLE id="addhoster2" style="display:none;">   
    <TR>
        <TD width=120>Hoster*:</TD>
        <TD>
            <SELECT class="interfaceforms" name="addmovie[1][hoster]">
                <OPTION value="">Please select</OPTION>
                <?PHP
                foreach($hoster_list AS $aKey => $aValue) {
                    echo'<option value="'.$aKey.'">'.$aValue.'</option>';
                }
                ?>
            </SELECT>
        </TD>
    </TR>
    <TR>
        <TD>&nbsp;
            <BR><a href="#" onClick="document.getElementById('addhoster3').style.display='';return false;">>Add hoster<</a>
        </TD>
        <TD>
            Part 1: <INPUT class="interfaceforms" type="text" name="addmovie[1][part]"> link or embed code<BR>
            Part 2: <INPUT class="interfaceforms" type="text" name="addmovie[1][part2]"><BR>
        </TD>
    </TR>
</TABLE>
<TABLE id="addhoster3" style="display:none;">   
    <TR>
        <TD width=120>Hoster*:</TD>
        <TD>
            <SELECT class="interfaceforms" name="addmovie[2][hoster]">
                <OPTION value="">Please select</OPTION>
                <?PHP
                foreach($hoster_list AS $aKey => $aValue) {
                    echo'<option value="'.$aKey.'">'.$aValue.'</option>';
                }
                ?>
            </SELECT>
        </TD>
    </TR>
    <TR>
        <TD>&nbsp;
            <BR><a href="#" onClick="document.getElementById('addhoster4').style.display='';return false;">>Add hoster<</a>
        </TD>
        <TD>
            Part 1: <INPUT class="interfaceforms" type="text" name="addmovie[2][part]"> link or embed code<BR>
            Part 2: <INPUT class="interfaceforms" type="text" name="addmovie[2][part2]"><BR>
        </TD>
    </TR>
</TABLE>

Code:

$result = $_POST["addmovie"];

if (count($result > 0))
    $new = array();

foreach ($result as $key)
{
    $new[] = "('" . $key["hoster"] . "', '" . $key["part"] . "', '" . $key["part2"] . "')";
}

if (count($new) > 0)
{
    $query = mysql_query("INSERT INTO topmovies.movies2 (hoster, part_1, part_2) VALUES " . implode(', ', $new));
    if ($query)
    {
        echo 'SUCCESS';
        print_r($new);
        echo '<BR>';
    }
    else
    {
        echo 'FAILED';
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *