AAA
AAA

Reputation: 3168

On update, PHP is making all fields empty

I am using the following code. Whenever the form is used for an update, PHP will empty those fields in the db. I know this will happen if the field_names don't match, so i have triple checked them, they all work. This is the first time i am getting this error. Not sure what to do.

Code:

     //Getting Unique ID
     $sql = "select `Person_id` from `accounts` where `username` = '$username'";
     $query = mysql_query($sql) or die ("Error: ".mysql_error());

     while ($row = mysql_fetch_array($query)){

     $pid = $row['Person_ID'];
      }
         mysql_free_result($query);

      $newname = mysql_real_escape_string($_POST['Full_name']);
      $newgender = mysql_real_escape_string($_POST['Patient_gender']);
       $newmonth = mysql_real_escape_string($_POST['Month']);
       $newday = mysql_real_escape_string($_POST['Day']);
       $newyear = mysql_real_escape_string($_POST['Year']);
        $newacctss = mysql_real_escape_string($_POST['Acct_SS']);
        $newaddress = mysql_real_escape_string($_POST['Address']);
        $newaddress2 = mysql_real_escape_string($_POST['Address2']);
          $newcity = mysql_real_escape_string($_POST['City']);
         $newstate = mysql_real_escape_string($_POST['State']);
         $newzipcode = mysql_real_escape_string($_POST['Zip_code']);
          $newhomephone = mysql_real_escape_string($_POST['Home_phone']);
        $newcellphone = mysql_real_escape_string($_POST['Cell_phone']);
       $neworkphone = mysql_real_escape_string($_POST['Work_phone']);
       $newsure = mysql_real_escape_string($_POST['Sure']);
       $newfav = mysql_real_escape_string($_POST['Favorite']);
       $newcars = mysql_real_escape_string($_POST['Cars']);
       $newdrinks = mysql_real_escape_string($_POST['Drinks']);
         $newmoi = mysql_real_escape_string($_POST['About_moi']);


       //Update Name Only
      $sql = "UPDATE accounts SET `full_name` = '$newname' WHERE Username = '$username'"; 
       $query1 = mysql_query($sql) or die ("Error: ".mysql_error());


     //Everything else being udpated here 
    $sql2 = "UPDATE profile SET `Patient_gender` = '$newgender', 
    `Month` = '$newmonth', `Day` =  '$newday', `Year` = '$newyear', 
    `Acct_SS` = '$newacctssn', `Address` = '$newaddress', 
    `Address2` = '$newaddress2', `City` = '$newcity', `State` = '$newstate', 
    `Zip_code` = '$newzipcode', `Home_phone` = '$newhomephone', 
    `Cell_phone` = '$cellphone', `Work_phone` = '$neworkphone', 
    `Sure` = '$newsure', `Favorite` = '$newfav', `Cars` = '$newcars', 
     `Drinks` = '$newdrinks', `About_moi` = '$newmoi' WHERE Person_id = '$pid'"; 
      $query2 = mysql_query($sql2) or die ("Error: ".mysql_error());

Upvotes: 0

Views: 209

Answers (1)

Marc B
Marc B

Reputation: 360702

Array keys are case sensitive in PHP. You're querying the database for Person_id, but are fetching Person_ID. You want to check that you're actually getting something from your result set by checking $pid is empty after the fetch call and bail out if so.

As well, assuming your query only fetches a single row, you do not need the while() construct.

Beyond that, check that $newname is actually being created properly. If your form isn't submitting properly, or the name field is actually called something else, then you'll be inserting a blank into the database because the variable was never filled-in properly. CHeck what's being submitted by doing var_dump($_POST).

Upvotes: 3

Related Questions