SitePoint Sponsor

User Tag List

Results 1 to 13 of 13

Threaded View

  1. #1
    SitePoint Member
    Join Date
    May 2012
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    What is wrong with my code?

    WHAT is WRONG WHIT THIS CODE?

    PHP Code:
    if (isset($_POST['submit'])) {
    $errors = array();

    $required_fields = array('menu_name''position''visible');
    foreach(
    $required_fields as $fieldname) {
    if (!isset(
    $_POST[$fieldname]) || (empty($_POST[$fieldname]) && $_POST[$fieldname] != 0)) { 
    $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)) {
    // Perform Update
    $id mysql_prep($_GET['subj']);
    $menu_name mysql_prep($_POST['menu_name']);
    $position mysql_prep($_POST['position']);
    $visible mysql_prep($_POST['visible']);

    $query "UPDATE subjects SET 
    menu_name = '
    {$menu_name}', 
    position = 
    {$position}
    visible = 
    {$visible} 
    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.";
    }

    I have a form with ID, MENU_NAME, POSITION and VISIBLE and i'm trying to validate the form by checking if the fields are empty OR set with foreach and if i'ts empty make $errors[] = $fieldname.. And I test IF errors is empty execute code if it has something in it don't.. but its not listening to me... Can you help me? Thank you in advance !
    Last edited by TheRaptor; May 10, 2012 at 11:24. Reason: reformatted code with [PHP] tags


Bookmarks

Posting Permissions

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