当我尝试将解析的数据插入数据库中的表时出现以下错误:
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
这是我到目前为止的代码:
<?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;
}
}
它在这里工作正常,但是下面的代码会引发上面的错误:
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);
}
}
?>
它们都在同一个文件中。我不明白为什么它会抛出这些错误,我该如何克服这个问题?谢谢。
[编辑]
这是打印语句之一:
[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
)
仍然没有在表中插入任何数据