0

user is on a page where he has to confirm sign-out of an activity. When clicking on OK the entry of his signup should be deleted in the database and after that he should be redirected. here is the code of the button-handler:

$("#OK").click(function()
    {
        unsubscribeMember();
         window.location.href="signup-for-event.php?memId=" + <?php echo $_SESSION['memberId'] ?>;
    });

here is the code of the function unsubscribeMember();

function unsubscribeMember()
{
    var data = "memId=" + <?php echo $_SESSION['memberId'];?> + "&actID=" + <?php echo $_GET['actID']; ?> ;
    alert(data);

    $.ajax({
        dataType: 'json',
        url: 'json-responses.php?fct=deleteMemberFromActivity',
        data: data,
        cache: false
    });
}

Function is being called properly which is shown by the output of the alert (data); "memId=1600&actID=302"

the json-responses.php file contains these lines to call the dataLayer file:

if ($_GET['fct'] == 'deleteMemberFromActivity')
{
    $result = deleteMemberFromActivity($connectionObject, $_GET['actID'], $_GET['memId']);
    echo $result;
}

the dataLayer code looks like this:

function deleteMemberFromActivity($PDOdbObject, $actId, $memberId)
{
    $deleteSignUpsSQL = "DELETE FROM 'member_activity' WHERE 'activity_id' = ? AND 'member_id' = ?";
    $stmt = $PDOdbObject->prepare($deleteSignUpsSQL);
    $stmt -> execute([$actId, $memberId]);
    $affected_rows = $stmt->rowCount();
    return $affected_rows;
}

but when the user clicks on the button, redirect works fine but the deletion in the database does not happen

Lelio Faieta
  • 6,457
  • 7
  • 40
  • 74
Alle V.
  • 19
  • 1
  • 9
  • 3
    Possible duplicate of [When to use single quotes, double quotes, and back ticks in MySQL](https://stackoverflow.com/questions/11321491/when-to-use-single-quotes-double-quotes-and-back-ticks-in-mysql) – aynber May 22 '18 at 12:39
  • In the data you send with jQuery you use "memId" and in the PHP script you use $_GET['memberId'] .. Also, I would suggest to use POST instead of GET when you send data. – ARN May 22 '18 at 12:48
  • @ARN just corrected the memberId to memId, still the same. data entry does not get deleted in database – Alle V. May 22 '18 at 13:00
  • $_GET['memberId'] should be $_GET['memId'] according to your code. – Gopalakrishnan May 22 '18 at 13:04
  • In addition to @Gopalakrishnan 's comment, try to put async:false in your ajax, because it's possible that your redirection is happening before ajax call – Ivan Gajic May 22 '18 at 13:18
  • 1
    @IvanGajic No, no, no, and again, no. NEVER do `async:false` – Patrick Q May 22 '18 at 13:24
  • I agree with @PatrickQ – Felipe G. May 22 '18 at 13:26
  • The asynchronous nature of the request is the _problem_ here, but making it synchronous is not the _solution_. – CBroe May 22 '18 at 13:29
  • thanks guys errors found: after correcting memberId to memId and adding `async:false` its working – Alle V. May 22 '18 at 13:31
  • 1
    @AlleV. I will say it one more time, please, for the love of everything holy (or unholy if you please), do NOT do `async:false`. This WILL lead to negative user experiences and some browsers have started to disallow this terrible programming loophole. – Patrick Q May 22 '18 at 13:33
  • Can try with specified post type like `type : "GET"`, in ajax call without `async` – Gopalakrishnan May 22 '18 at 14:35
  • OK I tried to remove the `async:false`. It is NOT not working at all, but it only works sometimes. Have the impression it works more often if I wait a few more seconds before confirming "OK". adding or removing `type:"GET"`does not seem to make a difference. – Alle V. May 23 '18 at 19:04

1 Answers1

0

Your AJAX call isn't referencing actID. The only thing you pass is fct=deleteMemberFromActivity. Your server has no idea which user you are trying to delete.

Also, you should post these values. Any user could see the structure you are using and swap out data in the URL to send data to your server.

cngodles
  • 428
  • 6
  • 14