update one field from one table to another table within same database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • puneetmca
    New Member
    • Jan 2010
    • 15

    update one field from one table to another table within same database

    hi all
    i want to update ecode from emp_company to at0310.update ecode that matches with ecardno of both the tables....i used query for it but update only same ecode for all............ .
    [code=php]
    <head>
    <title></title>
    </head>
    <body>
    <?php
    $con = mysql_connect(" localhost","roo t","root");
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }
    mysql_select_db ("master", $con);
    $query=mysql_qu ery("SELECT emp_company.eco de FROM emp_company INNER JOIN at0310
    ON emp_company.eca rdno = at0310.ecardno" )or die(mysql_error ());
    while($row=mysq l_fetch_array($ query))
    {
    $ecode=$row['ecode'];
    mysql_query("UP DATE at0310 SET ecode='$ecode'" )or die(mysql_error ());
    }

    if (mysql_query($q uery, $con))
    {
    echo "record updated !";
    }
    else
    {
    echo"something went wrong";
    }
    mysql_close($co n)
    ?>
    </BODY>
    </HTML>

    [/code]
    this is coding of at0310 that is from text file download from attendance machine.
    [code=php]
    <html>
    <?php
    $con=mysql_conn ect("localhost" ,"root","root") ;
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }
    mysql_select_db ('master',$con) ;

    $file = "ATND.txt";
    $fp = fopen($file, "r");
    $data = fread($fp, filesize($file) );
    fclose($fp);
    $output = str_replace("\t "," ", $data);
    $output = explode("\n", $output);
    foreach($output as $var)
    {
    $tmp = explode(" ", $var);
    $ecardno = $tmp[0];
    $cluster = $tmp[1];
    $atdate = substr($tmp[1],0,6);
    $attime = substr($tmp[1],6,4);
    $IO = substr($tmp[1],10,1);
    $serialno = substr($tmp[1],11,5);
    $cardrd = substr($tmp[1],16);
    $sql = "INSERT INTO at0310 SET ecardno='$ecard no', atdate='$atdate ',attime='$atti me',IO='$IO',se rialno='$serial no',cardrd='$ca rdrd'";


    mysql_query($sq l);

    }
    include("instxt ecode.php");
    echo "Done!";
    ?>
    [/code]
Working...