var inquiryQuery = "http://www.xxxxxx.org/httpsdocs/cgi-bin/update_tables.cgi?action=delete_Record&user_id=" + userid + '"'; var data; $.getJSON(inquiryQuery, function(data){ //Check data for status flag. If flag, remove and send data on to form loadJSONFormData(data); }); #### sub delete_Record { my $userid = $query->param('user_id'); my $json = JSON->new; my $result; my $count; my %success; my $key; my $value; my ($sth, $stmt); warn("Delete record based on user_id = '$userid'"); #Delete the desired record $key = "DeleteRecord"; $stmt = "DELETE FROM users WHERE user_id = ?"; $sth = $dbh->prepare ($stmt) or die "Error Preparing:\n" . $stmt . "\nDBI returned: \n", $dbh->errstr; $sth->execute ($userid) or die "Unable to execute query: " . $sth->errstr; $value = (SELECT ROW_ COUNT()); if ($value == 0) { %success{$key} = $value; #Add "0" FLAG Delete failed no other action follows $json = JSON->new; $json->canonical(1); $json = encode_json(\%success); print "Content-Type: application/json\n\n"; print $json; warn("Finished print $json"); exit(0); } elsif ($value >= 1) { %success{$key} = $value; #Update FLAG "1" Delete success #check previous record $stmt = "SELECT * FROM users WHERE user_id <= (SELECT MAX(user_id) FROM users WHERE user_id < ?) ORDER BY user_id DESC LIMIT 1"; $sth = $dbh->prepare ($stmt) or die "Error Preparing:\n" . $stmt . "\nDBI returned: \n", $dbh->errstr; $sth->execute ($userid) or die "Unable to execute query: " . $sth->errstr; $result = $sth->fetchrow_hashref(); $count = $sth->rows; warn("count 1st Pass = '$count'"); if ($count == 0) { #check next record $stmt = "SELECT * FROM users WHERE user_id >= (SELECT MIN(user_id) FROM users WHERE user_id > ?) ORDER BY user_id ASC LIMIT 1"; $sth = $dbh->prepare ($stmt) or die "Error Preparing:\n" . $stmt . "\nDBI returned: \n", $dbh->errstr; $sth->execute ($userid) or die "Unable to execute query: " . $sth->errstr; $result = $sth->fetchrow_hashref(); $count = $sth->rows; warn("count 2nd Pass = '$count'"); $key ="ReturnedRecord"; if ($count == 0) { warn("No other record must have deleted last one"); %success( $key => $count); #Add "0" FLAG Failed to return new record after successful delete $json->canonical(1); $json = encode_json(\%success); print "Content-Type: application/json\n\n"; print $json; warn("Finished print 0 count $json"); exit(1); } elsif ($count == 1) { warn("count = '$count'"); $json->canonical(1); %success( $key => $count); #Add "1" FLAG return new record successful %result = (%result, %success); #add %success flags to $result $json = encode_json(\%result); print "Content-Type: application/json\n\n"; print $json; warn("Finished print $json"); exit(0); } } elsif ($count == 1) { warn("count = '$count'"); $json->canonical(1); %result = (%result, %success); #add %success flags to $result $json = encode_json(\%result); print "Content-Type: application/json\n\n"; print $json; warn("Finished print $json"); exit(0); } } }