sub update_insert_Record { warn("522 update_insert_Record"); my $post = $query->param("post"); #UPDATE existing record if ($post == 0) { #Fill variables with new update request data (from form) my $id_1 = $query->param("id"); my $user_id_1 = $query->param("user_id"); my $username_1 = $query->param("username"); my $password_1 = $query->param("password"); my $pin_1 = $query->param("pin"); my $position_1 = $query->param("position"); my $forename_1 = $query->param("forename"); my $lastname_1 = $query->param("lastname"); my $business_1 = $query->param("business"); my $address1_1 = $query->param("address1"); my $address2_1 = $query->param("address2"); my $city_1 = $query->param("city"); my $state_1 = $query->param("state"); my $zip_1 = $query->param("zip"); my $email_1 = $query->param("email"); my $phone_home_1 = $query->param("phone_home"); my $phone_cell_1 = $query->param("phone_cell"); my $comments_1 = $query->param("comments"); my $MJ_1 = $query->param("MJ"); my $MD_1 = $query->param("MD"); my $DD_1 = $query->param("DD"); my $DP_1 = $query->param("DP"); my $newData = { id => $id_1, user_id => $user_id_1, username => $username_1, password => $password_1, pin => $pin_1, position => $position_1, forename => $forename_1, lastname => $lastname_1, business => $business_1, address1 => $address1_1, address2 => $address2_1, city => $city_1, state => $state_1, zip => $zip_1, email => $email_1, phone_home => $phone_home_1, phone_cell => $phone_cell_1, comments => $comments_1, MJ => $MJ_1, MD => $MD_1, DD => $DD_1, DP => $DP_1 }; #Declare variables to hold current record data my ($id,$user_id, $username,$password,$pin,$position,$forename,$lastname,$business,$address1,$address2,$city, $state,$zip,$email,$phone_home,$phone_cell,$comments,$MJ,$MD,$DD,$DP); #Recover existing values from database and fill variables my $stmt = "SELECT * FROM users WHERE user_id = $user_id_1 ORDER BY user_id ASC"; warn("statement = '$stmt'"); my $sth = $dbh->prepare ($stmt) or die "Error Preparing:\n" . $stmt . "\nDBI returned: \n", $dbh->errstr; DBI->trace(3,$dbitracelog); $sth->execute() or die "Unable to execute query: " . $sth->errstr; $sth->bind_columns (\$id,\$user_id, \$username,\$password ,\$pin ,\$position ,\$forename ,\$lastname ,\$business ,\$address1 ,\$address2 ,\$city, \$state ,\$zip ,\$email ,\$phone_home ,\$phone_cell ,\$comments ,\$MJ ,\$MD ,\$DD ,\$DP); my $bindResult = $sth->fetchrow_array(); my $oldData = { id => $id, user_id => $user_id, username => $username, password => $password, pin => $pin, position => $position, forename => $forename, lastname => $lastname, business => $business, address1 => $address1, address2 => $address2, city => $city, state => $state, zip => $zip, email => $email, phone_home => $phone_home, phone_cell => $phone_cell, comments => $comments, MJ => $MJ, MD => $MD, DD => $DD, DP => $DP }; my @fields; my @values; # compare old v new for (sort keys %$newData){ if ($newData->{$_} ne $oldData->{$_}){ push @fields,"$_ = ?"; push @values,$newData->{$_}; } } # skip if no change if (@fields == 0){ print "No update required\n"; } else { # build sql my $fields = join ',',@fields; my $stmt = "UPDATE table SET $fields WHERE user_id = ?"; #add id push @values,$user_id; warn("statement = '$stmt'"); # prepare and execute sql # print "$stmt\n(@values)\n"; $sth = $dbh->prepare ($stmt) or die "Error Preparing:\n" . $stmt . "\nDBI returned: \n", $dbh->errstr; $sth->execute() or die "Unable to execute query: " . $sth->errstr; return; } }