简体   繁体   中英

How do update data using multidimensional array?

I am tiring to update data into table_course

<?php

// update the ite_aps data into sgc table

require_once('../../includes/common.php');

$m = array();
$result = mysql_query("SELECT * from sgc where year = '2008'") or die(mysql_error());

while($row = mysql_fetch_array($result))
  {
      //echo $row['nric'];
      $res =mysql_query("select COURSE_TITLE_LONG, CRSE_GRADE_OFF, UNT_EARNED, nric from ite where nric = '".$row['nric']."' AND STRM = '2008'")or die(mysql_error()); 
      $norows=mysql_num_rows($res);
      echo $norows."<br>";
      $update = 'UPDATE sgc SET meta_ite = ';
      while($rows = mysql_fetch_array($res))
      {
          for($i=0;$i<$norows;$i++){
          $data = array("course" => "'".$rows['COURSE_TITLE_LONG']."'" , "Grade" => "'".$rows['CRSE_GRADE_OFF']. "'" , "Unit" => "'".$rows['UNT_EARNED']."'");
          $m = serialize($data);
          }
          print $m."<br>";
          $update .= '"'.mysql_real_escape_string($m).'"'; 

      }
      $update .= 'WHERE nric = "'.$row['nric'].'" AND year = "2008"' ;
      print $update."<br>";
     // mysql_query($update);
    }

?>

I got below output for above coding

a:3:{s:6:"course";s:24:"'Personal Effectiveness'";s:5:"Grade";s:3:"'S'";s:4:"Unit";s:3:"'3'";}
a:3:{s:6:"course";s:16:"'Basic Numeracy'";s:5:"Grade";s:3:"'U'";s:4:"Unit";s:3:"'2'";}
a:3:{s:6:"course";s:23:"'Piping & Valve System'";s:5:"Grade";s:3:"'D'";s:4:"Unit";s:3:"'6'";}
a:3:{s:6:"course";s:24:"'Mechanical Fabrication'";s:5:"Grade";s:3:"'F'";s:4:"Unit";s:3:"'6'";}

the output format is: array(course,grade,unit)array(course,grade,unit) - in serialize

But, i want the output in this format: array(array(course,grade,unit),array(course,grade,unit)........) -in serialize

for example: a:5:{a:3:{s:6:"course";s:24:"'Personal Effectiveness'";s:5:"Grade";s:3:"'S'";s:4:"Unit";s:3:"'3'";}' a:3:{s:6:"course";s:16:"'Basic Numeracy'";s:5:"Grade";s:3:"'U'";s:4:"Unit";s:3:"'2'";}.......}

Please any one help me, because i am new for php mysql.

Thanks.

You're approaching the problem completely wrong. You don't need to serialize anything. I'd suggest reading on php.net what serialize() does and where it's useful. In order to update your database - you can issue only 1 query. Since it seems this is a school project, I won't try to solve it for you - the above tips should prove helpful for researching and getting to the conclusion on your own :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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