0

I am trying to send Json data to sql using the below php code but nothing goes into sql and I can't figure out why. The Json is coming as POST from another server with the below information. Any help I would be great full for I have an example of the Json file if needed.

<?php
$con = mysql_connect("localhost","@username","@password") or die('Could not connect: ' . mysql_error());
mysql_select_db("job-complete", $con);

//read the json file contents
$jsondata = file_get_contents('php://input');
//convert json object to php associative array
$data = json_decode($jsondata, true);
//pull all job assignments 

//set all data 
$jobnumber = $data['jobNumber'];
$id = $data['businessUnit']['id'];
$bname = $data['businessUnit']['name'];
$campaign = $data['campagin']['name'];
$cname = $data['customer']['name'];
$castreet = $data['customer']['address']['street'];
$cacountry = $data['customer']['address']['country'];
$cacity = $data['customer']['address']['city'];
$castate = $data['customer']['address']['state'];
$cazip = $data['customer']['address']['zip'];
$castreetadd = $data['customer']['address']['streetAddress'];
$summary = $data['summary'];
$createdon = $data['createdOn'];
$scheduledon = $data['scheduledOn'];
$completedon = $data['completedOn'];
$start = $data['start'];
$end = $end['end'];
$duration = $data['duration'];
$charge = $data['noCharge'];
$ijobid = $data['invoice']['jobId'];
$jobassignresults = array();
$jatech = array(); //initiate variable
foreach($data['jobAssignments'] as $chunk){
$jatech = $chunk['technician'];
$jatechid = $jatech['id'];
$jatechname = $jatech['name'];
$jatechfinished = array($jatechid, $jatechname);
$jobassignreults[] = $jatechfinished;
}
foreach($jatech as $key => $techs){
$sql = "INSERT INTO techtable ('ijobid','jtid','jtname') VALUES ($ijobid, $jatechid, $jatechname)";
}
$jasplit = $data['jobAssignments']['split'];
$jadriving = $data['jobAssignments']['totalDrivingHours'];
$jaworking = $data['jobAssignments']['totalWorkingHours'];
$jaassigned = $data['jobAssignments']['assignedOn'];
$jatechstatus = $data['jobAssignments']['status'];
$iid = $data['invoice']['id'];
$ictive = $data['invoice']['active'];
$ijobnumber = $data['invoice']['jobNumber'];
$isvalue = $data['invoice']['status']['value'];
$isname = $data['invoice']['status']['name'];
$inumber = $data['invoice']['numnber'];
$itax = $data['invoice']['tax'];
$isubtotal = $data['invoice']['subtotal'];
$itotal = $data['invoice']['total'];
$ibalance = $data['invoice']['balance'];
$iitemsid = $data['invoice']['items']['id'];
$iiteminvoiceid = $data['invoice']['items']['invoiceId'];
$iitemactive = $data['invocie']['items']['active'];
$iitemskuid = $data['invoice']['items']['sku']['id'];
$iitemskuname = $data['invoice']['items']['sku']['name'];
$iitemskudisplayname = $data['invoice']['items']['sku']['displayName'];
$iitemskutype = $data['invoice']['items']['sku']['type'];
$iitemsalestype = $data['invoice']['items']['salesType'];
$iitemdescription = $data['invoice']['items']['description'];
$iitemsqty = $data['invoice']['items']['qty'];
$iitemsunitrate = $data['invoice']['items']['unitRate'];
$iitemtotal = $data['invoice']['items']['total'];
$iitemtotalcost = $data['invoice']['items']['totalCost'];
$ipaymentsid = $data['invoice']['payments']['id'];
$ipaymentsinvoiceid = $data['invoice']['payments']['invoiceId'];
$iptypeid = $data['invoice']['payments']['type']['id'];
$iptypename = $data['invoice']['payments']['type']['name'];
$invoiceamount = $data['invoice']['amount'];
$invoicetotalamount = $data['invoice']['totalAmount'];

