0

I want to update the value of a column from a mysql table with the result from a mysql_fetch_array

But the last result is just the one being inserted/updated into the column

Where am i having wrong? Here's my code. Thanks in advance

    $studentname="some value";
    $course="some value";
    $query=mysql_query("select SABJEK,GRADE,REMARKS from table where STUDENTNAME='$studentname' && STUDENTNUMBER IS NULL") or die(mysql_error());
    while($result=mysql_fetch_array($query))
        {
        $sabjek=$result['SABJEK'];
        $grade=$result['GRADE'];
        $remarks=$result['REMARKS'];
        $msg="$sabjek = $grade - $remarks ";
        $msg1="$studentname $course $msg";
        }
    mysql_query("update table2 set `msg`='$msg1' where studentname='$studentname'") or die(mysql_error());
2
  • what are you expecting for $msg string? Commented Nov 2, 2014 at 8:29
  • @Asik dont know man that's why i ask it here you have idea? Commented Nov 2, 2014 at 8:47

2 Answers 2

1

Put the mysql_query inside the while. Basically replace

  }
mysql_query("update table2 set `msg`='$msg1' where studentname='$studentname'") or die(mysql_error());

with

    mysql_query("update table2 set `msg`='$msg1' where studentname='$studentname'") or die(mysql_error());
}

and it will work :)

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

1 Comment

did not work @motanelu the last result is just the one that was saved any idea? thanks
0
  If you want to update the column name every-time whenvere you fetch the result just put update query inside the while loop,   
   $studentname="some value";
   $course="some value";
   $query=mysql_query("select SABJEK,GRADE,REMARKS from table where STUDENTNAME='$studentname' && 
   STUDENTNUMBER IS NULL") or die(mysql_error());
   while($result=mysql_fetch_array($query));
     { $sabjek=$result['SABJEK'];
       $grade=$result['GRADE'];
       $remarks=$result['REMARKS'];
       $msg="$sabjek = $grade - $remarks ";
       $msg1="$studentname $course $msg";        
       mysql_query("update table2 set `msg`='$msg1' where studentname='$studentname'") or         
       die(mysql_error());
     }

if you are not expecting this result then please explain me in detail so, i will update the answer.

Thanks.

11 Comments

there where 2 results from mysql_fetch_array result 1 and result 2 but result 2 is just the one inserted in the column why is it? i have placed update inside while thanks
i want both result 1 and reult 2 to be inserted in the column thanks any idea @a.saleem
Fine, both results in the same column? if yes, could you please tell me what is the column data type?
the column type of msg is text sir
Fine, you want to update the column please put the datatype as varchar,or let it be, consider it as example i am defining result1 is grade and result2 is course so , you want to show the values in the same column with separator eg with hyphen (-), $result1='A'; $result2='Maths'; So, Store it with $result3=$result1.'-'.$resultB; then if you want to show the result separetly just explode with (-) to show them as Grade & Course Thanks. Please dont call sir :) Cheers!
|

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.