-1

Here is my code

function myfoo() {
  $.ajax({
    type: "POST",
    url: "update.php",
    dataType: 'json',
    async: false,
    data: {
      Eventid: 1,
      Seats: 'Seats'
    },
    success: function(r) {}
  });
}

$(window).on('beforeunload', function() {
  return 'Are you sure you want to leave?';
});
$(window).on('unload', function() {
  console.log('calling ajax');
  myfoo();
});

update.php code

<?php
session_start();
include 'conn.php';

if(isset($_SESSION['Seats'])) {
    $seatS=$_SESSION['Seats'];
    $Eventid=$_SESSION['Eventid'];
    $cats = explode(" ", $seatS);
    $cats = preg_split('/,/', $seatS, -1, PREG_SPLIT_NO_EMPTY);
    foreach($cats as $key => $cat ) {
        $cat  = mysql_real_escape_string($cats[$key]);
        $cat = trim($cat);
        if($cat !=NULL) {
            $stmt = $con->prepare('UPDATE fistevent SET `Status`=" " where `Event_Id`=? AND `seats`="'.$cat.'" AND `Status`="Hold" ');
            $stmt->bind_param("s", $_SESSION['Eventid']);
            $stmt->execute();
            session_destroy();
        }
    }
}

?>
Barmar
  • 741,623
  • 53
  • 500
  • 612
krish
  • 120
  • 14

1 Answers1

1

The AJAX data: parameters are put in $_POST, not $_SESSION.

You can't use mysql_real_escape_string if you're using mysqli. You don't need to escape parameters when you're using bind_param(). And you can use explode() instead of preg_split(), since there's no regular expression pattern in your delimiter; array_filter() can be used to remove blank entries.

<?php
include 'conn.php';

if(isset($_POST['Seats'])) {
    $seatS=$_POST['Seats'];
    $_SESSION['Seats'] = $seatS;
    $Eventid=$_POST['Eventid'];
    $_SESSION['Eventid'] = $Eventid;
    $cats = array_filter(array_map('trim', explode(',', $seatS)));
    $stmt = $con->prepare('UPDATE fistevent SET `Status`=" " where `Event_Id`=? AND `seats`= ? AND `Status`="Hold" ') or die($con->error);
    $stmt->bind_param("ss", $_POST['Eventid'], $cat);
    foreach($cats as $key => $cat ) {
        $stmt->execute();
    }
}
?>
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • sir there is no errors but it does not updating the records – krish Aug 31 '17 at 06:47
  • Can you execute the query by hand? `UPDATE fistevent SET Status = " " WHERE Event_Id = 1 AND seats = "Seats" AND Status = "Hold"` – Barmar Aug 31 '17 at 06:51
  • i think the problem is passing the wrong data .i have to send this data in ajax $seatS=$_SESSION['Seats']; $Eventid=$_SESSION['Eventid'];.. hows it possible? – krish Aug 31 '17 at 06:53
  • You can't set session variables in Javascript. It can only send `$_POST` and `$_GET` variables. You can set the session variables from the post parameters if you want, but I don't see why that's needed for this query. – Barmar Aug 31 '17 at 06:54
  • or this field data >> – krish Aug 31 '17 at 06:55
  • i have to send these input fields data in data: {Eventid: 1,Seats:'Seats'}, so that i can get selected Eventid and selected seats on update.php – krish Aug 31 '17 at 06:59
  • `{ EventId: $("#Eventid").val(), Seats: $("#finalSeats").val() }` – Barmar Aug 31 '17 at 07:14
  • still not updating the records – krish Aug 31 '17 at 07:39
  • thanks sir .there is miner mistake in code that we are using Eventid but in data post name is EventID – krish Aug 31 '17 at 08:18
  • @krish So the problem turned out to be just a typo? – Barmar Aug 31 '17 at 16:53