user2552819
user2552819

Reputation:

updating data in foreach

i want to update data in table using foreach my code is

    foreach($_POST['marks'] as $key => $value) {

  mysql_query("update mark set marks='".$_POST['marks'][$key]."' where cid='1'");
}



<form method="post">
        <input type="text" name="marks[]"/><br />
        <input type="text" name="marks[]" /><br />
        <input type="submit" name="submit"/>
    </form>

but its coming what iam entering value in last field is taking

   id      cid     marks 
    1       1       20
    2       1       20

i want o/p like this

  id   cid   marks
   1    1     20  
   2    1     40

please help me.Thanks in Advance

Upvotes: 0

Views: 37

Answers (2)

US-1234
US-1234

Reputation: 1529

Your table cid value is "1" for both row, so it updates both row, Use "id" column as condition,

  mysql_query("update mark set marks='".$_POST['marks'][$key]."' where id='1' and cid = '1'");

Upvotes: 1

Vivek Singh
Vivek Singh

Reputation: 2447

if you want to update table using cid than it will update all the record having cid=1 with the last fired update query use any unique value to avoid this confliction.

      foreach($_POST['marks'] as $key => $value) 
     {
        mysql_query("update mark set marks='".$value."' 
where cid='1' and `uniqueid`='".$uniqueid."'");
     }

Upvotes: 0

Related Questions