PHP & MySQL :: Simple problem?

Associate
Joined
21 Sep 2007
Posts
453
Ok folks, I am learning some php and mysql stuff in order to help me jazz up a website.

I have been following a tutorial on how to make a basic CMS so I can add, edit and delete articles.

The adding and deleting work fine, as does viewing the articles on my main page, however the edit part is causing problems, because the tutorial file itself is broken.

Below is the code for the edit page. When the page is loaded, the title and content of the article to be edited should appear in the boxes, but they don't. Instead <?=$title;?> and <?=$content;?> appear.

Typing something in a box and clicking edit gives this error:

"Notice: Undefined index: id in C:\Webserver\Apache2\htdocs\admin\editnews.php on line 51"

But also shows the message saying the article was edited OK (it doesnt update at all!)

PHP:
<html>
<head>
<title>Edit An Article</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
.box {
	font-family: Arial, Helvetica, sans-serif;
	font-size: 12px;
	border: 1px solid #000000;
}
-->
</style>
</head>

<body>
<?php
include 'config.php';
include 'opendb.php';

if(isset($_GET['id']))
{
	$query = "SELECT id, title, content ".
	         "FROM news ".
			 "WHERE id = '{$_GET['id']}'";
	$result = mysql_query($query) or die('Error : ' . mysql_error());
	list($id, $title, $content) = mysql_fetch_array($result, MYSQL_NUM);
	
	$content = htmlspecialchars($content);
} 
else if(isset($_POST['title']))
{
    $id      = $_POST['id'];
	$title   = $_POST['title'];
	$content = $_POST['content'];
	
	if(!get_magic_quotes_gpc())
	{
		$title   = addslashes($title);
		$content = addslashes($content);
	}
	
	// update the article in the database
	$query = "UPDATE news ".
	         "SET title = '$title', content = '$content' ".
			 "WHERE id = '$id'";
	mysql_query($query) or die('Error : ' . mysql_error());

	// then remove the cached file
	$cacheDir  = dirname(__FILE__) . '/cache/';
	$cacheFile = $cacheDir . '_' . $_GET['id'] . '.html';
	
	@unlink($cacheFile);
	
	// and remove the index.html too because the file list
	// is changed
	@unlink($cacheDir . 'index.html');
		
	echo "<p align='center'>Article updated</p>";
	
	// now we will display $title & content
	// so strip out any slashes
	$title   = stripslashes($title);
	$content = stripslashes($content);
}

include 'closedb.php';
?>
<form method="post" action="editnews.php">
<input type="hidden" name="id" value="<?=$id;?>">
  <table width="700" border="0" cellpadding="2" cellspacing="1" class="box" align="center">
    <tr> 
      <td width="100">Title</td>
      <td><input name="title" type="text" class="box" id="title" value="<?=$title;?>"></td>
    </tr>
    <tr> 
      <td width="100">Content</td>
      <td><textarea name="content" cols="50" rows="10" class="box" id="content"><?=$content;?></textarea></td>
    </tr>
    <tr> 
      <td width="100">&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr> 
      <td colspan="2" align="center"><input name="update" type="submit" class="box" id="update" value="Update 

Article"></td>
    </tr>
  </table>
  <p align="center"><a href="admin.php">Back to admin page</a></p>
</form>
</body>
</html>

And just to inform you, yes my sql table it setup correctly :)

Need to fix this one problem before I can continue so I hope you can help!
 
Below is the code for the edit page. When the page is loaded, the title and content of the article to be edited should appear in the boxes, but they don't. Instead <?=$title;?> and <?=$content;?> appear.

I'm guessing your webhost has short_tags disabled, so you'll need to use the full tags, e.g: <?php print $title; ?> (or enable short_open_tag in php.ini)


Typing something in a box and clicking edit gives this error:

"Notice: Undefined index: id in C:\Webserver\Apache2\htdocs\admin\editnews.php on line 51"
In line 51 you have
Code:
    $cacheFile = $cacheDir . '_' . $_GET['id'] . '.html';
whereas above that in the file you have $_POST['id'], are you setting both (url & hidden form field)? If not you'll need to use $_POST for both.
 
Last edited:
Back
Top Bottom