//insert into mysql table
$sql = "INSERT INTO jobdone (jnumb, bid, bname, campaign, cname, castreet, cacountry, cacity, castate, cazip, castreetadd, summary, createdon, scheduledon, completedon, start, end, duration, charge, jasplit, jadriving, jaworking, jaassigned, jatechstatus, iid, ictive, ijobid, ijobnumber, isvalue, isname, inumber, itax, isubtotal, itotal, ibalance, iitemsid, iiteminvoiceid, iitemactive, iitemskuid, iitemskuname, iitemskudisplayname, iitemskutype, iitemsalestype, iitemdescription, iitemsqty, iitemsunitrate, iitemtotal, iitemtotalcost, ipaymentsid, ipaymentsinvoiceid, iptypename, invoiceamount, invoicetotalamount) VALUE ($jnumb, $bid, $bname, $campaign, $cname, $castreet, $cacountry, $cacity, $castate, $cazip, $castreetadd, $summary, $createdon, $scheduledon, $completedon, $start, $end, $duration, $charge, $jasplit, $jadriving, $jaworking, $jaassigned, $jatechstatus, $iid, $ictive, $ijobid, $ijobnumber, $isvalue, $isname, $inumber, $itax, $isubtotal, $itotal, $ibalance, $iitemsid, $iiteminvoiceid, $iitemactive, $iitemskuid, $iitemskuname, $iitemskudisplayname, $iitemskutype, $iitemsalestype, $iitemdescription, $iitemsqty, $iitemsunitrate, $iitemtotal, $iitemtotalcost, $ipaymentsid, $ipaymentsinvoiceid, $iptypename, $invoiceamount, $invoicetotalamount)";
if(!mysql_query($sql,$con))
{
die('Error : ' . mysql_error());
}



?>

Edited File:

<?php
    $servername = "localhost";
    $username = "@username";
    $password = "@password";
    $dbname = "job-complete";

    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 

    //read the json file contents
