how to insert data into mysql server from zoho jobopening module using php and mysql

how to insert data into mysql server from zoho jobopening module using php and mysql

I am creating a mobile and webapp where i am using zoho api to store the job details details. And i have my internal mysql database so i want to get those zoho jobs opening  into mysql database.

If you help me how to get those zoho jobs opening into mysql database, that will be great. I am using PHP as server side scripting language and mysql as database. 


I have zoho account with me. below is details and i already created tocken for this account. this is the tocken: 6df045b5b84421f430ef02ad825296cb

zoho login id : pranjan@italentcorp.com

Problem : I am able to read the value but data is not inserted into my table. please help me

url to check output in browser :  http://iappease.com/myitalentstg/zoho/insert-jobopening.php



Here is my code....


<?php
header("Content-type: application/xml");

        $token="6df045b5b84421f430ef02ad825296cb";

        

        $param= "authtoken=".$token."&scope=recruitapi";

                /* initialize curl handle */
                $ch = curl_init();

                /* set url to send post request */
                curl_setopt($ch, CURLOPT_URL, $url);

                /* allow redirects */
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

                /* return a response into a variable */
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

                /* times out after 30s */
                curl_setopt($ch, CURLOPT_TIMEOUT, 30);

                /* set POST method */
                curl_setopt($ch, CURLOPT_POST, 1);

                /* add POST fields parameters */
                curl_setopt($ch, CURLOPT_POSTFIELDS, $param);

                /* execute the cURL */
                $result = curl_exec($ch);

                curl_close($ch);

                echo $result;

                return $result;
class moduleDependant 
{
    /* Define your mysql database parameters */

    //private $host = "hostname";
    //private $username = "mysql_username";
    //private $password = "mysql_password";

    private $host = "myitalentstg.db.12054818.hostedresource.com";
    private $username = "myitalentstg";
    private $password = "Q!w2e3r4";

    public function insertInDB($xml) 
    {
    echo "We are in insertInDB function.....<br/>";
    
        $numberOfRecords = count($xml->result->Leads->row);
        echo "Number of records in XML file:-".$numberOfRecords."<br/>";
        
        /* $records[row value][field value] */
        $records[][] = array();
        for ($i = 0; $i < $numberOfRecords; $i++) 
        {
            echo "We are in $i for loop.....<br/>";
            $numberOfValues = count($xml->result->Leads->row[$i]->FL);
            for ($j = 0; $j < $numberOfValues; $j++) 
            {
                echo "We are in $j for loop.....<br/>";
                switch ((string) $xml->result->Leads->row[$i]->FL[$j]['val']) 
                {
                    /* Get attributes as element indices */
                    case 'JOBOPENINGID':
                        $records[$i]['JOBOPENINGID'] = (string) $xml->result->Leads->row[$i]->FL[$j];
                        echo "JOBOPENINGID:-".$records[$i]['JOBOPENINGID']."</br>";
                        break;
                    case 'Posting Title':
                        $records[$i]['Posting Title'] = (string) $xml->result->Leads->row[$i]->FL[$j];
                        echo "Posting Title:-".$records[$i]['Posting Title']."</br>";
                        break;
                    case 'Date Opened':
                        $records[$i]['Date Opened'] = (string) $xml->result->Leads->row[$i]->FL[$j];
                        echo "Date Opened:-".$records[$i]['Date Opened']."</br>";
                        break;
                    case 'Number of Positions':
                        $records[$i]['Number of Positions'] = (string) $xml->result->Leads->row[$i]->FL[$j];
                        echo "Number of Positions:-".$records[$i]['Number of Positions']."</br>";
                        break;
                }
            }
        }
        
        
        /* Inserting in database */
        //$connection = mysql_connect($this->host, $this->username, $this->password) or die(mysql_error());
        $connection=mysqli_connect("myitalentstg.db.12054818.hostedresource.com","myitalentstg","Q!w2e3r4","myitalentstg");
            // Check connection
            if (mysqli_connect_errno())
              {
              echo "Failed to connect to MySQL: " . mysqli_connect_error();
              }
        
        //$query = "Insert into recrit.post_zoho_job (id, firstname, lastname, company) value ";
        $query = "Insert into jobopenings (post_job_id, post_job_title, date_opened, post_job_position) value ";
        for ($k = 0; $k < count($records); $k++) 
        {
            if ($k == 0) 
            {
                $query .= "('" . $records[$k]['JOBOPENINGID'] . "','" . $records[$k]['Posting Title'] . "','" . $records[$k]['Date Opened'] . "','" . $records[$k]['Number of Positions'] . "')";
                echo $query;
            }
            if ($k > 0) 
            {
                $query .= ", ('" . $records[$k]['JOBOPENINGID'] . "','" . $records[$k]['Posting Title'] . "','" . $records[$k]['Date Opened'] . "','" . $records[$k]['Number of Positions'] . "')";
                echo $query;
            }
        }
        $result = mysql_query($query, $connection) or die(mysql_error());
        if (isset($result) && mysql_affected_rows($connection) == count($records)) {
            echo "Data's are inserted in database successfully.<br/><br/> ";

            /* Table structure of inserted data, you can check inserted data by uncommenting a below section */
           
              $insertedTable = "<table cellspacing=0 cellpadding='4px' border=1>";
              $insertedTable .= "<tr><td>JOBOPENINGID</td><td>Posting Title</td><td>Date Opened</td><td>Number of Positions</td></tr>";
              for ($k = 0; $k < count($records); $k++) {
              $insertedTable .= "<tr><td>" . $records[$k]['LEADID'] . "</td><td>" . $records[$k]['Posting Title'] . "</td><td>" . $records[$k]['Date Opened'] . "</td><td>" . $records[$k]['Number of Positions'] . "</td></tr>";
              }
              $insertedTable .= "</table>";
              echo $insertedTable;
             
        } else {
            echo 'Some error while inserting in database';
        }
    }

}

?>


My database table structure


CREATE TABLE `jobopenings` (
  `post_job_id` int(10) NOT NULL AUTO_INCREMENT,
  `post_job_title` varchar(255) NOT NULL,
  `date_opened` date NOT NULL,
  `post_job_position` int(10) NOT NULL,
  PRIMARY KEY (`post_job_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;