Lodius2000 Posted June 11, 2008 Share Posted June 11, 2008 so i have a php variable $var it contains form data that I want to update a table field because I misspelled a word in the field the field in my db might be one of 2 things, varchar(255) or text, here is my stab at the SQL to get what I want done article_id is an INT UPDATE table_name SET field_name = '$var' WHERE article_id = '$id' is all my punctation and syntax correct, because this doesnt work, coincidentally, I have tried to change the contents of the same 2 fields directly from phpmyadmin but that doesnt seem to work either, is this a permission thing, if my db user profile has full priviliages THANKS Quote Link to comment Share on other sites More sharing options...
DarkWater Posted June 11, 2008 Share Posted June 11, 2008 Does it give any errors? Quote Link to comment Share on other sites More sharing options...
Lodius2000 Posted June 11, 2008 Author Share Posted June 11, 2008 nah the script executes upon post and things that are supposed to print as confirmation of success print, all is well but nothing is changed in the db do you guys need to see the script, its 110 lines Quote Link to comment Share on other sites More sharing options...
DarkWater Posted June 11, 2008 Share Posted June 11, 2008 Only the relevant query lines please. Quote Link to comment Share on other sites More sharing options...
Lodius2000 Posted June 11, 2008 Author Share Posted June 11, 2008 <?php function process_form(){ global $db, $id, $title, $article; $edited_title = mysql_real_escape_string($_POST['title']); $edited_article = mysql_real_escape_string($_POST['body']); //check to see if title has changed, if changed, update db if ($title != $edited_title){ $db->query("UPDATE text_cms SET article_title = '$edited_title' WHERE article_id = '$id'"); print "Updated <strong>$title</strong> in the database.<br />\n"; } //check to see if body has changed, if changed, update db if ($article != $edited_article){ $db->query("UPDATE text_cms SET article = '$edited_article' WHERE article_id = '$id'"); print "Updated <strong>article body</strong> in the database.<br />\n"; } } ?> this prints "Updated article body in the database." no matter what changes were made, even if nothing was altered in $article, is still prints that, and it has never printed "Updated $title in the database." even when I do make changes there. thanks Quote Link to comment Share on other sites More sharing options...
DarkWater Posted June 12, 2008 Share Posted June 12, 2008 Please, please add OR die(mysql_error()); to the end of your queries. Quote Link to comment Share on other sites More sharing options...
BrianM Posted June 12, 2008 Share Posted June 12, 2008 <?php function process_form(){ global $db, $id, $title, $article; $edited_title = mysql_real_escape_string($_POST['title']); $edited_article = mysql_real_escape_string($_POST['body']); //check to see if title has changed, if changed, update db if ($title != $edited_title){ $db->query("UPDATE text_cms SET article_title = '$edited_title' WHERE article_id = '$id'") or die(mysql_error()); print "Updated <strong>$title</strong> in the database.<br />\n"; } //check to see if body has changed, if changed, update db if ($article != $edited_article){ $db->query("UPDATE text_cms SET article = '$edited_article' WHERE article_id = '$id'") or die(mysql_error()); print "Updated <strong>article body</strong> in the database.<br />\n"; } } ?> Try that and see what errors you get. Quote Link to comment Share on other sites More sharing options...
Lodius2000 Posted June 12, 2008 Author Share Posted June 12, 2008 DARKWATER: done so a query no looks like $db->query("UPDATE text_cms SET article_title = '$edited_title' WHERE article_id = '$id'")OR die(mysql_error()); so i brought up a page i want to edit, typed 'test' in both the $_POST['title'] and $_POST['body'] fields, posted and it printed Updated 3 months already! test in the database. Updated article body in the database. but there was no change in the record interestingly, i went back to the same record and added test onto only $_POST['title'] and I got the same confirmation as above will try yours also BrianM Quote Link to comment Share on other sites More sharing options...
BrianM Posted June 12, 2008 Share Posted June 12, 2008 Would you mind posting the entire page, with any includes if you have them? And a database structure to where this is being sent. Quote Link to comment Share on other sites More sharing options...
Lodius2000 Posted June 12, 2008 Author Share Posted June 12, 2008 brianm yours did not work will post whole thing Quote Link to comment Share on other sites More sharing options...
Lodius2000 Posted June 12, 2008 Author Share Posted June 12, 2008 dblogin.php will not be posted, it works on every other page i have so it is correct, pear/db.php can be found at http://pear.php.net/package/DB/docs/1.7.14RC1/ a sample url for $_GET['id'] would be /index.php?id=12 i have also printed all the relevant variables to make sure they contain the modified values ie TEST TEST TEST and they all do, hust dont know whats going on <?php //this page edits articles session_start(); require ('../../../install/PEAR/DB.php'); require ('../../../../dbfiles/db_login.php'); $db->setErrorHandling(PEAR_ERROR_DIE); $db->setFetchMode(DB_FETCHMODE_ASSOC); //print a text box function input_text($element_name, $values){ print '<input type="text" name="' . $element_name .'" value="'; print htmlentities($values[$element_name]) . '"/>'; } //print a textarea function input_textarea($element_name, $values){ print '<textarea cols="75" rows="50" name="' . $element_name .'">'; print htmlentities($values[$element_name]) . '</textarea>'; } //grabs article_id for later query $id = $_GET['id']; if ($_SESSION['username']){ if($_POST['_submit_check']){ if($form_errors = validate_form()){ show_form($form_errors); } else { process_form(); } } else { show_form(); } } else { print '<a href="../index.php">Log In dummy</a>'; } function show_form($errors = '') { global $db, $id; $items = $db->getRow('SELECT article_title, article FROM text_cms WHERE article_id = ?', array($id)); $title = stripslashes($items['article_title']); $article = stripslashes ($items['article']); //If form has been submitted, get defaults from submitted variables if ($_POST['_submit_check']){ $defaults = $_POST; } else { $defaults = array('title' => $title, 'body' => $article); } if ($errors){ print 'Please correct these errors: <ul><li>'; print implode('</li><li>', $errors); print '</li></ul>'; } print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">'; //begin the unique form print '<br />'; print "Article Title"; print "<br />\n"; input_text('title', $defaults); print "<br /><br />\n"; print 'Article Body'; print "<br />\n"; input_textarea('body', $defaults); print "<br /><br />\n"; input_submit('submit', 'Edit Article'); print '<input type="hidden" name="_submit_check" value="1" />'; print "\n"; print '</form>'; } function validate_form(){ if (trim(strlen($_POST['title'])) > 255){ $errors[] = 'Article title is too long, please shorten it'; } return $errors; } function process_form(){ global $db, $id, $title, $article; $edited_title = mysql_real_escape_string($_POST['title']); $edited_article = mysql_real_escape_string($_POST['body']); //check to see if title has changed, if changed, update db //if ($title != $edited_title){ $db->query("UPDATE text_cms SET article_title = '$edited_title' WHERE article_id = '$id'")OR die(mysql_error()); print "Updated <strong>$title</strong> in the database.<br />\n"; //} //check to see if body has changed, if changed, update db //if ($article != $edited_article){ $db->query("UPDATE text_cms SET article = '$edited_article' WHERE article_id = '$id'")OR die(mysql_error()); print "Updated <strong>article body</strong> in the database.<br />\n"; //} print '<a href="../managearticle/index.php">Go back to article management</a>'; } ?> thanks Quote Link to comment Share on other sites More sharing options...
BrianM Posted June 12, 2008 Share Posted June 12, 2008 And while I'm looking this over, you said the problem is that it wont update the database with new values posted? Quote Link to comment Share on other sites More sharing options...
Lodius2000 Posted June 12, 2008 Author Share Posted June 12, 2008 correct haha Quote Link to comment Share on other sites More sharing options...
DarkWater Posted June 12, 2008 Share Posted June 12, 2008 And it doesn't even work in phpMyAdmin? Does it error you out or something? Quote Link to comment Share on other sites More sharing options...
Lodius2000 Posted June 12, 2008 Author Share Posted June 12, 2008 no not there either, i just goes back to the browse screen after i hit save, as it it works, but nothing happens Quote Link to comment Share on other sites More sharing options...
widox Posted June 12, 2008 Share Posted June 12, 2008 humm, are you certain the permissions are correct for the db user? There are both database AND table level permissions. Quote Link to comment Share on other sites More sharing options...
Lodius2000 Posted June 12, 2008 Author Share Posted June 12, 2008 it sounds like we might all be coming to the same conclusion, i am talking with people at work and they all seem to think it could be something with my host because the db user does have full permissions, that was one of the first things i checked I think i may take it up with my host if someone could give me a confirmation that my sql should be doing what I want it to do, like if the sytax is right, i could call this solved, I just dont know UPDATE statements too well, so i want to make sure of that first Quote Link to comment 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.