Heres a bit of a script I wrote recently to change articles. Change this for how you need it.
PHP Code:
if (isset($_POST['content'])) {
// data has been sent
// get data and make it safe for query
$id = mysql_real_escape_string($_POST['id']);
$article_name = mysql_real_escape_string($_POST['article_name']);
$content = mysql_real_escape_string($_POST['content']);
$image = mysql_real_escape_string($_POST['image']);
$url = mysql_real_escape_string($_POST['url']);
$category = mysql_real_escape_string($_POST['category']);
$popular = mysql_real_escape_string($_POST['popular']);
$meta_keywords = mysql_real_escape_string($_POST['meta_keywords']);
// query to edit opening
$edit = "UPDATE articles SET
article_name = '$article_name',
content = '$content',
image = '$image',
url = '$url',
category = '$category',
popular = '$popular',
meta_keywords = '$meta_keywords'
WHERE id = $id";
$result = mysql_query($edit) or die (mysql_error());
// did it work?
if ($result) {
echo 'Article editted succesfully<br /><br />';
echo 'Go to <a href="/articles/'.$url.'">article</a>?';
} else {
echo 'Error! The values sent are below.<br /><br />';
echo $content;
}
} else if (isset($_GET['id'])) {
$edit = "SELECT * FROM articles WHERE id = {$_GET['id']}";
$result = mysql_query($edit) or die (mysql_error());
$row = mysql_fetch_array($result);
?>
<form method="post" action="edit-article.php">
<div>
<b>Title:</b><br /><input type="text" name="article_name" value="<?php echo $row['article_name'] ?>" /><br /><br />
<b>Content:</b><br /><textarea name="content" cols="60" rows="20"><?php echo $row['content'] ?></textarea><br /><br />
<b>Image:</b><br /><input type="text" name="image" value="<?php echo $row['image'] ?>" /><br /><br />
<?php
if (!empty($row['image'])) {
echo '<img src="/content-images/articles/'. urlencode($row['image']) .'" alt="" class="imgcenter" />
<br /><br />';
}
?>
<b>URL:</b><br /><input type="text" name="url" value="<?php echo $row['url'] ?>" /><br /><br />
<b>Category:</b><br /><input type="text" name="category" value="<?php echo $row['category'] ?>" /><br /><br />
<b>Popular (<?php echo $row['views']; ?> views):</b><br />
<select name="popular" style="width:100px;">
<option value="1"<?php echo ($row['popular'] == 1 ? ' selected="selected"' : ''); ?>>Yes</option>
<option value="0"<?php echo ($row['popular'] == 0 ? ' selected="selected"' : ''); ?>>No</option>
</select><br /><br />
<b>Meta Keywords:</b><br /><input type="text" name="meta_keywords" value="<?php echo $row['meta_keywords'] ?>" /><br /><br />
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" />
<input type="submit" value="Edit" />
</div>
</form>
<?php
} else {
// show all articles
$sql = "SELECT id, article_name, views, popular FROM articles ORDER BY views DESC";
$result = mysql_query($sql) or die (mysql_error());
// display each child
while ($row = mysql_fetch_array($result)) {
echo '
<a href="edit-article.php?id='.$row['id'].'">'.$row['article_name'].'</a> ('.$row['views'].' views';
echo ($row['popular'] == 1) ? ', popular' : '';
echo ')<br />';
}
}
Bookmarks