1

I have a field called 'score' in a database table that an ajax call is adding values to.

The query below causes it to add values of 1 and 2 under 'score'. It should be looking for "$usrscore1" and "$usrscore2" in my ajax call but it seems to just see the integers (1 and 2) in $us and incorrectly post them as the values under 'score'.

for ( $counter4 = 1; $counter4 <= 2; $counter4 += 1) {

    $mess4 = $messages + $counter4;
    $us = $usrscore + $counter4;

    mysql_query("INSERT INTO messages" . $counter4 . " (`score`)
            VALUES ($us)",$dbconn);
}

If I change:

$us = $usrscore + $counter4;

to

$us = $usrscore1;

it correctly looks for $usrscore1 in my ajax call and puts the correct value in the database field under 'score'. But I need it to work in the for loop so just writing it out as $usrscore1 won't work, even though it reads it correctly that way.

Just in case it helps to see it, here's what the ajax call looks like:

$('#rateplugin' + instance).raty({
                half: true,
                start: totalavgRounded,
                click: function(score){

                        $.ajax({
                            type: 'POST',
                            url: 'backend.php',
                            data: "usrscore"+instance+"="+score+"&action=postmsg",
                            success: function(xml) {
                                $.fn.raty.readOnly(true,'#rateplugin' + instance);
                            }
                        });
                    return false;

                } // end click
            }); // rat

Update:

I may have not been very clear. I set the loop up to only make 2 database tables, but I did that really just for testing purposes. In reality, once this is live, there could be thousands of these to create which is why I'm doing it with a loop. Would the URL variable still be a good solution here? I'm not exactly sure how I would implement it.

My main point of absolute confusion here is that that no matter how I broke up the variable integer from $usrscore it would always ignore the $usrscore part and only see the integer and then just use that as the final value which will always be wrong. Is there something weird about the VALUE MySQL bit that does this? Does anyone have any ideas as to why it is doing that and how to get around it? If that URL variable idea might work, could you please give me some direction into its application here?

1
  • I think you want $usrscore + $counter4 to result in the vatiable named $usrscore1. This is not what this currently does. It is numerically adding the content of $usrscore to the content of $counter4. Because strings will value at 0, the result is 1. You might want to look into how to generate the proper variable-name using string concatenation. Something like ${'usrscore'.$counter4}. However using this kind of construcion is often a sign of bad design. Most probably you'll need to look at using arrays. Commented Apr 2, 2011 at 20:50

1 Answer 1

1

from what i can see, you only have limited $usrscore1 and $usrscore2. i suggest you put a single variable $usrscore and populate it based on the parameter in the url.

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

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.