Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!--
- 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".
- This file handles both the displaying of the form, with any existing database entries displayed, as well as the processing of the form.
- On processing, it clears the database table completely, and then repopulates it with what was submitted on the form.
- This is NOT the best solution, as it loses any equivalency relationship with the previous values, and could also lead to
- the database being accidentally wiped.
- A direct copy and paste of this will NOT display properly, as I took out the calls to my CSS files.
- -->
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>Modify List</title>
- <?php
- //Retrieve updated info from form if already modified the artist
- $newKeywords = $_POST['keyword'];
- //Connect to the Database
- mysql_connect(" <!-- Database Server --> ", " <!-- DB User --> ", " <!-- DB Password --> ") or die(mysql_error());
- mysql_select_db(" <!-- Database --> ") or die(mysql_error());
- ?>
- <script type="text/javascript" language="javascript">
- <!--
- function addKeyword()
- {
- var keywordObj = document.getElementById("keywordForm");
- var count = document.getElementById("keywordCount").value;
- var newObj = document.createElement("div");
- newObj.setAttribute("id", "keywordInput_" + count);
- newObj.innerHTML = "<input type='text' name='keyword[" + count + "]' /> " +
- " <input type='button' value='Delete' onClick='delKeyword(" + count + ")' />";
- keywordObj.appendChild(newObj);
- count++;
- document.getElementById("keywordCount").value = count;
- }
- function delKeyword(i)
- {
- var div = document.getElementById("keywordForm");
- var delDiv = document.getElementById("keywordInput_" + i);
- div.removeChild(delDiv);
- }
- -->
- </script>
- </head>
- <p align=center>
- <font color=ffffff><b>Modify List</b></font>
- <?php
- //If the list was updated and we are now processing the form, then update the database
- if(isset($newKeywords))
- {
- //First clear the table
- mysql_query("DELETE FROM `artists_genres`") or die(mysql_error());
- //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
- sort($newKeywords);
- //Now run through the array of new genres
- $i=0;
- for($i;$i<sizeof($newKeywords);$i++)
- {
- //Make sure this one is not blank or doesn't already exist in the database (duplicate entries on one form)
- if(($newKeywords[$i] != '') && (mysql_num_rows(mysql_query("SELECT * FROM artists_genres WHERE (genre = '$newKeywords[$i]')")) == 0))
- {
- //Make this one safe to enter into the database (escaping bad characters)
- $word = mysql_real_escape_string($newKeywords[$i]);
- //Insert this one into the database
- mysql_query("INSERT INTO artists_genres (genre) VALUES ('$word')") or die(mysql_error());
- }
- }
- echo "<br /><font color=ffffff><b>List updated.</b></font><p />";
- }
- //Show the form with the values in the database prepopulated, whether or not we just updated the database.
- echo "
- <form enctype=\"multipart/form-data\" action=\"mod_genre.php\" method=\"post\">
- <!--This div holds all of the form fields -->
- <div id=\"keywordForm\">";
- //Pull the preexisting values
- $result = mysql_query("SELECT * FROM artists_genres");
- //Keep track of how many preexisting values we have, so that when we add new ones it knows with which ID number to start
- $kPreCount = 0;
- //Add a field for each preexisting value, housed in a DIV so that it can be removed if desired
- while($row=mysql_fetch_array($result))
- {
- echo "<div id=\"keywordInput_" . $kPreCount . "\"><input type=\"text\" name='keyword[" . $kPreCount . "]' value=\"" . $row['genre'] . "\"> <input type='button' value='Delete' onClick='delKeyword(" . $kPreCount . ")' /></div>";
- $kPreCount++;
- }
- echo "
- </div>
- <!--This holds the value of how many entries we have already displayed, so that the javascript knows where to start-->
- <input type=\"hidden\" id=\"keywordCount\" value=\"" . $kPreCount . "\" />
- <!--Now show the Add button for adding new fields -->
- <input type=\"button\" value=\"Add\" onClick=\"addKeyword();\" />
- <p />
- <input type=\"submit\" value=\"Submit\">
- </form>";
- ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement