0

I am needing a little help with updating a mysql table from data in an jquery array using ajax. I have tried searching for similar issues but could not find anything, or maybe I do not know the correct terms to search... I'm still fairly new to web dev/coding.

I'll try explain what I am trying to do as clearly as I can. I have a page with seats which users select by clicking on them, upon clicking the seat ID is added in its own span tag in a kind of shopping cart area on the left of the page. This works fine.

Upon checkout my js file is able to pick up these seat ID's in an array, but from here I am unsure of how to properly send this array to the php file, and then for the php file to read the seat ID's from the array and update the relevant seat rows to change the availability from 1 to 0 (this is using ajax).

Here is my code:

checkout.js

$(document).ready(function(){

$('#checkout').click(function(){
    var status = sessionStorage.getItem("username");

    if(sessionStorage.getItem("username")){
        var tickets = [];
        $("#myTickets").find("span").each(function(){ tickets.push(this.id); });

        var type = "POST",
        url = "scripts/sendSeatDetails.php"; 
        console.log(tickets);

        $.ajax ({
            url:url,
            type:type,
            data:tickets,
            success: function(response){
                if(response == 5){
                    alert("Seat update query failed");
                } else {
                    alert("success");
                }
            }

        });





    } else {
        alert("Before purchasing please log in. If you do not have an account please register.");
    }

});

});

In the console log this shows: ["A2", "A3", "A4"] (if I have selected seats with ID A2, A3, A4 etc).

sendSeatDetails.php

<?php
include("dbconnect.php");
$myseats=$_POST['tickets'];
$query="update seats set status='0' where seatnum=";
for($i=0;$i<count($myseats);$i++){
$query.="'$myseats[$i]'";
if($i<count($myseats)-1){
$query.=" || seatnum=";
}
}  
$link = mysql_query($query);
if (!$link) {
echo 5;
}
?>

This returns the alert showing success, but this is not true as the tables are not being updated. Can anyone help me with this or point me in the right direction?

I appreciate your help, and hopefully will be able to contribute to this site when I am at a better skill level in the future.

Many Thanks.

  • What is the result of $link when you dump it? – Matheno Nov 24 '14 at 14:54
  • 1
    Please, [don't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). You will also want to [Prevent SQL Injection!](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Nov 24 '14 at 15:01
  • 1
    Add error reporting to the top of your file(s) right after your opening ` – Jay Blanchard Nov 24 '14 at 15:02
  • When I dump $link (echo $link then console.log response in js file) I get some "notice: undefined index" error. @JayBlanchard - yes I plan on amending with PDO once I manage to get this working. Thanks. – skawwwt Nov 25 '14 at 00:51
  • Once you fix the undefined index the code should work. – Jay Blanchard Nov 25 '14 at 13:01

2 Answers2

0

To send an array in jQuery you have to serialize it then parse it in the PHP file:

data: tickets.serialize(),
...
parse_str($_POST['data'], $data);

And then you treat it as an ordinary array.

Useless Intern
  • 1,294
  • 1
  • 10
  • 20
0

Run update query one by one.

$myseats=$_POST['tickets'];
   $flag=0;// Total successful updates
    $myseats=explode(',',$myseats);
  for($i=0;$i<count($myseats);$i++){
     $query="update seats set status=0 where seatnum='".$myseats[$i]."'";
     $link = mysql_query($query);
      if (!$link) {
       $flag=$flag+1;
      }
    }

echo $flag;die;

Check response. it will be number of updated rows.

Confused
  • 1,602
  • 15
  • 25