I have this code that inserts the mp3s from a folder into a mysql database. I need it to check to see if the file being inserted is already in the database. If it is it will not be inserted or if the file is removed from the folder the database gets updated accordingly. I tried to do something with the $id, but no luck? Can you please look at this code and help me figure out this mess? Many thanks.
<?php
$artist = "bandname";
mysql_connect("localhost","name","pass");
mysql_select_db("mp3s");
if ($submit) {
// here if no ID then adding else we're editing
$id = "1";
// open directory
$dir = opendir("/bandname/song");
// loop through the directory while reading it's contents
while ($file = readdir($dir)) {
// if the current file in the loop is a mp3, then inset into database
if (ereg("mp3$", $file)) {
$title = $file;
$fileurl = "http://www.mysite.com/bandname/songs/$file ";
$get_file = $file;
$result = mysql_query("SELECT title FROM songs WHERE title LIKE '$get_file'");
//grab all the content
while($r=mysql_fetch_array($result))
{
$song_name=$r["title"];
//display the row
$id = $song_name;
if ($id) {
$sql = "UPDATE songlist SET filename='$fileurl',artist='$artist', title='$title' WHERE id=$id";
} else {
$sql = "INSERT INTO songlist (artist,filename,title)
VALUES ('$artist','$fileurl','$title')";}
$result = mysql_query($sql);
echo "Record updated/edited!<p>";
}
}
}
// run SQL against the DB
} elseif ($delete) {
// delete a record
$sql = "DELETE FROM songlist WHERE id=$id";
$result = mysql_query($sql);
echo "$sql Record deleted!<p>";
} else {
// this part happens if we don't press submit
if (!$id) {
// print the list if there is not editing
$result = mysql_query("SELECT * FROM songlist ",$db);
while ($myrow = mysql_fetch_array($result)) {
printf("<a href=\"%s?id=%s\">%s %s</a> \n", $PHP_SELF, $myrow["id"], $myrow["artist"], $myrow["fileurl"], $myrow["title"]);
printf("<a href=\"%s?id=%s&delete=yes\">(DELETE)</a><br>", $PHP_SELF, $myrow["id"]);
}
}
?>
<P>
<a>ADD YOUR BAND</a>
<P>
<form method="post" action="<?php echo $PHP_SELF?>">
<?php
if ($id) {
// editing so select a record
$sql = "SELECT * FROM songlist WHERE id=$id";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
$id = $myrow["id"];
$artist = $myrow["artist"];
$fileurl = $myrow["filename"];
$title = $myrow["title"];
// print the id for editing
?>
<input type=hidden name="id" value="<?php echo $id ?>">
<input type=hidden name="artist" value="<?php echo $artist ?>">
<input type=hidden name="filename" value="<?php echo $fileurl ?>">
<input type=hidden name="title" value="<?php echo $title ?>">
<?php
}
?>
<input type="Submit" name="submit" value="Finish">
</form>
<?php
}
?>