how do I copy value from one table and inserted to another in the same database??
- by mathew
I am having a tough time to do this...
I have created two table say table-1 and table-2 in same database.what I want is I need to copy some values from table-1 and insert the same to table-2. I have tried many ways but it does not seems work. below is my code can any one tell me where I am missing??
$db = mysql_connect("localhost", "user", "pass") or die("Could not connect.");
mysql_select_db("comdata",$db)or die(mysql_error());
$resultb = mysql_query("SELECT * FROM table-2")or die(mysql_error());
$row = mysql_fetch_array($resultb);
$days = (strtotime(date("Y-m-d")) - strtotime($row['regtime'])) / (60 * 60 * 24);
if($row > 0 && $days < 1){
$person = $row['person'];
$catogr = $row['catog'];
$position = $row['position'];
$location = $row['location'];
$rank = $row['rank'];
mysql_close($db);
}else{
$db = mysql_connect("localhost", "user", "pass") or die("Could not connect.");
mysql_select_db("comdata",$db)or die(mysql_error());
$result = mysql_query("SELECT * FROM table-1 WHERE regtime = DATE(NOW()) ORDER BY rank ASC LIMIT 1;")or die(mysql_error());
$row = mysql_fetch_array($result);
$person = $row['person'];
$catogr = $row['catog'];
$position = $row['position'];
$location = $row['location'];
$rank = $row['rank'];
mysql_query("INSERT INTO table-2
(regtime,person,catog,position,location,rank) VALUES(NOW(),'$person','$catogr','$position','$location','$rank')");
mysql_close($db);
}