Failing to write data

Hello! I’m trying to read a list cd names, remove those that have been selected by a user, and return the list to the user. The add function works fine, but remove fails to remove the data selected.

Here’s the snippet:

[php]

Select a Song:
<?php
query1();
?>




Selected songs:
<?php
if(isset($_POST[‘add’])) {
$songsList = fopen(“songsList.txt”, “w”) or die (“Unable to open file!”);
$selected_val = $_POST[‘songList’];
//$value = mysqli_fetch_row($selected_val);
foreach ($selected_val as $sVal) {
//echo $selectedOption."\n";
echo ‘’.$sVal.’’;
fwrite($songsList, $sVal."\r\n");
}
fclose();
}
if(isset($_POST[‘remove’])) {
$songsList = fopen(“songsList.txt”, “r+”) or die (“Unable to open file!”);
$selected_val = $_POST[‘songList’];
$songsList = array_diff($songsList, $selected_val);
foreach($songsList as $key => $song) {
fwrite($songsList, $song."\r\n");
echo ‘’.$song.’’;
}
fclose();
}
?>[/php]

Old post, did you resolve it yet? If not, I will guess it is the way the data is pulled from the form. You need to check the data.
Most likely it is the way you handle the songlist. You have two different ways of handling the FOREACH. One is correct and
the other is not. So, to really understand this, you need to look at what is inside the arrays.

In line #27: $songsList = array_diff($songsList, $selected_val);

You compare the two lists, $songsList and $selected_val. I suspect that they are NOT what you think they are. To check, you
can put this in your program and review the outputs to see what is inside the arrays:

               echo "<br>songs list:<br>";
               print_r($songList);
               echo "<br><br>selected songs:<br>";
               print_r($selected_val);
               die("<br><br>Done!");
               $songsList = array_diff($songsList, $selected_val);

Place this at #27 and run the page. Select some songs to remove and see what the output is. It will display the two
arrays that you are checking and most likely you will see your error.

Sponsor our Newsletter | Privacy Policy | Terms of Service