Why wont this update?
Posted: Sun Aug 09, 2009 8:39 am
I dont understand why this code is not updating the database. When I click submit all I get back is "Array()" at the top of the unchanged form.
Code: Select all
<?php require_once("includes/connect.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php
if (intval($_GET['article']) == 0) {
redirect_to("news.php");
}
if (isset($_POST['submit'])) {
$errors = array();
$required_fields = array('title', 'content');
foreach($required_fields as $fieldname) {
if (!isset($_POST[$fieldname]) || empty($_POST[$fieldname])) {
$errors[] = $fieldname;
}
}
print_r($errors);
if (empty($errors)) {
// Perform Update
$id = $_GET['article'];
$title = $_POST['title'];
$content = $_POST['content'];
$query = "UPDATE subjects SET
title = '{$title}',
content = '{$content}'
WHERE id = {$id}";
$result = mysql_query($query, $connect);
if (mysql_affected_rows() == 1) {
// Success
} else {
// Failed
}
} else {
// Errors occurred
}
}
?>
<?php
if(isset($_GET['article'])) {
$query = "SELECT * FROM news WHERE id={$_GET['article']} ";
$get_article = mysql_query($query, $connect);
if(!$get_article) {
die("Database Query Failed: ".mysql_error());
}
if ($articledata = mysql_fetch_array($get_article)) {
} else {
$articledata = NULL;
}
}
?>
<?php include("includes/header.php"); ?>
<h2>Edit News Article: <?php echo $articledata['title']; ?></h2>
<form action="editnews.php?article=<?php echo $articledata['id']; ?>" method="post">
<p>Title:<br /></p>
<input type="text" name="title" value="<?php echo $articledata['title']; ?>" id="title" />
<p> Content:<br /></p>
<textarea name="content" cols="40" rows="5" id="content"><?php echo $articledata['content']; ?></textarea>
<br /><input type="submit" name="submit" value="Edit News" />
</form>
<br />
<a href="news.php">Cancel</a>
<?php require("includes/footer.php"); ?>