Blank field posting, need help

  • jordonshaw
  • Student
  • Student
  • User avatar
  • Posts: 96
  • Loc: Tennessee

Post 3+ Months Ago

Ok guys, this is what I'm wanting to do. I have a form that allows me to update my user accounts. I want the script to only update the password, when there is something there. So, if I go to update the user and don't put anything in for the password, it will stay the same; however, if I do change the password, it will update. I tried writing the below code, using the isset for the field. The problem is, the script is treating blank or no entry as being set, so it's basically posting nothing in the password field. Here is my code:

Code: [ Select ]
 
<?php
    if (isset($_POST['submit'])){
    $errors = array();
     
      $required_fields = array('f_name', 'l_name', 'u_name');
      $errors = array_merge($errors, check_required_fields($required_fields, $_POST));
      $uid = stripslashes(str_replace("'", "''", ($_GET['usr'])));
      $firstname = stripslashes(str_replace("'", "''", ($_POST['f_name'])));
      $lastname = stripslashes(str_replace("'", "''", ($_POST['l_name'])));
      $username = stripslashes(str_replace("'", "''", ($_POST['u_name'])));
      $password = stripslashes(str_replace("'", "''", ($_POST['p_word'])));
      $hashed_password = sha1($password);
      $access = $_POST['alevel'];
         
      if ( empty($errors)) {
      $userup = "UPDATE SNP_Users SET" ;
        $userup .= " first_name = '{$firstname}',";
        $userup .= " last_name = '{$lastname}',";
        $userup .= " username = '{$username}',";
        if (isset($_POST['p_word'])) {
        $userup .= " password = '{$hashed_password}',";
        }
       
        $userup .= " access_level = '{$access}'";          
        $userup .= " WHERE user_id = '{$uid}'";
        $user_results = mssql_query($userup, $connection);
    if ($user_results) {
          $message = "User was updated.";
    } else {
        die("Database query failed: " . mssql_error());
    }
      } else {
          $message = "You had an error.<br />Please check required fields!" ;
        }
    }
?>
 
  1.  
  2. <?php
  3.     if (isset($_POST['submit'])){
  4.     $errors = array();
  5.      
  6.       $required_fields = array('f_name', 'l_name', 'u_name');
  7.       $errors = array_merge($errors, check_required_fields($required_fields, $_POST));
  8.       $uid = stripslashes(str_replace("'", "''", ($_GET['usr'])));
  9.       $firstname = stripslashes(str_replace("'", "''", ($_POST['f_name'])));
  10.       $lastname = stripslashes(str_replace("'", "''", ($_POST['l_name'])));
  11.       $username = stripslashes(str_replace("'", "''", ($_POST['u_name'])));
  12.       $password = stripslashes(str_replace("'", "''", ($_POST['p_word'])));
  13.       $hashed_password = sha1($password);
  14.       $access = $_POST['alevel'];
  15.          
  16.       if ( empty($errors)) {
  17.       $userup = "UPDATE SNP_Users SET" ;
  18.         $userup .= " first_name = '{$firstname}',";
  19.         $userup .= " last_name = '{$lastname}',";
  20.         $userup .= " username = '{$username}',";
  21.         if (isset($_POST['p_word'])) {
  22.         $userup .= " password = '{$hashed_password}',";
  23.         }       
  24.         $userup .= " access_level = '{$access}'";          
  25.         $userup .= " WHERE user_id = '{$uid}'";
  26.         $user_results = mssql_query($userup, $connection);
  27.     if ($user_results) {
  28.           $message = "User was updated.";
  29.     } else {
  30.         die("Database query failed: " . mssql_error());
  31.     }
  32.       } else {
  33.           $message = "You had an error.<br />Please check required fields!" ;
  34.         }
  35.     }
  36. ?>
  37.  


Any help would be greatly appreciated!!!

Thanks,
Jordon Shaw
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • joebert
  • Sledgehammer
  • Genius
  • User avatar
  • Posts: 13496
  • Loc: Florida

Post 3+ Months Ago

Use !empty instead of isset.

Browsers tend to send the field even if it's blank when it comes to text boxes. The empty function returns whether the variable equates to a null value, which would be the case with a missing or empty form field.
  • jordonshaw
  • Student
  • Student
  • User avatar
  • Posts: 96
  • Loc: Tennessee

Post 3+ Months Ago

Thank you so much! That worked perfectly! It amazing, sometimes I just can't think. I have used the empty command in the past, it just wasn't coming to me.

Once again, thanks for your help!!!

Jordon

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 76 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.