-2

I am getting the following errors when i am trying to insert parsed data into a table in my database:

PHP Notice:  Undefined index: manu in /home/svn/dev.comp/Asus.php on line 112
PHP Notice:  Undefined index: partnumber in /home/svn/dev.comp/Asus.php on line 112
PHP Notice:  Undefined index: description in /home/svn/dev.comp/Asus.php on line 112
PHP Notice:  Undefined property: DatabaseManager::$_link in /home/svn/dev.comp/Asus.php on line 112
PHP Warning:  mysql_query() expects parameter 2 to be resource, null given in /home/svn/dev.comp/Asus.php on line 112

Here is the code i have so far:

<?php

//Declaring all database connection information
$username = "username";
$password = "password";
$database = "database_name";

//Setting connection to database, fail if could not connect
$link = mysql_connect("localhost", $username, $password) or die("Could not connect to database");
mysql_select_db($database, $link);

$csv = new CSVManager();
$data = $csv->get_data_array();
$api = new DataGrabber($link);
$xmldata = $api->call_api($data);
$dm = new DatabaseManager();
$dm->insert_asus_data($xmldata);


//This class will pull data from the .CSV file and store it inside an array
class CSVManager {

 public function get_data_array() {

     //Open and declare location of .CSV file to pull data from 
     $hook = fopen('asus.csv', 'r');

     //Number of columns in .CSV file we are getting data from
     $allowedCols = 5;

     //Store all data we will extract, into an array
     $data = array();

     //Count the number of lines in the .CSV file
     while($line = fgetcsv($hook)) {

         $numcols = count($line);

         //Test wether the number of columns we are declaring, is equal to the number of columns in the .CSV file
         if($numcols == $allowedCols) {

             //If so, assign each data row from the column names to new entries which will be declared in the DatabaseManager Class 
             $entry = array(
                 'man' => $line[0], //index of column name in .CSV file
                 'cat' => $line[1],
                 'model' => $line[2],
                 'family_id' => $line[3],
                 'family' => $line[4]
             );

             //Insert all entries into the data array declared above
             $data[] = $entry;

         }


     }
     //Once data is inside the array, return this data
     return $data;  
 }      
}

//This class will pull data from the XML document and store it inside another array
class DataGrabber {

    //The URL where data will be extracted from, which is an XML file
    protected $URL = "http://json.zandparts.com/api/category/GetCategories/44/EUR/";

    public function call_api($data) {


        if(count($data) == 0) return array();

        $jsondata = array();

        foreach($data as $entry){


            $url = $this->URL . $entry['model'] . "/". "E%20Series" . "/" . rawurlencode($entry['cat']) . "/" . $entry['man'] . "/null";
            $json = file_get_contents($url);

            $data = json_decode($json, true);

            if(!empty($data['Products'])){
                foreach ($data['Products'] as $id => $product) {

                    $jsonentry = array(
                        'productnumber' => $id,
                        'partnumber' => $product['strPartNumber'],
                        'description' => $product['strDescription'],
                        'manu' => $product['Brand']
                    );

                    $jsondata[] = $jsonentry;
                }
            }
        }

        return $jsondata;

    }

}

It works fine upto here, but with the following code below it throws the errors above:

class DatabaseManager {

    public function insert_asus_data($partbind) {

       //create a new MySQL statement to insert data into the database table. Bind the entries declared above from both classes, to the fieldnames in the database and insert them as values.
       mysql_query("INSERT INTO asus_parts (manufacturer, partNumber, description) VALUES ('{$partbind['manu']}', '{$partbind['partnumber']}','{$partbind['description']}')", $this->_link);

    }

}
?>

They are both in the same file.. I dont understand why it is throwing these errors and how can i overcome this? Thanks.

[EDIT]

Here is one of the print statements:

[5] => Array
        (
            [productnumber] => 0
            [partnumber] => 0B200-00120100
            [description] => ME370T BAT COSLI LI-POLY FPACK
            [manu] => Google
        )

    [6] => Array
        (
            [productnumber] => 1
            [partnumber] => 0B200-00120200
            [description] => ME370T BAT COSLI LI-POLY FPACK
            [manu] => Google
        )

Still does not insert any data in the table

ash
  • 93
  • 5
  • 14
  • add an `print_r($partbind)` in `insert_asus_data`. I assume it is null (or empty) - so you simple miss to pass the right array to that function. – dognose May 07 '13 at 13:00
  • 1
    You're not assigning $link to the DatabaseManager class, and your array is inside another array. $partbind[0]['manu'] – aynber May 07 '13 at 13:02
  • Thanks for the help, @aynber i seem to have gotten rid of the errors but now they are gone, it still doesnt insert into the database.. i added the index too but still nothing. Edit above shows an output for the `print_r` statement – ash May 07 '13 at 13:43
  • I'd recommend stop using mysql_query since it's being deprecated, and going to PDO or mysqli, which has some good mysql error logging. Also, if possible, turn on mysql logging and watch the log to see the exact query going in, and if you can run that query yourself from the mysql command line or phpmyadmin. – aynber May 07 '13 at 14:02
  • @aynber thanks for the headsup however, the system for our company can not be compatible with the newer version which is why we can only use mysql – ash May 07 '13 at 14:08
  • Gotcha. Try the logging, if you can. Check out http://php.net/manual/en/function.mysql-error.php for information on getting mysql errors. – aynber May 07 '13 at 14:33
  • @aynber it seems to be working now thanks :-) But it only inserts one record, i am gathering because i have changed it to `$partbind[0]['manu']` for one item, how can i do it for all items? – ash May 07 '13 at 14:58
  • 1
    Instead of doing [0], do this: foreach($partbind as $bind){ mysql_query("INSERT INTO asus_parts (manufacturer, partNumber, description) VALUES ('{$bind['manu']}', '{$bind['partnumber']}','{$bind['description']}')", $this->_link); } – aynber May 07 '13 at 15:13

2 Answers2

1

The query is wrong you don't need the link in insert query change it to this

mysql_query("INSERT INTO asus_parts (manufacturer, partNumber, description) VALUES ('{$partbind['manu']}', '{$partbind['partnumber']}','{$partbind['description']}')");
chandresh_cool
  • 11,753
  • 3
  • 30
  • 45
0

You must escape your variables.

'{$partbind['manu']}'

This is shown as '{$partbind['

To escape the second quote just double it:

'{$partbind[''manu'']}'
Alan Piralla
  • 1,216
  • 2
  • 11
  • 21
  • 1
    No, thats wrong. He used `{}` around the variable so that part is fine. (it will be evaluated BEFORE the string enters mysql) – dognose May 07 '13 at 12:58