...

View Full Version : Admin Panel fopen & fwrite



fl00d
08-01-2007, 06:13 AM
Hi,

I'm having trouble getting the fopen & fwrite functions to work. I've included them in an admin panel script to make for easy, online editing. For some reason, the info isn't getting passed, and I don't know why. I think it may have to do with $_GET & $_POST conflicting with each other.

What happens is I click the link for the page I want to edit, it shows the form and I click the 'Update' button and the page returns blank. It doesn't even echo that it failed.
Anyone have an idea? I don't :confused:


<?php
session_start();
if(@$_SESSION['admin'] != "yes"){
echo "You are not admin.";
}else{
include("/home/flawclan/public_html/config.php");
$connect = mysql_connect("$host","$user","$passwd");

mysql_select_db("$dbname",$connect);

$table_name = "cms"; //table
//navbar query
$page = $_GET['page'];
$sql = "SELECT * FROM $table_name";
$query = mysql_query($sql,$connect);
while($row = @mysql_fetch_array($query)){
$id = $row['content_id'];
$title = $row['page_title'];
echo "<a href=\"admincp.php?page=".$id."\">Edit $title page</a><br />";
}

if($page == "1"){
$pagename = "main.php";
}
if($page == "2"){
$pagename = "news.php";
}
if($page == "3"){
$pagename = "roster.php";
}
if($page == "4"){
$pagename = "downloads.php";
}
if($page == "5"){
$pagename = "server.php";
}
if($page == "6"){
$pagename = "about.php";
}
//content page query
$sql = "SELECT content_id FROM $table_name WHERE content_id='$page'";
$query = mysql_query($sql,$connect);
while($row = @mysql_fetch_array($query)){
switch($_POST['button']){
case "Update":
$file = fopen("/home/flawclan/public_html/pages/".$pagename."", "w");
$content = $_POST['content'];
$write = fwrite($file,$content);
if($write){
echo "Updated sucessfully";
}else{
echo "Darn it... something went wrong";
}
fclose($file);
break;
default:
echo "<center><p>Editing $pagename";
echo "<form action=";
echo "admincp.php";
echo " method=\"post\">
<textarea name=\"content\"></textarea><br />
<input type=\"submit\" value=\"Update\" name=\"button\"></form>";
}
}
}
?>

Thanks for any possible help!

PappaJohn
08-01-2007, 09:23 PM
Well, I'm not seeing any error checking on either the variables passed in to the script or the queries.

1. Try echoing $page to see if it contains a value you expect
2. for all queries, add "or die(mysql_error());" like this:


$query = mysql_query($sql,$connect) or die(mysql_error());

fl00d
08-01-2007, 11:59 PM
yeah it is echoing the variable I expect it to be, I checked just as you suggested. I tried copying and pasting

<?php
switch($_POST['button']){
case "Update":
$file = fopen("/home/flawclan/public_html/pages/".$pagename."", "w");
$content = $_POST['content'];
$write = fwrite($file,$content);
if($write){
echo "Updated sucessfully";
}else{
echo "Darn it... something went wrong";
}
fclose($file);
break;
default:
echo "<center><p>Editing $pagename";
echo "<form action=";
echo "admincp.php";
echo " method=\"post\">
<textarea name=\"content\"></textarea><br />
<input type=\"submit\" value=\"Update\" name=\"button\"></form>";
}
?>
into another file and I ran it from there and it worked fine. The page was updated and it echoed that it was indeed successful. I have one idea maybe why its not working, because the db query isn't letting anything else be written to the page? I don't know at this point. I'm just throwing ideas around.

*EDIT*
Ok I fixed it! After hours of trouble shooting I realized the problem lay in this line here:

<form action="admincp.php" method="post">
The page wasn't right! I changed it to:

<form action="admincp.php?page=$page" method="post">

Thanks for the help! Simple mistakes always take the longest to find and fix.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum