Submit Your Article Forum Rules

Page 1 of 2 12 LastLast
Results 1 to 10 of 16

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

  1. #1
    Senior Member ristenk1's Avatar
    Join Date
    Apr 2009
    Posts
    122

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

    I am new to php... I am actually trying to walk myself through a Lynda tutorial and have come up on a hang up I can't figure out The code below works on everything EXCEPT when I try to make submit Visible - No. It doesn't update and says there is an error in my visibility field. If I go into the database and change all of the pages to no (changing the visibility field to zero for all of the fields), I can then use the form and successfully change the Visibility to Yes when submitting the form. I don't know why the visibility is working when I submit - yes (one) and not working when I submit - no (zero)... please can someone help me?

    Code of edit_page.php:


    //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']))
    ?>
    <?php find_selected_page (); ?>
    <div id="sidebar">

    <?php echo navigation($sel_subject, $sel_page) ?>

    </div>
    <div id="main">
    <h2> Edit Page: <?php echo $sel_page['menu_name']; ?></
    h2>
    <?php if (!empty($message)) {echo "<p class='message'>" . $message .
    "</p>";} ?>
    <?php if(!empty($errors)) {display_errors($errors); } ?>


    <form action="edit_page.php?page=<?php echo urlencode($sel_page['id']); ?>"
    method="post">

    <p>Page name:
    <input type="text" name="menu_name" value="<?php echo $sel_page['menu_name']; ?>" id="menu_name" />
    </p>
    <p>Position:
    <select name="position">
    <?php
    $page_set = mysql_query("SELECT * FROM pages ORDER BY position ASC", $connection);
    $page_count = mysql_num_rows($page_set);

    for($count=1; $count <= $page_count+1; $count++){

    echo "<option value='{$count}'";
    if($sel_page['position'] == $count){
    echo " selected";
    }

    echo">{$count} </
    option>";

    }

    ?>
    </select>
    </p>
    <p>Visible:
    <input type="
    radio" name="visible" value="0" <?php if($sel_page['visible'] == 0) {echo " checked";}
    ?> /> No
    &nbsp;
    <input type="
    radio" name="visible" value="1" <?php if ($sel_page['visible'] == 1) {echo " checked";}
    ?> /> Yes
    </p>
    <p> Content: <br />
    <textarea name="
    content" rows="20" cols="50"><?php echo $sel_page['content']; ?> </textarea>
    </p>

    <input type="
    submit" name="submit" value="Edit Page" /> &nbsp; &nbsp;
    <a href="
    delete_page.php?page=<?php echo urlencode($sel_page['id']); ?>" onclick="return confirm('Are you sure you want to delete this page?');"> Delete Page </a>
    &nbsp; &nbsp;
    <a href="
    content.php?page=<?php echo $sel_page['id']; ?>">Cancel</a>

    </form>


    </div>


    I tried posting in Stackoverflow but didn't get a helpful response and I've sometimes had more luck here. The questions and responses that were posted on the other site are below .. maybe someone on here can help me get a little further in trying to figure this out...


    What is the exact error message ? and if you log/echo the actual query before its executed what does it look like ? and what datatype is the visible field in MySQL ? – ManseUK Jul 13 at 10:44
    What's the description of the visibility field in table? – Dainis Abols Jul 13 at 10:44
    when I hit submit it reloads the page and says: There was 1 error in the form. Please review the following fields: - visible – ristenk1 Jul 13 at 13:45
    the mysql databtype for visible is tinyint(1).. @ManseUK- I'm not sure how to log/echo the actual query before its exected – ristenk1 Jul 13 at 13:49
    Last edited by ristenk1; 07-16-2012 at 04:31 PM.

  2. #2
    WebProWorld MVP williamc's Avatar
    Join Date
    Jul 2003
    Location
    On a really big hill in Kentucky
    Posts
    4,721
    Hi Ristenk, I will be happy to help you.

    after this line:

    $result = mysql_query($query, $connection);

    add this:

    Code:
    echo "--- Start Debug ---<br>\n" . $query . "<br>\n";
    if(mysql_error()){ echo mysql_error() . "<br>--- End Debug ---<br>\n"; }
    then paste me the output inside the 'Start Debug' and 'End Debug' lines
    William Cross
    Web Development by Those Damn Coders
    Firearm Friendly Websites because our constitution matters

  3. #3
    Senior Member ristenk1's Avatar
    Join Date
    Apr 2009
    Posts
    122
    Hi William and Weegillis (haven't checked the equal signs yet but ran the code - sorry, trying to do this between work and running home to the kids so that leaves me with barely any time - although I should have no excuses!) So this is what I got when I put the code in...

    When I changed the menu_name in the edit page and I got a successful update and the code read this at the top of the page:
    --- Start Debug ---
    UPDATE pages SET menu_name = 'History!yes!2', position = 1, visible = 1, content = 'This is the company history! ' WHERE id = 1

    but when I changed the visible to "no" and tried any other edit I got nothing at the top of the page and only the error I had thrown previously:
    here was 1 error in the form.


    Please review the following fields:
    - visible


    so that means that it's not triggering your debug code at all... so, I'm not sure what that means

  4. #4
    WebProWorld MVP williamc's Avatar
    Join Date
    Jul 2003
    Location
    On a really big hill in Kentucky
    Posts
    4,721
    Ok, first off let's fix things that are bothering the heck out of me, and move downwards.

    change this line:

    <form action="edit_page.php?page=<?php echo urlencode($sel_page['id']); ?>"
    method="post">

    to this:

    Code:
    <form action="edit_page.php" method="post">
    <input type="hidden" name="page" value="<?php echo urlencode($sel_page['id']); ?>" />
    See if that helps first then we will work downwards from there.

    - - - Updated - - -

    also, you are using an external function called mysql_prep() to prepare the fields for mysql, but do you actually have the function in your code somewhere?
    William Cross
    Web Development by Those Damn Coders
    Firearm Friendly Websites because our constitution matters

  5. #5
    Senior Member ristenk1's Avatar
    Join Date
    Apr 2009
    Posts
    122
    that breaks it.. it doesn't update anything and just resets the edit_page to the content_page

  6. #6
    Junior Member
    Join Date
    Jul 2012
    Posts
    4
    It seems likely that the SQL statement is invalid, possibly because the "visible" field is not being returned by the browser.

    Williamc was on the right track, but the debugging code was inserted at the wrong places.

    Please do the following:
    1. Revert the second change that williamc had you make--your code expects to find "page" in the $_GET array, so it needs to remain part of the form's Action HREF. (Technically it would be better if this variable was submitted using the hidden input field as was demonstrated, but obviously that would not fit in with the overall design of the site.)
    2. Have your php code show what variables and values are being posted by the browser, and the details of the reported errors.
    After this line:
    PHP Code:
    $content mysql_prep($_POST['content']); 
    Insert:
    PHP Code:
    echo '<pre>POST: ' print_r($_POSTtrue) . '</pre>';
    echo 
    '<pre>errors: ' print_r($errorstrue) . '</pre>'
    3. If there are no errors, display the SQL query (same code that williamc had you first add).
    After this line
    PHP Code:
    $result mysql_query($query$connection); 
    Insert:
    PHP Code:
    echo "--- Start Debug ---<br>\n" $query "<br>\n";
    if(
    mysql_error()){ echo mysql_error() . "<br>--- End Debug ---<br>\n"; } 
    These together should allow you to observe what data the browser is sending, how your code treats it, and whether or not the output is as you expect. I'm curious if the mysql_prep() function is translating the posted value to a boolean true/false, because boolean values frequently don't do well when simply concatenated into a string.

    Please post back with the extra info.
    Last edited by LordRegent; 07-18-2012 at 12:45 PM. Reason: inadvertent submission

  7. The following user agrees with LordRegent:
  8. #7
    WebProWorld MVP williamc's Avatar
    Join Date
    Jul 2003
    Location
    On a really big hill in Kentucky
    Posts
    4,721
    You are correct on both instances. I didn't think that the prep function could be changing types on her, as she didn't paste that function with her code so we had no idea what it was actually doing to the incoming data.
    William Cross
    Web Development by Those Damn Coders
    Firearm Friendly Websites because our constitution matters

  9. #8
    Senior Member ristenk1's Avatar
    Join Date
    Apr 2009
    Posts
    122
    Thank you. I realize that this is kind of like trying to put pieces of a puzzle together and not knowing what is and is not missing when dealing with someone that isn't familiar with php. The mysql_prep function is below as is the navigation and find_selected_page that sets the $sel_page:

    function mysql_prep($value)
    {
    $magic_quotes_active = get_magic_quotes_gpc();
    $new_enough_php = function_exists("mysql_real_escape_string"); //i.e. PHP >= v4.3.0
    if($new_enough_php)
    {//PHP v4.3.0 or higher
    //undo any magic quote effects so mysql_real_escape_string can do the work
    if($magic_quotes_active) {$value = stripslashes($value);}
    $value = mysql_real_escape_string($value);}
    else { //before PHP v4.3.0
    //if magic quotes aren't already on then add slashes manually
    if(!$magic_quotes_active){$value = addslashes($value);}
    //if magic quotes are active, then the slashes already exist
    }
    return $value;
    }
    function find_selected_page () {
    global $sel_subject;
    global $sel_page;


    if(isset($_GET['subj'])){
    $sel_subject = get_subject_by_id($_GET['subj']);
    $sel_page = NULL;

    }
    elseif (isset($_GET['page'])){
    $sel_subject = NULL;
    $sel_page = get_page_by_id($_GET['page']);

    }
    else{
    $sel_subject = NULL;
    $sel_page = NULL;
    }


    }


    function navigation($sel_subject, $sel_page, $public = false){
    $output = "<ul class='subjects'>";
    $subject_set = get_all_subjects($public);
    while($subject = mysql_fetch_array($subject_set)) {
    $output .= "<li";
    if($subject["id"] == $sel_subject['id']) {
    $output .= " class='selected'";
    }
    $output .= "> <a href=\"edit_subject.php?subj=" . urlencode($subject["id"]) . "\"> {$subject['menu_name']} </a> </li>";
    $page_set = get_pages_for_subject($subject["id"], $public);
    $output .= "<ul class='pages'>";
    while($page = mysql_fetch_array($page_set)) {
    $output .= "<li";
    if($page["id"] == $sel_page['id']) {
    $output .= " class='selected'";
    }
    $output .= "> <a href=\"content.php?page=" . urlencode($page["id"]) . "\"> {$page['menu_name']} </a> </li>";
    }
    $output .= "</ul>";

    }

    $output .= "</ul>";
    return $output;
    }



    I did the updates that LordRegent said and got the following results:

    When I did a regular edit to the page with visibility-yes it outputted:

    he page was successfully updated.


    POST:
    errors: Array
    (
    )
    --- Start Debug ---
    UPDATE pages SET menu_name = 'History!yes!5', position = 1, visible = 1, content = 'This is the company history! ' WHERE id = 1



    When I did an edit to the page with visibility-no it outputted:

    There was 1 error in the form.


    Please review the following fields:
    - visible


    POST:
    errors: Array
    (
    [0] => visible
    )

  10. #9
    Junior Member
    Join Date
    Jul 2012
    Posts
    4
    The debugging output does not contain the content of the POST array; please make sure the the following line is entered correctly (all characters are significant):
    PHP Code:
    echo '<pre>POST: ' print_r($_POSTtrue) . '</pre>'
    Also, to make sure that we can see the variables being submitted by the browser, please insert the following just after the command shown above:
    PHP Code:
    echo '<pre>REQUEST: ' print_r($_REQUESTtrue) . '</pre>'

  11. #10
    Senior Member ristenk1's Avatar
    Join Date
    Apr 2009
    Posts
    122
    Good Morning The first test is visible yes... sucessful and the second visible no... unsuccessful... error messages below:


    POST:
    REQUEST: Array
    (
    [page] => 1
    [menu_name] => History1!
    [position] => 1
    [visible] => 1
    [content] => This is the company history!
    [submit] => Edit Page
    )
    errors: Array
    (
    )
    --- Start Debug ---
    UPDATE pages SET menu_name = 'History1!', position = 1, visible = 1, content = 'This is the company history! ' WHERE id = 1

    POST:
    REQUEST: Array
    (
    [page] => 1
    [menu_name] => History2!
    [position] => 1
    [visible] => 0
    [content] => This is the company history!
    [submit] => Edit Page
    )
    errors: Array
    (
    [0] => visible
    )


    this is the debugging code added so far that is running:

    //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>';
    Last edited by ristenk1; 07-20-2012 at 11:19 AM. Reason: adding info

Page 1 of 2 12 LastLast

Posting Permissions

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