thebopps Posted April 13, 2007 Share Posted April 13, 2007 I have to make a simple discussion board and i am having trouble with my php script that is supposed to read a message number entered by a user and delete that specific message. When I enter a number and press delete, i get an error: Fatal error: Cannot unset string offsets in /home/cmeyer/public_html/IFS260/PA4/DeleteMessage.php on line 10 here is my code: <?php if (file_exists("messages.txt") && filesize("messages.txt") != 0) { $MessageArray = file("messages.txt"); $MessageArray = stripslashes($MessageArray); $Message = $_POST["message"] - 1; //echo $_POST[message]=$_POST[message]; if (isset($MessageArray[$Message])) { unset($MessageArray[$Message]); $MessageArray = array_values($MessageArray); $NewMessages = implode($MessageArray); $MessageStore = fopen("messages.txt", "a"); fwrite($MessageStore, "$NewMessages"); fclose($MessageStore); } } header("location:ViewDiscussion.php"); ?> the echo statement was used to make sure it is passing the entered number to the script just wanted to see if you guys noticed anything wrong with it thanks Quote Link to comment Share on other sites More sharing options...
btherl Posted April 13, 2007 Share Posted April 13, 2007 You can't call stripslashes() on an array.. instead, you need to make a loop that calls stripslashes() on each element of the array. This is what leads to your later error with the unset() Quote Link to comment Share on other sites More sharing options...
thebopps Posted April 13, 2007 Author Share Posted April 13, 2007 damn that's right out of a text book to hahaha, what would the loop look like, and where should it be? Quote Link to comment Share on other sites More sharing options...
clown[NOR] Posted April 13, 2007 Share Posted April 13, 2007 something like this maybe? *EDIT: Removed the stripslashes($MessageArray); forgot to take it away when I posted the message* <?php if (file_exists("messages.txt") && filesize("messages.txt") != 0) { $MessageArray = file("messages.txt"); $Message = $_POST["message"] - 1; //echo $_POST[message]=$_POST[message]; if (isset($MessageArray[$Message])) { unset($MessageArray[$Message]); $MessageArray = array_values($MessageArray); $NewMessages = implode($MessageArray); $NewMessages = stripslashes($NewMessages); $MessageStore = fopen("messages.txt", "a"); fwrite($MessageStore, "$NewMessages"); fclose($MessageStore); } } header("location:ViewDiscussion.php"); ?> - Clown Quote Link to comment Share on other sites More sharing options...
btherl Posted April 13, 2007 Share Posted April 13, 2007 Hmm.. clown's approach looks good. He is waiting until the array is converted to a string before calling stripslashes. If you wanted to do it with a loop, you could do: foreach ($MessageArray as $k => $v) { $MessageArray[$k] = stripslashes($MessageArray[$k]); } $k stands for key, and $v for value. Edit: edited to match clown's edit. No wonder they put the time limit in Quote Link to comment Share on other sites More sharing options...
clown[NOR] Posted April 13, 2007 Share Posted April 13, 2007 if you look at it, I edited the post and removed the first stripslashes Quote Link to comment Share on other sites More sharing options...
thebopps Posted April 13, 2007 Author Share Posted April 13, 2007 thanks a lot ill give it a whirl Quote Link to comment Share on other sites More sharing options...
thebopps Posted April 13, 2007 Author Share Posted April 13, 2007 no errors but instead of deleting the message it adds copies of existing ones on to the discussion board Quote Link to comment Share on other sites More sharing options...
clown[NOR] Posted April 13, 2007 Share Posted April 13, 2007 what happens if you change from this fwrite($MessageStore, "$NewMessages"); to this fwrite($MessageStore, $NewMessages); Quote Link to comment Share on other sites More sharing options...
thebopps Posted April 13, 2007 Author Share Posted April 13, 2007 same thing Quote Link to comment Share on other sites More sharing options...
clown[NOR] Posted April 13, 2007 Share Posted April 13, 2007 hmm... well ... if i understand this right you want to delete a entry in the dicussion... I dont know how exactly to fix that issue, the best thing I can do is to show you a code I made that I used on my old site to delete news entries, shouts etc... hope you get some usefull info from it... <?php $selLine = $_GET['id']; $delType = $_GET['type']; $file = "store/" . $delType . ".txt"; $news = file($file); $cnt = 0; foreach ($news as $key => $line) { if ($key != $selLine) { $result[] = $line; } $cnt+1; } if ($key != 0) { $fh = fopen($file, "w"); foreach ($result as $nyhet) { fwrite($fh, $nyhet); } fclose($fh); } elseif ($key == 0) { $fh = fopen($file, "w"); fwrite($fh, ""); fclose($fh); } ?> go trough it and see it yu can use anything from it... i'm going to sleep now... 6.00am here now =) - Clown Quote Link to comment Share on other sites More sharing options...
thebopps Posted April 13, 2007 Author Share Posted April 13, 2007 thanks for the hlp but I still can' figure this out, it is a tutorial in a college text book and the script does not function properly Quote Link to comment Share on other sites More sharing options...
btherl Posted April 13, 2007 Share Posted April 13, 2007 Hmm.. I am guessing that it adds copies because the file is opened with the "a" flag. That means append. Instead it should be opened with the "w" flag, meaning write (also overwrite). Quote Link to comment Share on other sites More sharing options...
thebopps Posted April 13, 2007 Author Share Posted April 13, 2007 oh that worked thanks a lot guys Quote Link to comment Share on other sites More sharing options...
clown[NOR] Posted April 13, 2007 Share Posted April 13, 2007 just like i used in the code I gave you $fh = fopen($file, "w"); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.