Jump to content

Help with unset error


thebopps

Recommended Posts

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

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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

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.