Jump to content
Larry Ullman's Book Forums

Paul Martin

Members
  • Posts

    5
  • Joined

  • Last visited

Everything posted by Paul Martin

  1. Hello Antonio, Sorry it has taken a while to get back to you on this issue. The good news is that it seems to be resolved, although I don't really understand why? As I am using HTML Quickform2, I was unable to embed your PHP snippet into the QF2 hidden field. Probably need to investigate a little more on how to do that. However, what I did was this: //Add hidden field for id: $id = $form->addElement('hidden', 'id', 1); followed by your suggestion: // Check for a form submission: if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Handle the form submission // Make sure an ID is found $id = isset($_POST['id']) ? (int) $_POST['id'] : 0; // Make sure ID is valid if ( $id === 0 ) { // Serve error message, redirect or do something along those lines exit("ID not found/not valid integer (0)."); } // Validate the form data: if ($form->validate()) { // Update Database with new values $qry = 'UPDATE pages SET title=:title , content=:content WHERE id=:id'; $stmt = $pdo->prepare($qry); $run=$stmt->execute(array(':id' => $id, ':title'=> $title->getValue(), ':content' => $content->getValue())); // Check if transaction was successful if ($run) { Now, the bit I don't understand is why in the QF2 hidden field I have hard coded a page id and yet the page still updates for any page id. I would have thought that page id 1 would be the only page that gets updated? Anyway, I am happy that the basic functionality for editing page content is now working. I don't know whether it is the proper way of performing edits, but maybe in my journey of learning PHP, PDO, HTML and everything else that goes with creating dynamic web sites, it is a start and hopefully I will learn more robust methods to apply this functionality. I thank you and Larry for taking the time to help people like myself. Keep up the good work. Regards, Paul.
  2. Hi Antonio, thanks for your help again. I tried your most recent suggestion, but unfortunately it did fail. However, I did then notice there were some mistakes in my code where it was trying to prepare $q instead of $qry, Doh! So, I made the changes but it still didn't work. I am now wondering whether the problem is that I am trying to retrieve the page id from the session into the $id variable incorrectly. My code now reads as: // Check for a form submission: if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Handle the form submission // Validate the form data: if ($form->validate()) { // Store in a session: $id = $_SESSION['id']; // Update Database with new values $qry = 'UPDATE pages SET title=:title , content=:content WHERE id=:id'; $stmt = $pdo->prepare($qry); $run=$stmt->execute(array(':id' => $id, ':title'=> $title->getValue(), ':content' => $content->getValue())); // Check if transaction was successful if ($run) { When the above is run, I get no error and the page moves on to the 'page updated successfully' screen, but the database is not updated. If I hard code the page id into the update qry, it works fine. Michiel put in his thread that he managed to get it to work by retrieving the page id into a hidden form element. Is that a better way to perform this type of update and if so, can you educate me on how to do that please? Many thanks Paul.
  3. Hi Antonio, Thanks for trying, but alas, that is not what I am after, although I did give your suggestion a go in an update query. I guess it would have probably helped you if I had listed the code being used in the page to start with, so here it is (Bit I am having the problem with is in red): // Need the utilities file: require('include/utilities.inc.php'); // Redirect if the user doesn't have permission: if (!$user->canCreatePage()) { header("Location:index.php"); exit; } // Create a new form: set_include_path(get_include_path() . PATH_SEPARATOR . '/usr/local/pear/share/pear/'); require('HTML/QuickForm2.php'); $form = new HTML_QuickForm2('editPageForm'); // add form element value for page id $title = $form->addElement('text', 'id'); $title->setLabel('Page ID'); $title->addFilter('strip_tags'); // Add the title field: $title = $form->addElement('text', 'title'); $title->setLabel('Page Title'); $title->addFilter('strip_tags'); $title->addRule('required', 'Please enter a page title.'); // Add the content field: $content = $form->addElement('textarea', 'content'); $content->setLabel('Page Content'); $content->addFilter('trim'); $content->addRule('required', 'Please enter the page content.'); // Add the submit button: $submit = $form->addElement('submit', 'submit', array('value'=>'Publish')); // Select the database entries //prepare statement: $qry = 'SELECT id, title, content FROM pages WHERE id=:id'; $stmt = $pdo->prepare($q); $run = $stmt->execute(array( ':id' => $_GET['id'])); if ($run) { // fetch the page content from database as object: $stmt->setFetchMode(PDO::FETCH_CLASS, 'Page'); $id=$_SESSION['id']; while ($page = $stmt->fetch()) { // populate the form with the data: $form->addDataSource(new HTML_QuickForm2_DataSource_Array(array( 'id' => $page->getId(), 'title' => $page->getTitle(), 'content'=> $page->getContent()))); } } // End of select // Check for a form submission: if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Handle the form submission // Validate the form data: if ($form->validate()) { // Store in a session: $_SESSION['id'] = $id; // Update Database with new values $qry = 'UPDATE pages SET title=:title , content=:content WHERE id="$id"'; $stmt = $pdo->prepare($q); $run=$stmt->execute(array(':title'=> $title->getValue(), ':content' => $content->getValue())); // Check if transaction was successful if ($run) { $form->toggleFrozen(true); $form->removeChild($submit); } } // End of form validation IF. } // End of form submission IF. // } // Show the page: $pageTitle = 'Edit Page'; include('include/header.inc.php'); include('modules/edit_page.html'); include('include/footer.inc.php'); ?> Many thanks for your help Paul.
  4. Hi Larry, With the help of PHP and MySQL for Dynamic Web Pages 4th edition, I managed to put my first database driven web site live last October. Your book was/is truly inspirational for me as it enabled the inclusion of functionality that I would never have thought of if I hadn't read it. Now, I have moved on to your PHP Advanced and OOP 3rd edition so that I can get some idea on the way each syntax works and enable me to make a judgement on which to use and when. However, I have to say that I am struggling with the same chapter 9 exercise that Michiel was having a problem with. I have got to the same point where the title, content etc. have been fetched into the form where I can edit the content, but when I click submit, the page confirms it has been updated, even though it hasn't. I can see that the page id is being pulled into the page by including it as a quickform element, but unfortunately the where id=':id'); section of the update query is failing to recognise it. If I hard code the page id, the database updates as it should. I don't normally ask for help, preferring to try and figure it out myself, but after a week of trying it has become a bit frustrating. I am sure the answer is probably very simple, but would appreciate the answer so that I can analyze where I was making my mistake and move on to the next challenge. Many thanks Paul.
×
×
  • Create New...