0

Simple question guys , i have AJAX that pickup all data from page and it suppose to open new php page to update MySQL database , its only updating last row of data , BUT when i use alert from javascript just to check all data i got he does update whole table ... is there any chance that AJAX is not working fast enough or something?

here is my code

var request_type;
var browser = navigator.appName;

if (browser == "Microsoft Internet Explorer") {
    request_type = new ActiveXObject("Microsoft.XMLHTTP");
}
else {
    request_type = new XMLHttpRequest();
}

var http = request_type;

var MatchID = '';
var HomeTeam = '';
var AwayTeam = '';
var TipID = '';
var arrayMaxValues = 3;
var myArray = new Array(3);
var i = 0;
$('#teams_table input[type=text]').each(function () {

        myArray[i] = $(this).val();

        if (!!myArray[2]) 
        {
                 MatchID = myArray[0];
                 HomeTeam = myArray[1];
                 AwayTeam = myArray[2];


           if (HomeTeam > AwayTeam) {
                  TipID = 1;
           }
           else if (HomeTeam == AwayTeam) {
                  TipID = 2;
           }
           else if (HomeTeam < AwayTeam) {
                  TipID = 3;
           }



           http.open('get', 'adminUpdate.php?MatchID=' + MatchID + '&TipID=' + 
           TipID + '&HomeTeam=' + HomeTeam + '&AwayTeam=' + AwayTeam, true);
           http.send(null);

           myArray = new Array(3);
           i=0;

        }
        else
        {
         i++;
        }

});

It is kinda odd to me when i use

 alert('MatchID = ' + MatchID + ' HomeTeamScore = ' + HomeTeam + ',
 AwayTeamScore = ' + AwayTeam)

Inside of AJAX code i get whole table updated , without it just last row

And my php page

<?php

include('config.php');

$matchID = $_GET['MatchID'];
$tipID = $_GET['TipID'];
$HomeScore = $_GET['HomeTeam'];
$AwayScore = $_GET['AwayTeam'];

$query="update probatip1.matches set ResultTipID=".$tipID.",HomeTeamScore = "
.$HomeScore.",AwayTeamScore= ".$AwayScore." where MatchID =".$matchID;

$UpdateGame= mysql_query($query) or die(mysql_error());

mysql_close()

?> 
9
  • 1
    What is http? You seem to be using jQuery; why are you not using $.ajax or $.get? Commented Aug 31, 2012 at 1:12
  • seems like you're already using jquery are you open to jquery solutions like $.post or $.get? Commented Aug 31, 2012 at 1:12
  • 1
    You should combine all your changes in one ajax request instead of doing one per input. And you really need to switch to PDO or mysqli and prepared statements. Commented Aug 31, 2012 at 1:14
  • 1
    You're using one XMLHttpRequest(assuming http is an XMLHttpRequest object) to send multiple request at the same time, thats not going to work. Commented Aug 31, 2012 at 1:16
  • hmm but when i add that alert() and browser showing me right data , and it does update MySQL table right ... problem is i am picking up data from table with foreach and i put it inside of array and when array is full i send it for updating , and then reseting array to null and refill it again and im sending all data with get method to another php page Commented Aug 31, 2012 at 1:19

4 Answers 4

1

Try encoding the data. i.e:

MatchID = encodeURIComponent(myArray[0]);
HomeTeam = encodeURIComponent(myArray[1]);
AwayTeam = encodeURIComponent(myArray[2]);

in php use

function escapedata($data) {
    if(get_magic_quotes_gpc()) {
        $data= stripslashes($data);
    }
    return mysql_real_escape_string($data);
}

to escape your data before updating the table. i.e:

$query="update probatip1.matches set ResultTipID=".escapedata($tipID).",HomeTeamScore = ".escapedata($HomeScore).",AwayTeamScore= ".escapedata($AwayScore)." where MatchID =".escapedata($matchID);

Hope this works.

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

5 Comments

I just tried that code , and result is same ... it still update only last row ... but thanks for trying
I think the problem is from if(!!myArray[2]). Try using if(!myArray[2]). ! in it stands for not. not not gives you the opposite of not. That means if(!!myArray[2]) is the same as if(myArray[2]) and that is why it picks the last row.
hmm i am using that part just to check is my 3rd element of array filled with data , because i pickup 3 data from HTML table, so when that element is filled , data is ready to be send for updating , and after sending it i just fully empty my array and reset i=0;
I guess what you need for that is if(myArray.length== arrayMaxValues). Showing us the HTML side of this would be helpful.
I can add HTML side , but there is nothing special , just table with 8 table rows with data selected from MySQL , and there is button with event onclick() calling this script i posted above ... idea is that user add just results of match and with script i take those data and use it to update MySQL
1

Not really a direct answer, just something that you can base your answer from. What the code does is to submit a whole object using the $.post method in jquery which takes in 2 parameters and a callback function which is executed once the request is done.Not really sure by: open new php page to update MySQL database but I assume that you're simply using that page to update the database and not actually open it.

<script src="js/jquery.min.js"></script>
<script>
var obj = {
    'teams' : [
        {'name' : 'teamA', 'grade' : 'A'},
        {'name' : 'teamB', 'grade' : 'B'}
    ]
};

$.post('access.php', {'obj' : obj}, function(data){
    var d = JSON.parse(data);
    for(var x in d){
        console.log(d[x].name);
    }
});
</script>

access.php:

<?php
$post = $_POST['obj']['teams'];
$array = [];
foreach($post as $row){
    $name =  $row['name'];
    $grade = $row['grade'];

    $array[] = ['name'=>$name, 'grade'=>$grade];
}

echo json_encode($array);
?>

So you only have to modify the php page, and put your database query inside the loop. This way you won't need to perform so many ajax request by putting it inside $.each

Then utilize $.each to build the object that you're going to submit via ajax through $.post method:

var obj = {};
$().each(function(index){
     var myArray[i] = $(this).val();

     var MatchID = myArray[0];
     var HomeTeam = myArray[1];
     var AwayTeam = myArray[2];

     obj[index] = [];
     obj[index]['match_id'] = MatchID;
});

Comments

0

The problem is with your logic in the way you are sending requests to php file to update the MYSQL. Actually you are running the ajax request in a loop and the loop is too fast that kills the previous update request.

Solution

You can compose an array and send it to the php outside the loop. That will work for you.

1 Comment

Thank you ill try to make it that way
0

Guys with your help i managed to fix my problem

http.open('get', 'adminUpdate.php?MatchID=' + MatchID + '&TipID=' + TipID + 
'&HomeTeam=' + HomeTeam + '&AwayTeam=' + AwayTeam, false);
http.send(null);
var response = http.responseText;

So basicly with this line i told http request not to go for next line of code until update in table is not completed , when http has done his job then it moves on next line of code.

Thank you for help

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.