$jsondata = file_get_contents('php://input');
     //convert json object to php associative array
    $data = json_decode($jsondata, true);
    //pull all job assignments 

    //set all data 
    $jobnumber = $data['jobNumber'];
    $id = $data['businessUnit']['id'];
    $bname = $data['businessUnit']['name'];
    $campaign = $data['campagin']['name'];
    $cname = $data['customer']['name'];
    $castreet = $data['customer']['address']['street'];
    $cacountry = $data['customer']['address']['country'];
    $cacity = $data['customer']['address']['city'];
    $castate = $data['customer']['address']['state'];
    $cazip = $data['customer']['address']['zip'];
    $castreetadd = $data['customer']['address']['streetAddress'];
    $summary = $data['summary'];
    $createdon = $data['createdOn'];
    $scheduledon = $data['scheduledOn'];
    $completedon = $data['completedOn'];
    $start = $data['start'];
    $end = $data['end'];
    $duration = $data['duration'];
    $charge = $data['noCharge'];
    $ijobid = $data['invoice']['jobId'];
    $jobassignresults = array();
    $jatech = array(); //initiate variable
    foreach($data['jobAssignments'] as $chunk){
        $jatech = $chunk['technician'];
        $jatechid = $jatech['id'];
        $jatechname = $jatech['name'];
        $jatechfinished = array($jatechid, $jatechname);
        $jobassignreults[] = $jatechfinished;
    }
    foreach($jatech as $key => $techs){
        $sql = "INSERT INTO `techtable` (`ijobid`, `jtid`, `jtname`) VALUES ('$ijobid', '$jatechid', '$jatechname')";
        if (mysqli_query($conn, $sql)) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

}
    $jasplit = $data['jobAssignments']['split'];
    $jadriving = $data['jobAssignments']['totalDrivingHours'];
    $jaworking = $data['jobAssignments']['totalWorkingHours'];
    $jaassigned = $data['jobAssignments']['assignedOn'];
    $jatechstatus = $data['jobAssignments']['status'];
    $iid = $data['invoice']['id'];
    $ictive = $data['invoice']['active'];
    $ijobnumber = $data['invoice']['jobNumber'];
    $isvalue = $data['invoice']['status']['value'];
    $isname = $data['invoice']['status']['name'];
    $inumber = $data['invoice']['numnber'];
    $itax = $data['invoice']['tax'];
    $isubtotal = $data['invoice']['subtotal'];
    $itotal = $data['invoice']['total'];
    $ibalance = $data['invoice']['balance'];
    $iitemsid = $data['invoice']['items']['id'];
    $iiteminvoiceid = $data['invoice']['items']['invoiceId'];
    $iitemactive = $data['invocie']['items']['active'];
    $iitemskuid = $data['invoice']['items']['sku']['id'];
    $iitemskuname = $data['invoice']['items']['sku']['name'];
    $iitemskudisplayname = $data['invoice']['items']['sku']['displayName'];
    $iitemskutype = $data['invoice']['items']['sku']['type'];
    $iitemsalestype = $data['invoice']['items']['salesType'];
    $iitemdescription = $data['invoice']['items']['description'];
    $iitemsqty = $data['invoice']['items']['qty'];
    $iitemsunitrate = $data['invoice']['items']['unitRate'];
    $iitemtotal = $data['invoice']['items']['total'];
    $iitemtotalcost = $data['invoice']['items']['totalCost'];
    $ipaymentsid = $data['invoice']['payments']['id'];
    $ipaymentsinvoiceid = $data['invoice']['payments']['invoiceId'];
    $iptypeid = $data['invoice']['payments']['type']['id'];
    $iptypename = $data['invoice']['payments']['type']['name'];
    $invoiceamount = $data['invoice']['amount'];
    $invoicetotalamount = $data['invoice']['totalAmount'];

     //insert into mysql table
     $sql = "INSERT INTO jobdone (jnumb, bid, bname, campaign, cname, castreet, cacountry, cacity, castate, cazip, castreetadd, summary, createdon, scheduledon, completedon, start, end, duration, charge, jasplit, jadriving, jaworking, jaassigned, jatechstatus, iid, ictive, ijobid, ijobnumber, isvalue, isname, inumber, itax, isubtotal, itotal, ibalance, iitemsid, iiteminvoiceid, iitemactive, iitemskuid, iitemskuname, iitemskudisplayname, iitemskutype, iitemsalestype, iitemdescription, iitemsqty, iitemsunitrate, iitemtotal, iitemtotalcost, ipaymentsid, ipaymentsinvoicedid, iptypename, invoiceamount, invoicetotalamount) VALUES ('$jnumber', '$id', '$bname', '$campaign', '$cname', '$castreet', '$cacountry', '$cacity', '$castate', '$cazip', '$castreetadd', '$summary', '$createdon', '$scheduledon', '$completedon', '$start', '$end', '$duration', '$charge', '$jasplit', '$jadriving', '$jaworking', '$jaassigned', '$jatechstatus', '$iid', '$ictive', '$ijobid', '$ijobnumber', '$isvalue', '$isname', '$inumber', '$itax', '$isubtotal', '$itotal', '$ibalance', '$iitemsid', '$iiteminvoiceid', '$iitemactive', '$iitemskuid', '$iitemskuname', '$iitemskudisplayname', '$iitemskutype', '$iitemsalestype', '$iitemdescription', '$iitemsqty', '$iitemsunitrate', '$iitemtotal', '$iitemtotalcost', '$ipaymentsid', '$ipaymentsinvoiceid', '$iptypename', '$invoiceamount', '$invoicetotalamount')";
     // Check connection
if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
    $conn->close();


?>

Edited above to change the ' to `

Edited php file to correct for arrays:

<?php
    $servername = "localhost";
    $username = "@username";
    $password = "@password";
    $dbname = "job-complete";

    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 

    //read the json file contents
