#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0

    Unhappy Need HELP on PHP Form MySQL Update


    Hi Everyone,

    I'm new in PHP and trying to update the page content of my database on my edit_page.php I created. Unfortunately, I'm getting an error from my form functions:
    "Please review the following fields:
    - info1
    - info2
    - info3
    - page"

    ...any help is much appreciated. Sincerely, Teepee.

    Here's my Edit Page:
    if (intval($_GET['page']) == 0) {
    redirect_to('content.php');
    }

    include_once("includes/form_functions2.php");

    // START FORM PROCESSING
    // only execute the form processing if the form has been submitted
    if (isset($_POST['submit'])) {
    // initialize an array to hold our errors
    $errors = array();

    // perform validations on the form data
    $required_fields = array('page', 'position', 'visible', 'info1', 'info2', 'info3' );
    $errors = array_merge($errors, check_required_fields($required_fields));

    $fields_with_lengths = array('page' => 30);
    $errors = array_merge($errors, check_max_field_lengths($fields_with_lengths));

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

    // Database submission only proceeds if there were NO errors.
    if (empty($errors)) {
    $query = "UPDATE main SET
    page = '{$page}',
    position = {$position},
    visible = {$visible},
    info1 = '{$info1}'
    info2 = '{$info2}'
    info2 = '{$info2}'
    WHERE id = {$id}";
    $result = mysql_query($query);
    // test to see if the update occurred
    if (mysql_affected_rows() == 1) {
    // Success!
    $message = "The page was successfully updated.";
    } else {
    $message = "The page could not be updated.";
    $message .= "<br />" . mysql_error();
    }
    } else {
    if (count($errors) == 1) {
    $message = "There was 1 error in the form.";
    } else {
    $message = "There were " . count($errors) . " errors in the form.";
    }
    }
    // END FORM PROCESSING
    }
    ?>

    And the Form Function:

    <?php
    function check_required_fields($required_array) {
    $field_errors = array();
    foreach($required_array as $fieldname) {
    if (!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) && $_POST[$fieldname] != 0)) {
    $field_errors[] = $fieldname;
    }
    }
    return $field_errors;
    }

    function check_max_field_lengths($field_length_array) {
    $field_errors = array();
    foreach($field_length_array as $fieldname => $maxlength ) {
    if (strlen(trim(mysql_prep($_POST[$fieldname]))) > $maxlength) { $field_errors[] = $fieldname; }
    }
    return $field_errors;
    }

    function display_errors($error_array) {
    echo "<p class=\"errors\">";
    echo "Please review the following fields:<br />";
    foreach($error_array as $error) {
    echo " - " . $error . "<br />";
    }
    echo "</p>";
    }

    ?>

    Many thanks again!
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,316
    Rep Power
    7170
    Presumably that's because those fields are not filled out. If they are filled out, it probably means you have an error in the HTML for your form (like the fields not being named correctly or not being inside your form tag).

    PHP Code:
     page '{$page}',
    position = {$position},
    visible = {$visible},
    info1 '{$info1}'
    info2 '{$info2}'
    info2 '{$info2}' 
    You have an error in your SQL code, you're missing a couple commas. Also put quotes around $position and $visible or your code will be open to SQL injection.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0
    Thanks for your reply, E-Oreo. I really appreciate it.Will make some changes on the areas you pointed out...

    Originally Posted by E-Oreo
    Presumably that's because those fields are not filled out. If they are filled out, it probably means you have an error in the HTML for your form (like the fields not being named correctly or not being inside your form tag).

    PHP Code:
     page '{$page}',
    position = {$position},
    visible = {$visible},
    info1 '{$info1}'
    info2 '{$info2}'
    info2 '{$info2}' 
    You have an error in your SQL code, you're missing a couple commas. Also put quotes around $position and $visible or your code will be open to SQL injection.

IMN logo majestic logo threadwatch logo seochat tools logo