JAnders1410 Posted June 22, 2006 Share Posted June 22, 2006 Hi everyone! Below is a script that I have written to allow a user to imput song information into a database, but something is wrong with it. I'm new to PHP and this script is borrowed from a textbook that I've been using to teach myself. I have modified the script to fit my needs. Basically I think one of my data tests is not working since that is the error message I keep getting but I can't figure out how to resolve it. Any help that you can provide would be greatly appreciated! If you would like to see the script in action the address is [a href=\"http://www.college-rock.com/redhail/conrol/add_song.php\" target=\"_blank\"]www.college-rock.com/redhail/control/add_song.php[/a][code]1<?php # Script 12. 5 - add_song.php2 // This page allows users to add URLs to the database.3 4 //Set the page title and include the HTML header.5 $page_title = 'Add a Song';6 include ('./includes/header.html');7 8 require_once('./php/mysql_connect.php'); //Connect to the database.9 10 if (isset($_POST['submitted'])) { // Handle the form11 12 // Check for Genre13 if (isset($_POST['genre']) && (is_array($_POST['genre']))) {14 $genre = TRUE;15 } else {16 $genre = FALSE;17 echo '<p><font color="red">Please select at least one genre!</font></p>';18 }19 20 // Check for a Composer Name.21 if (!empty($_POST['composer'])) {22 $c = escape_data($_POST['composer']);23 } else {24 $c = FALSE;25 echo '<p><font color="red">Please enter the composer´s name!</font></p>';26 }27 28 // Check for a Artist Name.29 if (!empty($_POST['artist'])) {30 $a = escape_data($_POST['artist']);31 } else {32 $a = FALSE;33 echo '<p><font color="red">Please enter an artist´s name!</font></p>';34 }35 36 // Check for a Album Name.37 if (!empty($_POST['album'])) {38 $r = escape_data($_POST['album']);39 } else {40 $r = FALSE;41 echo '<p><font color="red">Please enter the album name or list as "Self Titled"!</font></p>';42 }43 44 // Check for Song Title.45 if (!empty($_POST['song'])) {46 $s = escape_data($_POST['song']);47 } else {48 $s = FALSE;49 echo '<p><font color="red">Please enter the title of the song!</font></p>';50 }51 52 // Check for a BPM.53 if (isset($_POST['BPM']) && (is_array($_POST['BPM']))) {54 $BPM = TRUE;55 } else {56 $BPM = FALSE;57 echo '<p><font color="red">Please select one BPM category!</font></p>';58 }59 60 // Check for lyrics.61 if (!empty($_POST['lyrics'])) {62 $l = escape_data($_POST['lyrics']);63 } else {64 $l = FALSE;65 echo '<p><font color="red">Please enter the lyrics of the song! If there are no lyrics please write "Instrumental".</font></p>';66 }67 68 // Check for keywords.69 if (!empty($_POST['keywords'])) {70 $k = escape_data($_POST['keywords']);71 } else {72 $k = FALSE;73 echo '<p><font color="red">Please enter the searchable keywords for the song! This will assist in more efficient searching for our customers.</font></p>';74 }75 76 // Check for image.77 if (!empty($_POST['image'])) {78 $i = escape_data($_POST['image']);79 } else {80 $i = FALSE;81 echo '<p><font color="red">Please enter the image file path of the song! All images should be uploaded seperately to the /art/ folder.</font></p>';82 }83 84 // Check for Song File.85 if (!empty($_POST['sound'])) {86 $sound = escape_data($_POST['sound']);87 } else {88 $sound = FALSE;89 echo '<p><font color="red">Please enter the song file path of the song! All song files should be uploaded sperately to the /sound/ folder.</font></p>';90 }91 if ($genre && $c && $a && $r && $s && $BMP && $l && $k && $i && $sound) { // If everything is Okay...92 93 // Add the Song to the Song table.94 $query = "INSERT INTO song (genre, composer, artist, album, song, BPM, lyrics, keywords, image, sound) VALUES ('$genre', '$c', '$a', '$r', '$s', '$BMP', '$l', '$k', '$i', '$sound')";95 $result = @mysql_query ($query); // Run the query.96 $uid = @mysql_insert_id(); // Get the song ID.97 98 if($uid > 0) { //New Song has been added.99 100 // Make the Song associations.101 102 // Build the query103 $query = 'INSERT INTO song_associations (song_id, song_category_id, approved) VALUES ';104 foreach ($_POST['types'] as $v) {105 $query .= "($uid, $v, 'Y'), ";106 }107 $query = substr ($query, 0, -2); //Chop off the last comma and space.108 109 $result = @mysql_query ($query); //Run the query110 111 if (mysql_affected_rows() == count($_POST['types'])) { //Query ran OK112 113 echo '<p><b>This song has been successfully added!</p></b>';114 $_POST = array(); // Reset values115 116 } else { // If second query did not run OK117 118 echo '<p><font color="red">Your submission could not be processed due to a system error. We apologize for any inconvenience. Either try again or contact John with the following error:</font></p>'; //Public message119 echo '<p><font color="red">' .mysql_error() . '<br /><br />Query: ' .$query .'</font></p>'; // Debugging Message120 121 // Delete the URL from the urls table.122 $query = "DELETE FROM song WHERE song_id=$uid";123 @mysql_query ($query); //Run the query124 125 } // End of mysql_affected_rows() If.126 127 } else { //If first query did not run OK128 echo '<p><font color="red">Your submission could not be processed to a system error. We apologize for any inconvenience.</font></p>'; //Public Message129 echo '<p><font color="red">' .mysql_error() . '<br /><br />Query: ' .$query . '</font></p>'; // Debugging message130 }131 132 } else { // If one of the data tests failed.133 echo '<p><font color="red">Please try again. A data test failed!</font></p>';134 }135 136 } //End of the main submitted conditional.137 // ---------------- DISPLAY THE FORM -------------------138 ?>139<style type="text/css">140<!--141.style1 {142 font-size: medium;143 font-weight: bold;144}145-->146</style>147148149150 <form action="add_song.php" method="post">151 <fieldset>152 <legend class="style1">Add a song to Redhail Media Database:</legend>153 <p><b>Genre:</b><br>154 155 <select name="genre[]" multiple="multiple" size="5">156 <?php // Create the pull-down menu information157 $query = "SELECT * FROM genre ORDER BY genre ASC";158 $result = @mysql_query ($query);159 while ($row = mysql_fetch_array($result, MYSQL_NUM)) {160 echo "<option value=\"$row[0]\"";161 // Make sticky, if necessary.162 if (isset($_POST['types']) && (in_array($row[0], $_POST['types']))) {163 echo ' selected="selected"';164 }165 echo ">$row[1]</option>\n";166 }167 ?>168 </select>169 <br>170 <br>171 <b>Composer Name(s): </b> 172 <input type="text" name="composer" size="60" maxlength="60" value="<?php if (isset($_POST['composer'])) echo $_POST['composer']; ?>" />173 </p>174 <p><b>Artist's Name: </b>175 <input type="text" name="artist" size="60" maxlength="60" value="<?php if (isset($_POST['artist'])) echo $_POST['artist']; ?>" />176 <br />177 <br>178 <b>Album Name: </b> <input type="text" name="album" size="60" maxlength="60" value="<?php if (isset($_POST['album'])) echo $_POST['album']; ?>" />179 <br>180 <em>For albums with no name please list as "Self Titled" </em></p>181 <p><b>Song Title: </b>182 <input type="text" name="song" size="60" maxlength="60" value="<?php if (isset($_POST['song'])) echo $_POST['song']; ?>" />183 </p>184 <p><b>BPM: </b>185 <select name="BPM[]" size="2" multiple="multiple" id="BPM[]">186 <?php // Create the pull-down menu information187 $query = "SELECT * FROM BPM ORDER BY BPM ASC";188 $result = @mysql_query ($query);189 while ($row = mysql_fetch_array($result, MYSQL_NUM)) {190 echo "<option value=\"$row[0]\"";191 // Make sticky, if necessary.192 if (isset($_POST['types']) && (in_array($row[0], $_POST['types']))) {193 echo ' selected="selected"';194 }195 echo ">$row[1]</option>\n";196 }197 ?>198 </select>199</p>200 <b>Lyrics: </b>201 <textarea name="lyrics" cols="40" rows="5"><?PHP if (isset($_POST['lyrics'])) echo $_POST ['lyrics']; ?>202 </textarea>203<br>204 <em>For songs without lyrics please write "Instrumental"205 </em>206 <p><b>Keywords/Mood: </b> 207 <textarea type="text" name="keywords" cols="40" rows="5"><?PHP if (isset($_POST['keywords'])) echo $_POST ['keywords']; ?>208 </textarea>209 </p>210 <p><b>HTML Image Path: </b>211 <input type="text" name="image" size="60" maxlength="60" value="<?php if (isset($_POST['image'])) echo $_POST['image']; ?>" />212 213 <br>214 <em>Format like "/art/{Album Name Here}"215</em></p>216 <p><b>HTML Song Path: </b>217 <input type="text" name="sound" size="60" maxlength="60" value="<?php if (isset($_POST['sound'])) echo $_POST['sound']; ?>" />218 <br>219 <em>Format like "/sound/{Song Title Here}" </em>220 </p>221 </fieldset>222 <input type="hidden" name="submitted" value="TRUE" />223 224 <div align="center"><input type="submit" name="submit" value="Submit" /></div>225 226</form>227 <?php228 mysql_close(); //Close the database connection.229 include ('./includes/footer.html');230 ?>[/code] Link to comment https://forums.phpfreaks.com/topic/12660-help-im-stuck/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.