未定义的索引错误,文件中的多个数据库连接

I have a php file/script running in powershell which is meant to connect to one db server, select info, connect to another db/server and insert that info.

I was having problems with the connections, but now I have it working, except when I run this script in powershell I get a slew of errors, 5 to be exact (which matches the records in the database currently) about undefined index.

This affects line 51 through 55 which happen to be the end of my code, the VALUES section, starting with the ON DUPLICATE KEY line. My table in mysql workbench has the exact same column names, index values, etc. that my test table has where I execute this script for testing in workbench. Am I calling those values incorrectly in my insert statement?

    <?php

    $servername = "//";
    $username = "//";
    $password = "//";

    $servername2 = "//";
    $username2 = "//";
    $password2 = "//";

    // Create connection
    $conn = new mysqli($servername, $username, $password);
    $conn2 = new mysqli($servername2, $username2, $password2);

    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    echo "Connected successfully";

    // Check connection2
    if ($conn2->connect_error) {
        die("Connection failed: " . $conn2->connect_error);
    }
    echo "Connected successfully";

    $data = mysqli_query($conn, " SELECT c.extension
                          ,RESPONSIBLEUSEREXTENSIONID
                          , sum(Duration) AS Total_Talk_Time_seconds
                          , round(sum(Duration) / 60,2) AS Total_Talk_Time_minutes
                          , sum(if(LEGTYPE1 = 1,1,0)) AS Total_Outbound
                          , sum(if(LEGTYPE1 = 2,1,0)) AS Total_Inbound
                          , sum(if(Answered = 1,0,1)) AS Total_Missed
                          , count(DISTINCT b.NOTABLECALLID) AS Total_Calls
                          , NOW() AS time
                          , curdate() AS date
                      FROM cdrdb.session a
                      LEFT JOIN cdrdb.callsummary b
                           ON a.NOTABLECALLID = b.NOTABLECALLID
                      LEFT join cdrdb.mxuser c
                           ON a.RESPONSIBLEUSEREXTENSIONID = c.EXTENSIONID
                      WHERE b.ts >= curdate()
                      AND c.extension IN (7295,7306,7218,7247,7330,7000,7358)
                      group by c.extension");


            foreach ($data as $d) {

                    $stmt = mysqli_prepare($conn2, "Insert into jfi.ambitionphone(Extension, ExtID, Total_Talk_Time_seconds,
                          Total_Talk_Time_minutes,Total_Outbound, Total_Inbound,
                          Missed_Calls, Total_Calls, Time_of_report,Date_of_report  )
                            VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
                            ON duplicate key update
                            Total_Talk_Time_seconds = values(Total_Talk_Time_seconds),
                            Total_Talk_Time_minutes = values(Total_Talk_Time_minutes),
                            Total_Outbound = values(Total_Outbound),
                            Total_Inbound = values(Total_Inbound),
                            Missed_calls = values(Missed_Calls),
                            Total_Calls = values(Total_Calls),
                            Time_of_report = values(Time_of_report),
                            Date_of_report = values(Date_of_report)");
                            mysqli_stmt_bind_param($stmt, "ssiiiiiiss", $d['extension'], $d['RESPONSIBLEUSEREXTENSIONID'],
                            $d['Total_Talk_Time_seconds'], $d['Total_Talk_Time_minutes'],
                            $d['Total_Outbound'], $d['Total_Inbound'], $d['Total_Missed'], $d['Total_Calls'],
                            $d['time'], $d['date']);

            mysqli_stmt_execute($stmt);

            }

     ?>

UPDATE:

This is the mysql query I'm converting to this PHP script, for context:

Insert into test.ambition_test(Extension, ExtID, Total_Talk_Time_seconds,
Total_Talk_Time_minutes,Total_Outbound, Total_Inbound, 
Missed_Calls, Total_Calls, Time_of_report,Date_of_report  )  
SELECT 
 c.extension 
,RESPONSIBLEUSEREXTENSIONID 
, sum(Duration) 
, round(sum(Duration) / 60,2) 
, sum(if(LEGTYPE1 = 1,1,0)) 
, sum(if(LEGTYPE1 = 2,1,0)) 
, sum(if(Answered = 1,0,1)) 
-- , count(DISTINCT b.NOTABLECALLID) as Total_Calls
, sum(if(LEGTYPE1 = 1,1,0)) + sum(if(LEGTYPE1 = 2,1,0))   as total_calls 
, NOW()
, curdate() 
FROM cdrdb.session a
LEFT JOIN cdrdb.callsummary b
 ON a.NOTABLECALLID = b.NOTABLECALLID
LEFT join cdrdb.mxuser c
 ON a.RESPONSIBLEUSEREXTENSIONID = c.EXTENSIONID
WHERE b.ts >= curdate()
AND c.extension IN (7295,7306,7218,7247,7330,7000,7358)
group by c.extension
ON duplicate key update Total_Talk_Time_seconds 
=values(Total_Talk_Time_seconds), 
Total_Talk_Time_minutes =values(Total_Talk_Time_minutes), 
Total_Outbound = values(Total_Outbound), 
Total_Inbound = values(Total_Inbound), 
Missed_calls = values(Missed_calls), 
Total_Calls = values(Total_Calls), 
Time_of_report = NOW(); 

The problem is that you didn't assign aliases to the function calls in the first query. So there's no columns named Total_Outbound, Total_Inbound, etc. It should be:

$data = mysqli_query($conn, " SELECT c.extension
                              ,RESPONSIBLEUSEREXTENSIONID
                              , sum(Duration) AS Total_Talk_Time_seconds
                              , round(sum(Duration) / 60,2) AS Total_Talk_Time_minutes
                              , sum(if(LEGTYPE1 = 1,1,0)) AS Total_Outbound
                              , sum(if(LEGTYPE1 = 2,1,0)) AS Total_Inbound
                              , sum(if(Answered = 1,0,1)) AS Total_Missed
                              , count(DISTINCT b.NOTABLECALLID) AS Total_Calls
                              , NOW() AS time
                              , curdate() AS date
                          FROM cdrdb.session a
                          LEFT JOIN cdrdb.callsummary b
                               ON a.NOTABLECALLID = b.NOTABLECALLID
                          LEFT join cdrdb.mxuser c
                               ON a.RESPONSIBLEUSEREXTENSIONID = c.EXTENSIONID
                          WHERE b.ts >= curdate()
                          AND c.extension IN (7295,7306,7218,7247,7330,7000,7358)
                          group by c.extension");

Then you also need to fix the syntax of your INSERT query. It doesn't specify the values to insert, so how is it supposed to tell if you're creating a duplicate key? And you should use a prepared statement to avoid quoting issues.

$stmt = mysqli_prepare($conn2, "Insert into jfi.ambitionphone(Extension, ExtID, Total_Talk_Time_seconds,
                      Total_Talk_Time_minutes,Total_Outbound, Total_Inbound,
                      Missed_Calls, Total_Calls, Time_of_report,Date_of_report  )
                        VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
                        ON duplicate key update 
                        Total_Talk_Time_seconds = values(Total_Talk_Time_seconds), 
                        Total_Talk_Time_minutes = values(Total_Talk_Time_minutes), 
                        Total_Outbound = values(Total_Outbound), 
                        Total_Inbound = values(Total_Inbound), 
                        Missed_calls = values(Missed_Calls), 
                        Total_Calls = values(Total_Calls), 
                        Time_of_report = values(Time_of_report), 
                        Date_of_report = values(Date_of_report)");

foreach ($data as $d) {
    mysqli_stmt_bind_param($stmt, "ssiiiiiiss", $d['extension'], $d['RESPONSIBLEUSEREXTENSIONID'], 
        $d['Total_Talk_Time_seconds'], $d['Total_Talk_Time_minutes'], 
        $d['Total_Outbound'], $d['Total_Inbound'], $d['Total_Missed'], $d['Total_Calls'],
        $d['time'], $d['date']);
    mysqli_stmt_execute($stmt);
}