SHARE
TWEET

MichThom

a guest Nov 26th, 2008 236 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!--
  2.  
  3. This is mod_genre.php. It updates a list of genres in a mySQL database. The table is "artists_genres", and has one column, "genre".
  4.  
  5. This file handles both the displaying of the form, with any existing database entries displayed, as well as the processing of the form.
  6.  
  7. On processing, it clears the database table completely, and then repopulates it with what was submitted on the form.
  8. This is NOT the best solution, as it loses any equivalency relationship with the previous values, and could also lead to
  9. the database being accidentally wiped.
  10.  
  11. A direct copy and paste of this will NOT display properly, as I took out the calls to my CSS files.
  12.  
  13. -->
  14.  
  15.  
  16. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  17. <html>
  18. <head>
  19. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  20.  
  21. <title>Modify  List</title>
  22. <?php
  23.  
  24. //Retrieve updated info from form if already modified the artist
  25. $newKeywords = $_POST['keyword'];
  26.  
  27. //Connect to the Database
  28.    mysql_connect(" <!-- Database Server --> ", " <!-- DB User --> ", " <!-- DB Password --> ") or die(mysql_error());
  29.    mysql_select_db(" <!-- Database --> ") or die(mysql_error());
  30.  
  31. ?>
  32.  
  33. <script type="text/javascript" language="javascript">
  34. <!--
  35.  
  36. function addKeyword()
  37. {
  38. var keywordObj = document.getElementById("keywordForm");
  39. var count = document.getElementById("keywordCount").value;
  40. var newObj = document.createElement("div");
  41. newObj.setAttribute("id", "keywordInput_" + count);
  42. newObj.innerHTML = "<input type='text' name='keyword[" + count + "]' /> " +
  43. " <input type='button' value='Delete' onClick='delKeyword(" + count + ")' />";
  44. keywordObj.appendChild(newObj);
  45. count++;
  46. document.getElementById("keywordCount").value = count;
  47. }
  48.  
  49.  
  50. function delKeyword(i)
  51. {
  52. var div = document.getElementById("keywordForm");
  53. var delDiv = document.getElementById("keywordInput_" + i);
  54. div.removeChild(delDiv);
  55. }
  56. -->
  57. </script>
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64. </head>
  65. <p align=center>
  66. <font color=ffffff><b>Modify List</b></font>
  67.  
  68. <?php
  69.  
  70. //If the list was updated and we are now processing the form, then update the database
  71. if(isset($newKeywords))
  72. {
  73.         //First clear the table
  74.      mysql_query("DELETE FROM `artists_genres`") or die(mysql_error());
  75.  
  76.         //Now sort the array of new genres, so that there will not be any array ID's with blank values if the user left a middle field blank
  77. sort($newKeywords);
  78.  
  79.         //Now run through the array of new genres
  80.         $i=0;
  81.         for($i;$i<sizeof($newKeywords);$i++)
  82.         {
  83.            //Make sure this one is not blank or doesn't already exist in the database (duplicate entries on one form)
  84.            if(($newKeywords[$i] != '') && (mysql_num_rows(mysql_query("SELECT * FROM artists_genres WHERE (genre = '$newKeywords[$i]')")) == 0))
  85.            {
  86.              //Make this one safe to enter into the database (escaping bad characters)
  87.               $word = mysql_real_escape_string($newKeywords[$i]);
  88.  
  89.              //Insert this one into the database
  90.               mysql_query("INSERT INTO artists_genres (genre) VALUES ('$word')") or die(mysql_error());
  91.            }
  92.         }
  93.  
  94.      echo "<br /><font color=ffffff><b>List updated.</b></font><p />";
  95. }
  96.  
  97.  
  98. //Show the form with the values in the database prepopulated, whether or not we just updated the database.
  99.    echo "
  100. <form enctype=\"multipart/form-data\" action=\"mod_genre.php\" method=\"post\">
  101.  
  102. <!--This div holds all of the form fields -->
  103. <div id=\"keywordForm\">";
  104.  
  105. //Pull the preexisting values
  106.    $result = mysql_query("SELECT * FROM artists_genres");
  107.  
  108. //Keep track of how many preexisting values we have, so that when we add new ones it knows with which ID number to start
  109.    $kPreCount = 0;
  110.  
  111. //Add a field for each preexisting value, housed in a DIV so that it can be removed if desired
  112.    while($row=mysql_fetch_array($result))
  113.    {
  114.       echo "<div id=\"keywordInput_" . $kPreCount . "\"><input type=\"text\" name='keyword[" . $kPreCount . "]' value=\"" . $row['genre'] . "\"> <input type='button' value='Delete' onClick='delKeyword(" . $kPreCount . ")' /></div>";
  115.       $kPreCount++;
  116.    }
  117.  
  118.    echo "
  119. </div>
  120.  
  121. <!--This holds the value of how many entries we have already displayed, so that the javascript knows where to start-->
  122. <input type=\"hidden\" id=\"keywordCount\" value=\"" . $kPreCount . "\" />
  123.  
  124. <!--Now show the Add button for adding new fields -->
  125. <input type=\"button\" value=\"Add\" onClick=\"addKeyword();\" />
  126. <p />
  127. <input type=\"submit\" value=\"Submit\">
  128. </form>";
  129.  
  130.  
  131. ?>
  132.  
  133.  
  134. </body>
  135. </html>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top