View Full Version : problem updating my database via PHP

05-11-2011, 07:10 AM
CMS driven site that I've written a cronjob for to read latest xml file and then insert data into a MySQL database - everything works fine in terms of inserting new entries BUT i can't get it to update old ones - any help appreciated! The RED text in the code below is the portion of code in question that doesn't seem to work...

function display(){


$file_dir = JPATH_SITE . DS . 'xml';
$files_arr = array();

if ($handle = opendir($file_dir)) {
//echo "Directory handle: $handle\n";
//echo "Files:\n";

/* This is the correct way to loop over the directory. */
while (false !== ($file = readdir($handle))) {
$files_arr[] = $file;




function saveDataFromXML($fname)
include (JPATH_COMPONENT . DS . 'assets' . DS . 'xml2array.php');

$file = JPATH_SITE . DS . 'xml' . DS . $fname;

$database =& JFactory::getDBO();

$contents = file_get_contents($file);
$result = xml2array($contents, 1, 'attribute');

$database->setQuery("UPDATE #__tst_jglist_postings set `published`='0'");

## Get the Company/Advertiser
$company = $result['Jobs']['attr']['advertiser'];
$database->setQuery("SELECT * FROM #__tst_jglist_companies WHERE company_code='$company'");
$row = $database->loadObject();

$company_id = $row->id;
} else {
$database->setQuery("INSERT INTO #__tst_jglist_companies (`id`,`company_code`,`company`,`published`) VALUES ('','$company','$company',1);");
$company_id = $database->insertid();

$success = 0;
$errors = 0;

$params = "pre_article=0\n".

foreach($result['Jobs']['Job'] as $jobs){
$job_id = $jobs['attr']['jid'];
$job_ref = $jobs['attr']['reference'];
$job_date = $jobs['attr']['datePosted'];

$job_title = mysql_escape_string($jobs['Title']['value']);
$job_sum = mysql_escape_string($jobs['Summary']['value']);
$job_desc = mysql_escape_string($jobs['Description']['value']);


$job_email = mysql_escape_string($jobs['Apply']['EmailTo']['value']);
$job_url = mysql_escape_string($jobs['Apply']['Url']['value']);
$contact_id = $this->checkContactExistAdd($job_email, $company_id, $company);

$job_salary = $jobs['Salary']['MinValue']['value'];
$job_salary = $job_salary . " - " . $jobs['Salary']['MaxValue']['value'];
$job_salary = mysql_escape_string($job_salary . " " . $jobs['Salary']['attr']['period']);

foreach($jobs['Classifications']['Classification'] as $class){

if($class['attr']['name'] == "Category"){
$job_cat = $class['value'];
$job_catid = $this->checkCategoryExistAdd($job_cat, $company_id);

if($class['attr']['name'] == "Sub Category"){
$job_subcat = $class['value'];

if($class['attr']['name'] == "Location"){
$job_loc = $class['value'];
$job_locid = $this->checkLocationExistAdd($job_loc, $company_id);

if($class['attr']['name'] == "Work Type"){
$job_wtype = $class['value'];
$job_wtypeid = $this->checkWorkTypeExistAdd($job_wtype, $company_id);

$job_addinfo = "<ul>";
foreach($jobs['BulletPoints']['BulletPoint'] as $addinfo){
$job_addinfo .= "<li>".$addinfo['value']."</li>";
$job_addinfo .= "</ul>";
$job_addinfo = mysql_escape_string($job_addinfo);

$database->setQuery("SELECT * FROM #__tst_jglist_jobs WHERE id='$job_id' AND job_reference='$job_ref'");
$row = $database->loadObject();

$database->setQuery("UPDATE #__tst_jglist_jobs SET title='$job_title', category_id='$job_catid', subcategory='$job_subcat', location_id='$job_locid', pay_rate='$job_salary', job_description='$job_desc', summary='$job_sum', published='1' preferred_skills='$job_addinfo' WHERE job_id='$job_id' AND job_reference='$job_ref'");

$database->setQuery("SELECT * FROM #__tst_jglist_postings WHERE job_id='$job_id'");
$postrow = $database->loadObject();


$database->setQuery("UPDATE #__tst_jglist_postings SET published='1' WHERE job_id='$job_id' AND company_id='{$postrow->company_id}'");

} else {
$postingQuery = "INSERT INTO #__tst_jglist_postings (`id`,`job_id`,`company_id`,`location_id`,`summary`,`posting_date`,`params`,`published`,`contact_id` )".
" VALUES ('','$job_id','{$row->company_id}','{$row->location}','{$row->summary}','$job_date','$params','1','$contact_id')";

} else {

$insertQuery = "INSERT INTO #__tst_jglist_jobs (`id`, `title`, `category_id`, `subcategory`, `location_id`, ".
"`jobtype_id`, `company_id`, `pay_rate`, `job_description`, `summary`, `preferred_skills`, ".
"`email`, `url`, `published`,`job_reference` ) VALUES ('$job_id', '$job_title', '$job_catid', '$job_subcat', '$job_locid', ".
"'$job_wtypeid', '$company_id', '$job_salary', '$job_desc', '$job_sum', '$job_addinfo', ".
"'$job_email', '$job_url', '1', '$job_ref')";

$postingQuery = "INSERT INTO #__tst_jglist_postings (`id`,`job_id`,`company_id`,`location_id`,`summary`,`posting_date`,`params`,`published`,`contact_id` )".
" VALUES ('','$job_id','$company_id','$job_locid','$job_sum','$job_date','$params','1','$contact_id')";



Many thanks in advance

05-11-2011, 07:13 PM
Where's the error handling on this?
There is far too much custom stuff in here to determine what the problem is from what you have. You need to handle the errors first to determine why its failing.
Also, MySQL specific there is a REPLACE syntax as well. It will insert when non-existent keys are found, and update on collision (actually, I believe it works by deleting and recreating, so that will affect any triggers for those I'd think). Very handy for shortcuts.

05-12-2011, 03:51 AM
Thanks for the REPLACE tip
in regards for error handling - i have no idea how to do it in this case (i'm mostly self taught PHP/SQL)...

08-27-2011, 07:11 AM
I tend to agree. You need to try and simplyfy this or break it down into more readable functions then there might be some help available that isn't so vague.

08-27-2011, 03:20 PM
Then look up mysql_error (in the php manual) at a minimum and then do a web search on programming and error handling.