$jsondata = file_get_contents('php://input');
     //convert json object to php associative array
    $data = json_decode($jsondata, true);
    //pull all job assignments 

    //set all data 
    $jobnumber = $data['jobNumber'];
    $id = $data['businessUnit']['id'];
    $bname = $data['businessUnit']['name'];
    $campaign = $data['campaign']['name'];
    $cname = $data['customer']['name'];
    $castreet = $data['customer']['address']['street'];
    $cacountry = $data['customer']['address']['country'];
    $cacity = $data['customer']['address']['city'];
    $castate = $data['customer']['address']['state'];
    $cazip = $data['customer']['address']['zip'];
    $castreetadd = $data['customer']['address']['streetAddress'];
    $summary = $data['summary'];
    $createdon = $data['createdOn'];
    $scheduledon = $data['scheduledOn'];
    $completedon = $data['completedOn'];
    $start = $data['start'];
    $end = $data['end'];
    $duration = $data['duration'];
    $charge = $data['noCharge'];
    $ijobid = $data['invoice']['jobId'];
    foreach($data['jobAssignments'] as $chunk){
        $jatech = $chunk['technician'];
        $jatechid = $jatech['id'];
        $jatechname = $jatech['name'];
        $jasplit = $chunk['split'];
        $jadriving = $chunk['totalDrivingHours'];
        $jaworking = $chunk['totalWorkingHours'];
        $jaassigned = $chunk['assignedOn'];
        $jatechstatus = $chunk['status'];
        $jatechfinished = array($jatechid, $jatechname);
        $jobassignreults[] = $jatechfinished;
    }
    foreach($jatech as $key => $techs){
        $sql = "INSERT INTO `techtable` (`ijobid`,`jtid`,`jtname`) VALUES ('$ijobid', '$jatechid', '$jatechname')";
        if (mysqli_query($conn, $sql)) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

}

    $iid = $data['invoice']['id'];
    $ictive = $data['invoice']['active'];
    $ijobnumber = $data['invoice']['jobNumber'];
    $isvalue = $data['invoice']['status']['value'];
    $isname = $data['invoice']['status']['name'];
    $inumber = $data['invoice']['number'];
    $itax = $data['invoice']['tax'];
    $isubtotal = $data['invoice']['subtotal'];
    $itotal = $data['invoice']['total'];
    $ibalance = $data['invoice']['balance'];
    foreach($data['invoice']['items'] as $chunked){
        $iitemsid = $chunked['id'];
        $iiteminvoiceid = $chunked['invoiceId'];
        $iitemactive = $chunked['active'];
        $iitemskuid = $chunked['sku']['id'];
        $iitemskuname = $$chunked['sku']['name'];
        $iitemskudisplayname = $chunked['sku']['displayName'];
        $iitemskutype = $chunked['sku']['type'];
        $iitemsalestype = $chunked['salesType'];
        $iitemdescription = $chunked['description'];
        $iitemsqty = $chunked['qty'];
        $iitemsunitrate = $chunked['unitRate'];
        $iitemtotal = $chunked['total'];
        $iitemtotalcost = $chunked['totalCost'];
    }
    foreach($data['invoice']['payments'] as $chunking){
        $ipaymentsid = $chunking['id'];
        $ipaymentsinvoiceid = $chunking['invoiceId'];
        $iptypeid = $chunking['type']['id'];
        $iptypename = $chunking['type']['name'];
        $invoiceamount = $chunking['amount'];
        $invoicetotalamount = $chunking['totalAmount'];
    }

     //insert into mysql table
     $sql = "INSERT INTO `jobdone` (`jnumb`, `bid`, `bname`, `campaign`, `cname`, `castreet`, `cacountry`, `cacity`, `castate`, `cazip`, `castreetadd`, `summary`, `createdon`, `scheduledon`, `completedon`, `start`, `end`, `duration`, `charge`, `jasplit`, `jadriving`, `jaworking`, `jaassigned`, `jatechstatus`, `iid`, `ictive`, `ijobid`, `ijobnumber`, `isvalue`, `isname`, `inumber`, `itax`, `isubtotal`, `itotal`, `ibalance`, `iitemsid`, `iiteminvoiceid`, `iitemactive`, `iitemskuid`, `iitemskuname`, `iitemskudisplayname`, `iitemskutype`, `iitemsalestype`, `iitemdescription`, `iitemsqty`, `iitemsunitrate`, `iitemtotal`, `iitemtotalcost`, `ipaymentsid`, `ipaymentsinvoicedid`, `iptypename`, `invoiceamount`, `invoicetotalamount`) VALUES ('$jobnumber', '$id', '$bname', '$campaign', '$cname', '$castreet', '$cacountry', '$cacity', '$castate', '$cazip', '$castreetadd', '$summary', '$createdon', '$scheduledon', '$completedon', '$start', '$end', '$duration', '$charge', '$jasplit', '$jadriving', '$jaworking', '$jaassigned', '$jatechstatus', '$iid', '$ictive', '$ijobid', '$ijobnumber', '$isvalue', '$isname', '$inumber', '$itax', '$isubtotal', '$itotal', '$ibalance', '$iitemsid', '$iiteminvoiceid', '$iitemactive', '$iitemskuid', '$iitemskuname', '$iitemskudisplayname', '$iitemskutype', '$iitemsalestype', '$iitemdescription', '$iitemsqty', '$iitemsunitrate', '$iitemtotal', '$iitemtotalcost', '$ipaymentsid', '$ipaymentsinvoiceid', '$iptypename', '$invoiceamount', '$invoicetotalamount')";
     // Check connection
