Jump to content

Need help debugging PHP program


jameshay

Recommended Posts

I can't figure out what's wrong with this. The program works, but the duplication doesn't work. The error I get is :

Notice: Undefined variable: SongToAdd in C:\ITEC315\htdocs\users\ramojumder0\Reinforcement Exercises\Ch. 6\R.E.6-1_SongOrganizer.php on line 64

Notice: Undefined variable: ExistingSongs in C:\ITEC315\htdocs\users\ramojumder0\Reinforcement Exercises\Ch. 6\R.E.6-1_SongOrganizer.php on line64

Warning: in_array() expects parameter 2 to be array, null given in C:\ITEC315\htdocs\users\ramojumder0\Reinforcement Exercises\Ch. 6\R.E.6-1_SongOrganizer.php on line 64

Warning: fopen(SongOrganizer/songs.txt): failed to open stream: No such file or directory in C:\ITEC315\htdocs\users\ramojumder0\Reinforcement Exercises\Ch. 6\R.E.6-1_SongOrganizer.php on line 72
There was an error saving your message!

I bolded line 64 below, which is an if (in_array ) statement.
line 64 is:

 if (in_array($SongToAdd, $ExistingSongs))
    {
        echo "<p>The song you entered already exists!<br />\n";
        echo "Your song was not added to the list.</p>";
    }
   
<?php

 if (isset($_GET['action']))
    {
        if ((file_exists("SongOrganizer/songs.txt")) 
        && (filesize("SongOrganizer/songs.txt") != 0))
        {
            $SongArray = file("SongOrganizer/songs.txt");
            
            switch ($_GET['action'])
            {
                case 'Remove Duplicates':
                    $SongArray = array_unique($SongArray);
                    $SongArray = array_values($SongArray);
                    break;
                case 'Sort Ascending':
                    sort($SongArray);
                    break;
                case 'Shuffle':
                    shuffle($SongArray);
                    break;
            } // end of switch statement
            
            if (count($SongArray)>0)
            {
                $NewSongs = implode($SongArray);
                $SongStore = fopen("SongOrganizer/songs.txt", "wb");
                if ($SongStore === false)
                    echo "There was an error updating the song file\n";
                else
                {
                    fwrite($SongStore, $NewSongs);
                    fclose($SongStore);
                }
            }
            else
                unlink("SongOrganizer/songs.txt");
        }
    }
    
    if (isset($_POST['submit']))
    {
        $SongToAdd = stripslashes($_POST['SongName']) . "\n";
        $ExistingSongs = array();
        if (file_exists("SongOrganizer/songs.txt") 
        	&& filesize("SongOrganizer/songs.txt") > 0)
        {
            $ExistingSongs = file("SongOrganizer/songs.txt");
        }
    }
    

    if (in_array($SongToAdd, $ExistingSongs))
    {
        echo "<p>The song you entered already exists!<br />\n";
        echo "Your song was not added to the list.</p>";
    }
   
    else
    {
        $SongFile = fopen("SongOrganizer/songs.txt", "ab");
        if ($SongFile === false)
            echo "There was an error saving your message!\n";
        else
        {
            fwrite($SongFile, $SongToAdd);
            fclose($SongFile);
            echo "Your song has been added to the list.\n";
        }
    }
    
   
    if ((!file_exists("SongOrganizer/songs.txt")) 
    	|| (filesize("SongOrganizer/songs.txt") == 0))
        echo "<p>There are no songs in the list.</p>\n";
    else
    {
        $SongArray = file("SongOrganizer/songs.txt");
        echo "<table border=\"1\" width=\"100%\" 
        	 style=\"background-color:lightgray\">\n";
        foreach ($SongArray as $Song)
        {
            echo "<tr>\n";
            echo "<td>" . htmlentities($Song) . "</td>";
            echo "</tr>\n";
        }
        echo "</table>";
    }
?> 


<!-- Display hyperlinks for the three functions in the switch statement 
	(Sort Ascending, Remove Duplicates, and Shuffle) -->
<p>
    <a href="SongOrganizer.php?action=Sort%20Ascending">Sort Song List</a><br />
    <a href="SongOrganizer.php?action=
    Remove%20Duplicates">Remove Duplicate Songs</a><br />
    <a href="SongOrganizer.php?action=Shuffle">
    Randomize Song List</a><br />
</p>

<!--  web form for entering new song names into the song list: -->

<form action="SongOrganizer.php" method="post">
    <p>Add a New Song</p>
    <p>Song Name: <input type="text" name="SongName" /></p>
    <p><input type="submit" name="submit" value="Add Song to List" />
    <input type="reset" name="reset" value="Reset Song Name" /></p>
</form>

Any help would be appreciated! 

Edited by jameshay
Link to comment
Share on other sites

in your posted code, lines 52 to 61 are your form processing logic, and are where the variables/arrays are defined at. however, the code following line 61 is also part of your form processing logic. you need to move the closing } that is on line 61 down so that it is after the end of your form processing logic.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.