jamcoupe Posted August 9, 2009 Share Posted August 9, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/169415-why-wont-this-update/ Share on other sites More sharing options...
smerny Posted August 9, 2009 Share Posted August 9, 2009 $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 Quote Link to comment https://forums.phpfreaks.com/topic/169415-why-wont-this-update/#findComment-893840 Share on other sites More sharing options...
jamcoupe Posted August 9, 2009 Author Share Posted August 9, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/169415-why-wont-this-update/#findComment-893842 Share on other sites More sharing options...
smerny Posted August 9, 2009 Share Posted August 9, 2009 put print_r($errors); before if(empty($errors)), and see what comes up Quote Link to comment https://forums.phpfreaks.com/topic/169415-why-wont-this-update/#findComment-893843 Share on other sites More sharing options...
jamcoupe Posted August 9, 2009 Author Share Posted August 9, 2009 nothing comes up still... Quote Link to comment https://forums.phpfreaks.com/topic/169415-why-wont-this-update/#findComment-893844 Share on other sites More sharing options...
jamcoupe Posted August 9, 2009 Author Share Posted August 9, 2009 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()" Quote Link to comment https://forums.phpfreaks.com/topic/169415-why-wont-this-update/#findComment-893858 Share on other sites More sharing options...
PFMaBiSmAd Posted August 9, 2009 Share Posted August 9, 2009 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); Quote Link to comment https://forums.phpfreaks.com/topic/169415-why-wont-this-update/#findComment-893860 Share on other sites More sharing options...
jamcoupe Posted August 9, 2009 Author Share Posted August 9, 2009 After adding it here: <?php require_once("includes/connect.php"); ?> <?php ini_set("display_errors", "1"); error_reporting(E_ALL); ?> <?php require_once("includes/functions.php"); ?> Nothing happens.. Quote Link to comment https://forums.phpfreaks.com/topic/169415-why-wont-this-update/#findComment-893865 Share on other sites More sharing options...
jamcoupe Posted August 9, 2009 Author Share Posted August 9, 2009 I am still stuck on this piece of code anyone got any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/169415-why-wont-this-update/#findComment-894092 Share on other sites More sharing options...
wildteen88 Posted August 9, 2009 Share Posted August 9, 2009 This line <?php ini_set("display_errors", "1"); error_reporting(E_ALL); ?> Should be the first line. Quote Link to comment https://forums.phpfreaks.com/topic/169415-why-wont-this-update/#findComment-894093 Share on other sites More sharing options...
jamcoupe Posted August 9, 2009 Author Share Posted August 9, 2009 I changed that line of code to the top and it doesn't change anything. Quote Link to comment https://forums.phpfreaks.com/topic/169415-why-wont-this-update/#findComment-894095 Share on other sites More sharing options...
wildteen88 Posted August 9, 2009 Share Posted August 9, 2009 Check your sites error log instead. Your host should provide a utility for viewing your sites error log. Quote Link to comment https://forums.phpfreaks.com/topic/169415-why-wont-this-update/#findComment-894097 Share on other sites More sharing options...
jamcoupe Posted August 9, 2009 Author Share Posted August 9, 2009 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"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/169415-why-wont-this-update/#findComment-894098 Share on other sites More sharing options...
PFMaBiSmAd Posted August 9, 2009 Share Posted August 9, 2009 You were also previously asked, but never replied, if the data in the form was correct - Does a "view source" of the form page show what you expect for - action="editnews.php?article=<?php echo $articledata['id']; ?>" Quote Link to comment https://forums.phpfreaks.com/topic/169415-why-wont-this-update/#findComment-894101 Share on other sites More sharing options...
wildteen88 Posted August 9, 2009 Share Posted August 9, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/169415-why-wont-this-update/#findComment-894102 Share on other sites More sharing options...
jamcoupe Posted August 9, 2009 Author Share Posted August 9, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/169415-why-wont-this-update/#findComment-894108 Share on other sites More sharing options...
wildteen88 Posted August 9, 2009 Share Posted August 9, 2009 If you dont output anything then you're not going to know whether the script is working or not. That's why I put the echo's in to see what is happening. Its the basic steps of debugging. Quote Link to comment https://forums.phpfreaks.com/topic/169415-why-wont-this-update/#findComment-894116 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.