phpmysqlapachehtdocs

Check duplication record from one table and duplication record insert to another table using php script


I have two below tables

1)CandidateDetails

EmployeeId  FirstName       LastName    Mobile          Email     BatchId
1           fn1             ln1         123456789       Email1      75
2           fn2             ln2         123456790       Email2      75
3           fn3             ln3         123456791       Email3      75
4           fn4             ln4         123456792       Email4      75
                

2)DuplicateCandidate

EmployeeId  FirstName       LastName    Mobile          Email     BatchId

My requirement is if EmployeeId is already available in CandidateDetails table then duplicate record to insert into DuplicateCandidate

Below is PHP script only for inserting array to CandidateDetails table, but i am not able to check the condition for if employee id is duplication then that record to insert into another DuplicateCandidate table.

<?php
header("Access-Control-Allow-Origin: http://localhost:4200");
header("Access-Control-Allow-Credentials: true ");
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
header("Access-Control-Allow-Headers: X-Custom-Header, Origin, Content-Type , Authorisation , X-Requested-With");
header("Content-Type: application/json; charset=UTF-8 ");

$json = file_get_contents('php://input');
$decoded = json_decode($json, true);

print_r($decoded);
 $id=$_GET['id'];

function conn() {
  $dbhost = "xxxx";
  $user = "xxx";
  $pass = "xxx";
  $db = "xxxx";
  $conn = new PDO('mysql:host=xxx;dbname=xxx', $user, $pass);
  return $conn;
}
$db = conn();
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);                  
$p = $db->prepare("INSERT INTO CandidateDetails(EmployeeId, FirstName, LastName, Mobile,Email,BatchId)VALUES(:EmployeeId,:FirstName,:LastName,:Mobile,:Email,'$id')");
                   
foreach ($decoded as $item) {
  $p->execute($item);
}

echo json_encode(true);

?>

Solution

  • <?php
    header("Access-Control-Allow-Origin: http://localhost:4200");
    header("Access-Control-Allow-Credentials: true ");
    header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
    header("Access-Control-Allow-Headers: X-Custom-Header, Origin, Content-Type , Authorisation , X-Requested-With");
    header("Content-Type: application/json; charset=UTF-8 ");
    
    $json = file_get_contents('php://input');
    $decoded = json_decode($json, true);
    
    print_r($decoded);
    $id = $_GET['id'];
    
    function conn()
    {
        $dbhost = "x.x.x.x";
        $user = "x";
        $pass = "x";
        $db = "x";
        $conn = new PDO('mysql:host=x;dbname=x', $user, $pass);
        return $conn;
    }
    $db = conn();
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    foreach ($decoded as $item)
    {
        echo $item['EmployeeId'];
    
        $sql = "SELECT * FROM CandidateDetails WHERE EmployeeId = " . $item['EmployeeId'];
        //$result = $conn-                         >query($sql);
        echo $item['EmployeeId'];
        echo $sql;
        $result = $db->query($sql);
        //echo $result;
        //echo $result->num_rows;
        $count = $result->rowCount();
        echo $count;
        if ($count > 0)
        {
            //DuplicateCandidate
            echo "duplicte results";
            //$p = $db->prepare("INSERT INTO CandidateDetailsLog(EmployeeId, FirstName, LastName, Mobile,Email,BatchId)VALUES(:EmployeeId,:FirstName,:LastName,:Mobile,:Email,'$id')");
            $p = $db->prepare("INSERT INTO CandidateDetailsLog(EmployeeId, FirstName, LastName, Mobile,Email,BatchId)
                                          VALUES ('" . $item['EmployeeId'] . "', '" . $item['FirstName'] . "',
                                                          '" . $item['LastName'] . "', '" . $item['Mobile'] . "', '" . $item['Email'] . "',
                                                           '" . $id . "')");
    
            $p->execute();
        }
        else
        {
            echo "0 results";
            //$p = $db->prepare("INSERT INTO CandidateDetails(EmployeeId, FirstName, LastName, Mobile,Email,BatchId)VALUES(:EmployeeId,:FirstName,:LastName,:Mobile,:Email,'$id')");
            $p = $db->prepare("INSERT INTO CandidateDetails(EmployeeId, FirstName, LastName, Mobile,Email,BatchId)
                                          VALUES ('" . $item['EmployeeId'] . "', '" . $item['FirstName'] . "',
                                                          '" . $item['LastName'] . "', '" . $item['Mobile'] . "', '" . $item['Email'] . "',
                                                           '" . $id . "')");
    
            $p->execute();
        }
    
    }
    
    $o = $db->prepare("UPDATE BatchDetailsInfo SET BatchStatus='B2' WHERE BatchId='$id'");
    $o->execute();
    
    echo json_encode(true);
    
    ?>