[PHP] File editing not working X_X

He has: 296 posts

Joined: May 2002

My friend asked me to write a review script for his website. He doesn't have MySQL which made it harder than usuall, but so far it works. Today I started working on the editing the reviews part of it and got stumped. Here's the code:

<?php
function Edit($id) {
  global
$globals;
 
Output(\"header2\",\"Edit File\",FALSE,\"\");
  if (!isset(
$id)) {
    echo \"You must specify a review to edit! Please go back and select one!\";
  } else {
   
$blah = $globals[\"dir\"].\"/\".$id.\".\".$globals[\"ext\"];
   
$fp = fopen($blah, \"r\");
   
$file = fread($fp,filesize($blah));
    fclose(
$fp);
   
$contents = explode(\"///--///\",$file);
   
$review = str_replace(\"|br|\",\"\",$contents[\"5\"]);
   
$title = $contents[\"4\"];
echo<<<MyForm
  <form action=\"reviews.php\" method=\"post\">
  <input type=\"hidden\" name=\"doedit\" value=\"do\">
  <input type=\"hidden\" name=\"id\" value=\"
$id\">
  <b>Review:</b> <input type=\"text\" name=\"title\" value=\"
$title\"><br>
  <b>Contents:</b><br>
  <textarea cols=\"50\" rows=\"15\" name=\"review\">
$review</textarea><br>
  Password: <input type=\"password\" name=\"password\"><br>
  <input type=\"submit\">
  <input type=\"reset\"></form>
MyForm;
  }
  Output(\"footer\",\"\",FALSE,\"\");
}

function DoEdit() {
  global
$globals;
 
$id = $_POST[\"id\"];
 
$blah = $globals[\"dir\"].\"/\".$id.\".\".$globals[\"ext\"];
  echo
$blah.\"<br>\";
 
$fp = fopen($blah,\"r\");
 
$file = fread($fp,filesize($blah));
  fclose(
$fp);
 
$file = explode(\"///--///\",$file);
 
$password = md5($_POST[\"password\"]);
  if (
$password != $file[\"2\"]) {
    Output(\"header2\",\"Edit File\",FALSE,\"\");
    echo \"Error! Passwords do not match!\";
    Output(\"footer\",\"\",FALSE,\"\");
    exit;
  }
 
$review = str_replace(\"\n\",\"|br|\",$_POST[\"review\"]);
 
$review = $review.\"|br||br|<i>Last Edited:\".date (\"F dS, l, Y at h:i:s A\");
 
$new_file = $id.\"///--///\".$file[\"1\"].\"///--///\".$file[\"2\"].\"///--///\".$file[\"3\"].\"///--///\".$_POST[\"title\"].\"///--///\".$review.\"///--///0\";
 
$fp = fopen($blah,\"w+\");
  if (fwrite(
$fp,$new_file)) {
    Output(\"header2\",\"Edit File\",TRUE,
$globals[\"url\"]);
    echo \"Updated!\";
    Output(\"footer\",\"\",FALSE,\"\");
  } else {
    Output(\"header2\",\"Edit File\",FALSE,\"\");
    echo \"Error! File was not updated!\";
    Output(\"footer\",\"\",FALSE,\"\");
    exit;
  }
  fclose(
$fp);
}
?>

When DoEdit() runs it's just blank. If I add a foreach() to echo the form values it echos them fine.

Script: http://necrotic.anitrade.net/reviews.php
To add a review: http://necrotic.anitrade.net/reviews.php?submit=new

Any help would be much appreciated!

[James Logsdon]

He has: 296 posts

Joined: May 2002

O yes, the file format is:

id///--///ip///--///password///--///author///--///title///--///review///--///rating

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

I'm sorry, I'm half conscious right now.

Why are you using a flat file as opposed to a mysql db?

He has: 296 posts

Joined: May 2002

Quote: He doesn't have MySQL which made it harder than usuall, but so far it works.

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

I'm sorry. I'm not quite myself today. (had 4 wisdom teeth pulled 12 hours ago, and I'm still light headed and now very sleepy)

I'm guessing that DoEdit() is not running at all. Where are you calling it from? You should having something like:

if ($doedit=='do') {
DoEdit();
}

Mark Hensler
If there is no answer on Google, then there is no question.

He has: 296 posts

Joined: May 2002

And I do. All that happens is a blank page shows up.

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

can you paste those lines here plz

He has: 296 posts

Joined: May 2002

<?php
if ((!isset($id))&&(!isset($submit))&&(!isset($edit))&&(!isset($doedit))) Main();
elseif ((isset(
$id))&&(!isset($submit))&&(!isset($edit))&&(!isset($doedit))) Display($id);
elseif ((!isset(
$id))&&($submit==\"new\")&&(!isset($edit))&&(!isset($doedit))) Submit();
elseif ((!isset(
$id))&&($submit==\"do\")&&(!isset($edit))&&(!isset($doedit))) DoSubmit();
elseif ((!isset(
$id))&&(isset($edit))&&(!isset($submit))&&(!isset($doedit))) Edit($edit);
elseif ((!isset(
$id))&&($doedit==\"do\")&&(!isset($edit))&&(!isset($submit))) DoEdit();
?>

[James Logsdon]

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

logic error

$id is set, and you have !isset($id)

He has: 296 posts

Joined: May 2002

The thing is, though, is that if I tell the function DoEdit() to echo say, all the form values, it does it fine. When I get the chance (tomorrow) I'm going to have it echo $new_file and see if the values are set correctly.

[James Logsdon]

He has: 296 posts

Joined: May 2002

Well, turns out that when I change the if statements controlling the functions it works. Thanks for giving me the idea of rewriting it, Mark! Now I can do the last 2 things: a small rating system and a logging function and I'll be done!

[James Logsdon]

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.