Jump to content

need to update record from one table to another....


mcastu

Recommended Posts

hi...

i m new in this forum....

i have  a table(at0310)  having fields:

ecardno,atdate,attime,serialno,cardrd and ecode

my all fields having values dat comes from  a text file dat i already done .......... but ecode is empty and i want this ecode comes from another table from same database i.e emp_company.

In emp_company i have field ecardno and ecode ..

so i want at0310 table dat ecardno matches with emp_company's ecardno and return the ecodeno correspondence it and update dat ecode into at0310 table...........

in my code the problem is that ecode is updated but only one ecode is update everytime for each record...

<html>
<?php
$con=mysql_connect("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='$ecardno', atdate='$atdate',attime='$attime',IO='$IO',serialno='$serialno',cardrd='$cardrd'";
//"INSERT INTO at0310 SET ecardno='$ecardno', atdate='$atdate',attime='$attime',IO='$IO',serialno='$serialno',cardrd='$cardrd' ";
//$sql ="INSERT INTO at0310 ('$ecardno','$atdate','$attime','$IO','$serialno','$cardrd','$ecode')
//SELECT ecode FROM emp_company GROUP BY ('$ecode')";

mysql_query($sql);

}
include("instxtecode.php");
echo "Done!";
?>

<head>
<title></title>
</head>
<body>
<?php
$con = mysql_connect("localhost","root","root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("master", $con);
$query=mysql_query("SELECT emp_company.ecode FROM emp_company INNER JOIN at0310
ON emp_company.ecardno = at0310.ecardno")or die(mysql_error());
while($row=mysql_fetch_array($query))
{
$ecode=$row['ecode'];
mysql_query("UPDATE at0310 SET ecode='$ecode'")or die(mysql_error());
}

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



 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.