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.

5
  • What is the result of $link when you dump it? Commented Nov 24, 2014 at 14:54
  • 1
    Please, don't use mysql_* functions, They are no longer maintained and are officially deprecated. Learn about prepared statements instead, and use PDO or MySQLi. You will also want to Prevent SQL Injection! Commented Nov 24, 2014 at 15:01
  • 1
    Add error reporting to the top of your file(s) right after your opening <?php tag error_reporting(E_ALL); ini_set('display_errors', 1); In addition add error reporting for your MySQL functions. Commented Nov 24, 2014 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. Commented Nov 25, 2014 at 0:51
  • Once you fix the undefined index the code should work. Commented Nov 25, 2014 at 13:01

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.