我有两张桌子。两者结构相同。只有一个具有数据转储(table1),另一个只有唯一数据(table2)。我想使用循环和更新table2。如果找到新数据,则在table2中插入新行,否则更新table2中的现有行。 以下是我的代码: $ data_pts:包含table1数据的数组。
while($row = mysqli_fetch_array($rs_select_ptsdata_replica))
{
$temp = 0;
for($i=0;$i<sizeof($data_pts); $i++)
{
if( $row['app_SlNo'] == $data_pts[$i][1])
{
if( $row['pts_ProjectNum'] == $data_pts[$i][3])
{
if( $row['pts_ReleaseDate'] == $data_pts[$i][6])
{
/*If any of the below condition doesnt match then the loop should execute*/
if( ($data_pts[$i][9] != $row['pts_DTVResources']) ||
($data_pts[$i][10] != $row['pts_IBMPrep']) ||
($data_pts[$i][11] != $row['pts_IBMExec']) ||
($data_pts[$i][12] != $row['pts_DTVContractors']) ||
($data_pts[$i][13] != $row['pts_IBMTnM']) ||
($data_pts[$i][14] != $row['pts_IBMAS']))
{
$temp ++; // if any hour doesn't match the table1 data compared to table2 data.
break;
}
else
{
echo "hours not same<br/>";
}
}
}
}
else
{
$temp = 0; // Every hours is same as in table1 and table2
break;
}
}
if($temp > 0)
{
$sql_update_pts = "update ".$db.".tbl_ptsdata set
pts_projectname ='".$row[7]."',
pts_commit_status = '".$row[8]."',
pts_dtvresources = '".$row[9]."',
pts_ibmprep = '".$row[10]."',
pts_ibmexec = '".$row[11]."',
pts_dtvcontractors = '".$row[12]."',
pts_ibmtnm = '".$row[13]."',
pts_ibmas = '".$row[14]."'
where app_slno = '".$row[1]."' and
pts_applicationname = '".$row[2]."' and
pts_projectnum = '".$row[3]."' and
pts_chargeto = '".$row[4]."' and
pts_crnum = '".$row[5]."' and
pts_releasedate = '".$row[6]."'";
$rs_insert_pts = $mysqli->query($sql_update_pts);
}
else if($temp == 0) /*If table1 data is not present in table2,insert in table2*/
{
$sql_insert_pts = "insert into ".$db.".tbl_ptsdata values('',
'".$row[1]."','".$row[2]."','".$row[3]."',
'".$row[4]."','".$row[5]."',
'".date('Y-m-d',strtotime($row[6]))."',
'".$row[7]."','".$row[8]."','".$row[9]."',
'".$row[10]."','".$row[11]."',
'".$row[12]."','".$row[13]."','".$row[14]."')";
$rs_insert_pts = $mysqli->query($sql_insert_pts);
}
else if($temp == 2)
{
continue;
}
}