if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
    $conn->close();


?>

Question: I am getting the following errors like my sql statement cant read what is in the arrays how can i fix this?

[26-Sep-2016 20:36:47 UTC] PHP Notice:  Undefined variable: iitemsid in /home/jtairadmin/public_html/webhooks/job_complete.php on line 97
[26-Sep-2016 20:36:47 UTC] PHP Notice:  Undefined variable: iiteminvoiceid in /home/jtairadmin/public_html/webhooks/job_complete.php on line 97
[26-Sep-2016 20:36:47 UTC] PHP Notice:  Undefined variable: iitemactive in /home/jtairadmin/public_html/webhooks/job_complete.php on line 97
[26-Sep-2016 20:36:47 UTC] PHP Notice:  Undefined variable: iitemskuid in /home/jtairadmin/public_html/webhooks/job_complete.php on line 97
[26-Sep-2016 20:36:47 UTC] PHP Notice:  Undefined variable: iitemskuname in /home/jtairadmin/public_html/webhooks/job_complete.php on line 97
[26-Sep-2016 20:36:47 UTC] PHP Notice:  Undefined variable: iitemskudisplayname in /home/jtairadmin/public_html/webhooks/job_complete.php on line 97
[26-Sep-2016 20:36:47 UTC] PHP Notice:  Undefined variable: iitemskutype in /home/jtairadmin/public_html/webhooks/job_complete.php on line 97
[26-Sep-2016 20:36:47 UTC] PHP Notice:  Undefined variable: iitemsalestype in /home/jtairadmin/public_html/webhooks/job_complete.php on line 97
[26-Sep-2016 20:36:47 UTC] PHP Notice:  Undefined variable: iitemdescription in /home/jtairadmin/public_html/webhooks/job_complete.php on line 97
[26-Sep-2016 20:36:47 UTC] PHP Notice:  Undefined variable: iitemsqty in /home/jtairadmin/public_html/webhooks/job_complete.php on line 97
[26-Sep-2016 20:36:47 UTC] PHP Notice:  Undefined variable: iitemsunitrate in /home/jtairadmin/public_html/webhooks/job_complete.php on line 97
[26-Sep-2016 20:36:47 UTC] PHP Notice:  Undefined variable: iitemtotal in /home/jtairadmin/public_html/webhooks/job_complete.php on line 97
[26-Sep-2016 20:36:47 UTC] PHP Notice:  Undefined variable: iitemtotalcost in /home/jtairadmin/public_html/webhooks/job_complete.php on line 97
[26-Sep-2016 20:36:47 UTC] PHP Notice:  Undefined variable: ipaymentsid in /home/jtairadmin/public_html/webhooks/job_complete.php on line 97
[26-Sep-2016 20:36:47 UTC] PHP Notice:  Undefined variable: ipaymentsinvoiceid in /home/jtairadmin/public_html/webhooks/job_complete.php on line 97
[26-Sep-2016 20:36:47 UTC] PHP Notice:  Undefined variable: iptypename in /home/jtairadmin/public_html/webhooks/job_complete.php on line 97
[26-Sep-2016 20:36:47 UTC] PHP Notice:  Undefined variable: invoiceamount in /home/jtairadmin/public_html/webhooks/job_complete.php on line 97
[26-Sep-2016 20:36:47 UTC] PHP Notice:  Undefined variable: invoicetotalamount in /home/jtairadmin/public_html/webhooks/job_complete.php on line 97
  • 3
    your insert syntax is incorrect for the first query. Wrong identifier qualifiers and requires string values to be quoted for all your queries. You never told us what errors you got back neither. – Funk Forty Niner Sep 20 '16 at 11:55
  • add quotes around the values that you add, then there is syntax error in 2nd INSERT query where it has to be `VALUES` – Sasikumar Sep 20 '16 at 11:57
  • 3
    You shouldn't use any mysql_*-functions. They have been deprecated since php 5.5 and completely removed in php 7.0 – Manish Sep 20 '16 at 11:58
  • @Fred-ii- I made the changes and now the only error left is [20-Sep-2016 14:50:31 UTC] PHP Warning: Invalid argument supplied for foreach() in /home/jtairadmin/public_html/webhooks/job_complete.php on line 42 – Joshua Miller Sep 20 '16 at 15:04
  • 1
    @JoshuaMiller You will need to update your question's code with what you are now using. Please make that as an additional edit and marked as an edit underneath your original post. That way, people won't question the original close. Reping me once you've done that, thanks. – Funk Forty Niner Sep 20 '16 at 15:06
  • @Fred-ii- Added the updated file. – Joshua Miller Sep 20 '16 at 15:53
  • @JoshuaMiller Thank you. However, `INSERT INTO techtable ('ijobid','jtid','jtname')` either remove those regular quotes `'` or replace them with ticks `\``. That insert will not work with the quotes like that. I was under the impression that you fixed those also and seems to be the root of the problem. – Funk Forty Niner Sep 20 '16 at 15:55
  • @Fred-ii- oh I thought I needed them on both so its either use ` or don't use anything for the columns? – Joshua Miller Sep 20 '16 at 15:57
  • 1
    @JoshuaMiller That is correct. Quotes `'` are only for string values in `VALUES`. Ticks `\`` are for tables/columns if they are required. However, if there are any characters that MySQL will complain about such as quotes in string value and for example: `John's Bar`, then you will need to escape those with `mysql_real_escape_string()`. A prepared statement with either `mysqli_` or PDO will take care of all that, which is something you should be moving to, as that mysql_ API is deprecated and may no longer be available for you to use in the future. It's best to start looking into that now. – Funk Forty Niner Sep 20 '16 at 16:00
  • @Fred-ii- Thank you for all your help, I edited the above file that is how the techtable should look? – Joshua Miller Sep 20 '16 at 16:05
  • 1
    @JoshuaMiller You're welcome. Yes, that's it. Are you still getting errors and if so, what are they? If there are any, edit your question again to include those errors. Once done, I'll reopen the question. – Funk Forty Niner Sep 20 '16 at 16:07
  • @Fred-ii- thank you I will test it now! – Joshua Miller Sep 20 '16 at 16:08
  • 1
    @JoshuaMiller Ooops, slight typo earlier. You are using mysqli_. The escape function needs to read as `mysqli_real_escape_string()` and and not my `mysql_` version. The use of it is, and for example `mysqli_real_escape_string($conn, $var)` replacing `$var` with the ones you wish to use and separately. – Funk Forty Niner Sep 20 '16 at 16:09
  • @Fred-ii- sorry it took me so long to get back to you i am still getting an error at line 40 & 47 (the foreach section) – Joshua Miller Sep 24 '16 at 02:08
  • @Fred-ii- also nothing sends to my sql server but i know the connection is working – Joshua Miller Sep 24 '16 at 02:08
  • @Fred-ii- I updated my main post with the new file that i have finished and the error codes I am getting any help I would be grateful thank you! – Joshua Miller Sep 26 '16 at 21:06
  • 1
    I reopened the question Joshua but there isn't anything else I can do here, sorry. Let's see if someone else can spot something I didn't. – Funk Forty Niner Sep 26 '16 at 22:19

0 Answers0