I have a field dbscore in my database and it it an array of 5(5 level). lets say i have a new score for level 3. I want to update that score in the database for that particular level. the code below is updating the values but i think it's not correct since the dbscore is an array. Can someone help?

$score is my new score.

mysql_query("UPDATE score SET dbscore='$score' WHERE name='$name'");

You need to loop through your array if you want to input each of the values into your database table. I can't tell you exactly how to do it since we don't have all of your code but it could be something like this.

foreach ($yourArray as $key => $score) {
    mysql_query("UPDATE score SET dbscore='$score' WHERE name='$name'");
}

well if i put this loop, it will through the whole arary and update. I want to update a specific index in the array.
that is update dbscore[1]=250. like this, only one update.

You sort of just answered your own question then. If you are storing your array in $score and you know which key holds the data, then all you have to do is....

mysql_query("UPDATE score SET dbscore='". $score[1] ."' WHERE name='$name'");

Here my code.unserialize the array of marks first and serialize it to save it in database. but it's not working!

public function SaveSpeed($id,$current,$level,$marks){
        $query=mysql_query("SELECT Marks FROM subject where Id='$id'"); 
        $rs=mysql_fetch_assoc($query);
        $marksInDB=unserialize($rs["Marks"]);
        $index=$marksInDB[$level-1];

        if($level<$current){
            if($marks>$index){
                $result=serialize($marksInDB[$level-1]);
                mysql_query("UPDATE subject SET Marks='$result' WHERE Id='$id'");
                return 'Marks updated';
            }

        }
}

Uhg... I hate serialize. I would store it as json instead using json_encode/json_decode.

You might want to try echoing out $result to see what the value is. It is probably false indicating some issue. I am in need of sleep right now but I will look again when I wake up, if no one else has by then.

am using ajax to send the request and it alerts me undefined variable marks in marks.php

Server.prototype.marksupdate=function(id,current,level,marks){

        $.ajax(
        {url:"php/marks.php",
        data:  {id:id,curent:level,level:level,marks:marks},
        type:"POST",
        success:function(data){
            alert(data);

        }
        }
        );


    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.