Submit Your Article Forum Rules

Page 2 of 2 FirstFirst 12
Results 11 to 16 of 16

Thread: Getting error when submitting 'visible' field to database with php

  1. #11
    Senior Member ristenk1's Avatar
    Join Date
    Apr 2009
    Posts
    122
    //clean up the form data before putting it in the database
    $id = mysql_prep($_GET['page']);
    $menu_name = trim(mysql_prep($_POST['menu_name']));
    $position = mysql_prep($_POST['position']);
    $visible = mysql_prep($_POST['visible']);
    $content = mysql_prep($_POST['content']);


    echo '<pre>POST: ' . print_r($_POST, true) . '</pre>';
    echo '<pre>REQUEST: ' . print_r($_REQUEST, true) . '</pre>';
    echo '<pre>errors: ' . print_r($errors, true) . '</pre>';


    //Database submission only proceeds if there were NO errors.
    if(empty($errors)) {
    $query = "UPDATE pages SET
    menu_name = '{$menu_name}',
    position = {$position},
    visible = {$visible},
    content = '{$content}'
    WHERE id = {$id}";
    $result = mysql_query($query, $connection);
    echo "--- Start Debug ---<br>\n" . $query . "<br>\n";
    if(mysql_error()){ echo mysql_error() . "<br>--- End Debug ---<br>\n"; }


    I noticed that the visible- no doesn't output the "start debug" command, I guess it doesn't get that far.

  2. #12
    Senior Member ristenk1's Avatar
    Join Date
    Apr 2009
    Posts
    122
    Well, I got it working by using the same code on another site from the tutorial that I could get to work... I don't really see what is different about the code except that it was arranged in a different order but I'll post it below and maybe someone else with a more trained eye can see what is different about this code compared to what I originally posted:

    New Code that is now working:

    if (intval($_GET['page']) == 0) {
    redirect_to("content.php");
    }
    if (isset($_POST['submit'])){
    $errors = array();
    $required_fields = array('menu_name', 'position', 'visible', 'content');
    foreach($required_fields as $fieldname) {
    if (!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) && !is_numeric($_POST[$fieldname]))) {
    $errors[] = $fieldname;
    }
    }
    $fields_with_lengths = array('menu_name' => 30);
    foreach($fields_with_lengths as $fieldname => $maxlength){
    if(strlen(trim(mysql_prep($_POST[$fieldname]))) > $maxlength) {
    $errors[] = $fieldname; }
    }
    if(empty($errors)) {
    $id = mysql_prep($_GET['page']);
    $menu_name = mysql_prep($_POST['menu_name']);
    $position = mysql_prep($_POST['position']);
    $visible = mysql_prep($_POST['visible']);
    $content = mysql_prep($_POST['content']);
    echo '<pre>POST: ' . print_r($POST, true) . '</pre>';
    echo '<pre>REQUEST: ' . print_r($_REQUEST, true) . '</pre>';
    echo '<pre>errors: ' . print_r($errors, true) . '</pre>';

    $query = "UPDATE pages SET
    menu_name = '{$menu_name}',
    position = {$position},
    visible = {$visible},
    content = '{$content}'
    WHERE id = {$id}";
    $result = mysql_query($query, $connection);
    if(mysql_affected_rows() == 1) {
    //Success
    $message = "The subject was successfully updated.";
    } else {
    //Failed
    $message = "The subject update failed.";
    $message .= "<br />" . mysql_error();
    }

    } else {
    //Errors occurred
    $message = "There were " . count($errors) . " errors in the form.";
    }
    }//end: if(isset($_POST['submit']))

    Old code (that was posted previously and does not work when submitting visible - no):

    //clean up the form data before putting it in the database
    $id = mysql_prep($_GET['page']);
    $menu_name = trim(mysql_prep($_POST['menu_name']));
    $position = mysql_prep($_POST['position']);
    $visible = mysql_prep($_POST['visible']);
    $content = mysql_prep($_POST['content']);

    //Database submission only proceeds if there were NO errors.
    if(empty($errors)) {
    $query = "UPDATE pages SET
    menu_name = '{$menu_name}',
    position = {$position},
    visible = {$visible},
    content = '{$content}'
    WHERE id = {$id}"
    ;
    $result = mysql_query($query, $connection);

    //test to see if the update occurred
    if(mysql_affected_rows() == 1) {
    //Success!
    $message = "The page was successfully updated.";
    } else {
    //Failed
    $message = "The page update failed.";
    $message .= "<br />" . mysql_error();

    }


    } else {
    if(count($errors) == 1){
    $message = "There was 1 error in the form.";

    } else {
    //Errors occurred
    $message = "There were " . count($errors) . " errors in the form.";
    }
    }
    //END FORM PROCESSING

    }//end: if(isset($_POST['submit']))



    By the way, how do I format my code to look neater in this site... I've noticed that other's have been wrapping there's in a code tag but am unsure of where to find this on the editing toolbar for this post.
    Last edited by ristenk1; 07-21-2012 at 05:19 AM.

  3. #13
    Junior Member
    Join Date
    Jul 2012
    Posts
    4
    What caused the initial issue was the use of PHP's empty() function... it returns true for variables that are null, unset, empty, the value 0 in any data type, etc. (for more info visit "php dot net" and search for a function named empty (sorry, I can't post links yet). The typical workaround is to also test the length of the value using the strlen function.

    I did see a typo in your reference to the POST array--it should be $_POST (the underscore is important).

    To prettify your code, wrap the code in a set of "PHP" tags using square brackets, similarly to HTML tags. In the "Advanced Editor", you can select (highlight) the code in your post then click the "php" button on the end of the 2nd row of formatting tools.
    Last edited by LordRegent; 07-21-2012 at 09:35 AM. Reason: correct a typo

  4. #14
    Senior Member ristenk1's Avatar
    Join Date
    Apr 2009
    Posts
    122
    aha! it's all become a little clearer, which was the goal at the end of the day THanks guys!!

  5. #15
    WebProWorld MVP williamc's Avatar
    Join Date
    Jul 2003
    Location
    On a really big hill in Kentucky
    Posts
    4,721
    to display code on the forum properly add a [ code ] and [ /code ] without the spaces in them, it is part of most forums BBcode markup
    William Cross
    Web Development by Those Damn Coders
    Firearm Friendly Websites because our constitution matters

  6. #16
    Junior Member
    Join Date
    Jul 2012
    Posts
    4
    As I was thinking through this yesterday, I wanted to see the code that populated the $errors array, because I felt certain that was the core of the issue.

    Here's the final working code that you posted:
    PHP Code:
    $errors = array();
    $required_fields = array('menu_name''position''visible''content');
    foreach(
    $required_fields as $fieldname) {
      if (!isset(
    $_POST[$fieldname]) || (empty($_POST[$fieldname]) && !is_numeric($_POST[$fieldname]))) {
        
    $errors[] = $fieldname;
      }

    You should compare the fourth line of this code against what was in use when you first posted this issue. I expect that initially it was just doing "!isset($_POST[$fieldname]) || empty($_POST[$fieldname]) " which gave a false positive for values that evaluated to zero.

    Also, while the [ code ] and [ /code ] tags will make any block of text stand out, the [ php ] and [ /php ] tags (without the spaces) will apply syntax highlighting to php code (which, incidentally, can be utilized as a simple, free syntax checker: just "Go Advanced", paste your code and wrap it in "php" blocks, and then "Preview Post.")

  7. The following user agrees with LordRegent:
Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •