简体   繁体   English

从mysql_fetch_array结果PHP更新列值

[英]Update Column Value From mysql_fetch_array result PHP

I want to update the value of a column from a mysql table with the result from a mysql_fetch_array 我想用mysql_fetch_array的结果更新mysql表中的列的值

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());

Put the mysql_query inside the while. 将mysql_query放在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 :) 它会工作:)

  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. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM