Jump to content

Why wont this update?


jamcoupe

Recommended Posts

Hey I dont get any errors with this code it just doesnt update the database when I click submit?

 

<?php

if(isset($_POST['sumbit'])) {
$errors = array();
$required_fields = array('title', 'content');
	foreach($required_fields as $fieldname) {
		if(!isset($_POST[$fieldname]) || empty($_POST[$fieldname])) {
			$errors[] = $fieldname;
		}
	}
	if(empty($errors)) {
		//update
		$id = $_GET['article'];
		$title = $_POST['title'];
		$content = $_POST['content'];

		$query = "UPDATE news SET
					title = '{$title}',
					content = '{$content},'
				  WHERE id= {$id}";
		$result = mysql_query($query, $connect);
		if(mysql_affected_rows() == 1) {
		 	//yeye
		} else {
			//failed
		}

	} else {
		//failed
	}	
}
?>
<?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>

Link to comment
Share on other sites

$result = mysql_query($query, $connect) or die ("MySQL could not update:".$action);

 

 

you should add something like that, also are you sure "if(empty($errors))" is coming true? you can put an echo within that to make sure that you are even getting to that code

Link to comment
Share on other sites

I added this:

	if(empty($errors)) {
		echo $errors;
		//update
		$id = $_GET['article'];
		$title = $_POST['title'];
		$content = $_POST['content'];

		$query = "UPDATE news SET
					title = '{$title}',
					content = '{$content},'
				  WHERE id= {$id}";
		$result = mysql_query($query, $connect) or die("Failed: ".mysql_error());
		if(mysql_affected_rows() == 1) {

 

and it hasnt solved anything and when I hit the submit button the old data still appears and nothing is updated on the database :S

Link to comment
Share on other sites

After changing the code to:

<?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"); ?>

 

It still wont update but it outputs "Array()"

Link to comment
Share on other sites

Does a "view source" of the form page show what you expect for -

 

action="editnews.php?article=<?php echo $articledata['id']; ?>"

 

And what does putting the following two lines of code immediately after your first opening <?php tag give -

 

ini_set("display_errors", "1");
error_reporting(E_ALL);

Link to comment
Share on other sites

Am just running it on my own computer the last few errors in my php_error.log file are:

[09-Aug-2009 14:48:57] PHP Notice:  Undefined variable: articledata in /Applications/MAMP/htdocs/jstar/news.php on line 21
this error also displays on my news.php page but every thing works fine.

 

my news.php code is:

<?php require_once("includes/functions.php"); ?>
<?php ini_set("display_errors", "1"); error_reporting(E_ALL); ?>
<?php require("includes/connect.php"); ?>
<?php
//gets information to display the article on its own page.
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 {
	}
}


?>
<?php include("includes/header.php"); ?>
<?php 
if (!is_null($articledata)) {
?>
        <div class="news">
        <h3 class="news_title"><?php echo $articledata["title"] ?></h3>
        <p class="news_content"><?php echo $articledata["content"]; ?></p>
        <p class="news_bottom">[<?php echo $articledata["date"];?>]<br />by jamcoupe</p>
        </div>
        <a href="news.php">Back</a>
<?php
} else {
	$get_news = get_news();
	?>
	<h2>News</h2>
        <p><a href="addnews.php">Add News</a></p>
	<?php
	while ($articledata = mysql_fetch_array($get_news)) {
?>
	<div class="news">
	<h3 class="news_title"><a href="news.php?article=<?php echo $articledata['id']; ?>"> <?php echo $articledata['title'] ?></a></h3>
	<p class="news_content"><?php echo $articledata['content']; ?></p>
	<p class="news_bottom">[<?php echo $articledata['date'];?>]<br />by jamcoupe<br /><a href="editnews.php?article=<?php echo $articledata['id']; ?>">[Edit]</a></p>
	</div><br />	

<?php
}
?>
<?php
	}
?>

<?php require("includes/footer.php"); ?>

Link to comment
Share on other sites

This

$query = "UPDATE news SET
                  title = '{$title}',
                  content = '{$content},'
                 WHERE id= {$id}";
         $result = mysql_query($query, $connect);
         if(mysql_affected_rows() == 1) {
             //yeye
         } else {
            //failed
         }

Should be

$query = "UPDATE news SET
                  title = '{$title}',
                  content = '{$content},'
                 WHERE id= {$id}";
         $result = mysql_query($query, $connect);
         if(mysql_affected_rows() == 1) {
             echo 'Successfully updated';
         } else {
            echo 'Failed to update! Why?<br />' . mysql_info();
            echo 'Is there an error?<br/>' . mysql_error();
            echo 'Whats the query?<br/>' . $query;
         }

 

Am just running it on my own computer the last few errors in my php_error.log file are:

If you're running this locally then edit your php.ini and setup error_reporting to E_ALL and set display_errors to On. Also Notices wont stop your script from running. Only errors do.

Link to comment
Share on other sites

Thanks wildteen88 that has solved my problem. I dont understand why because my code seems the same just without the echos.  :-\

 

I checked my php.ini file and it had error_reporting set as E_ALL and display_errors to On.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.