Jump to content
Larry Ullman's Book Forums
Sign in to follow this  
sampath

Xml File Broken From The Middle In My Php File

Recommended Posts

Dear all,

 

I have a horrible issue with one of my PHP file which generates an XML file for the AJAX response. The original file(also the correct code) is as follows.

 

<?php # update.php
header("Content-Type: text/xml");
echo '<?xml version="1.0" encoding="utf-8" standalone="yes" ?><response>';

$error = false;
if (!empty($_POST['prof_name']))
{
 $name = $_POST['prof_name'];
}
else
{
 $error = true;
 echo '<field_error>prof_name</field_error>';
}

if (!empty($_POST['prof_address']))
{
 $address = $_POST['prof_address'];
}
else
{
 $address = "";
}

if (!empty($_POST['prof_nic']))
{
 $nic = $_POST['prof_nic'];
}
else
{
 $nic = "";
}

if (!empty($_POST['prof_telephone']))
{
 $telephone = (int) $_POST['prof_telephone'];
}
else
{
 $telephone = 0;
}

if (!empty($_POST['prof_email']))
{
 $email = $_POST['prof_email'];
}
else
{
 $email = "";
}

if (!empty($_POST['prof_post']))
{
 $post = $_POST['prof_post'];
}
else
{
 $post = "";
}

if (!$error && $_POST['cmbprofessionalid']!="Default")
{
 //Open the database connection.
 require_once (dirname(__FILE__).'/../../functions/dbconnect.php');

 $q = "UPDATE Professional_Table
 SET name = '$name', address = '$address', NIC = '$nic', telephone = $telephone, email = '$email', post = '$post'
   WHERE professionalID = " . $_POST['cmbprofessionalid'];
 $r = mysql_query($q, $dbc);
 $updateError = false;
 if (mysql_affected_rows($dbc) == 0)
 {
  // Query failure.
  if(mysql_errno()>0)
  {
   $updateError = true;
   if(mysql_error()=="Duplicate entry '" . $name . "' for key 'name'")
   {
 echo '<name_error>prof_name</name_error>';
 echo '<system_error>Same professional name already exists! Please select a different name and click "Submit".</system_error>';
   }
  }
 }
 //Start saving the professional user rights.
 $professionalID =  $_POST['cmbprofessionalid'];

 //Check whether the change rights are set.
 if(isset($_POST['prof_changeright']) || isset($_POST['prof_viewright']))
 {
  //Delete the old user rights from the professional_user_rights_table
  $q = "DELETE FROM Professional_User_Rights_Table WHERE professionalID = $professionalID";
  $r = mysql_query($q, $dbc);
  //Extract the professional names from change rights list seperately.
  $line_data1 = explode("\n", $_POST['prof_changeright']);
  //For each extracted professional name start saving the rights.
  foreach($line_data1 as $key => $value1)
  {
   //Retrive the allowed professional ID from the professional table.
   $q2 = "SELECT professionalID FROM Professional_Table WHERE name = '$value1'";
   $r2 = mysql_query($q2, $dbc);
   if (mysql_num_rows($r2)>0)
   {
 // Retrieve the results:
 $row2 = mysql_fetch_row($r2);
 //Prepare the professional ID.
 $allowedChangeProfessionalID =  $row2[0];
 //Professional has both change and view rights.
 $q3 = "INSERT INTO Professional_User_Rights_Table (professionalID, allowedProfessionalID, changeRight, viewRight) VALUE($professionalID, $allowedChangeProfessionalID, 1, 1)";
 $r3 = mysql_query($q3, $dbc);
 if (mysql_affected_rows($dbc) == 0)
 {
  $rights_save_error = true;
 }
   }
  }

  //Extract the professional names from view rights list seperately.
  $line_data2 = explode("\n", $_POST['prof_viewright']);
  //For each extracted professional name start saving the rights.
  foreach($line_data2 as $key => $value2)
  {
   $q4 = "SELECT professionalID FROM Professional_Table WHERE name = '$value2'";
   $r4 = mysql_query($q4, $dbc);
   if (mysql_num_rows($r4)>0)
   {
 // Retrieve the results:
 $row4 = mysql_fetch_row($r4);
 //Prepare the professional ID.
 $allowedViewProfessionalID =  $row4[0];

 //Check whether the user rights inserted for the selected professional under the change rights.
 $q5 = "SELECT allowedProfessionalID FROM Professional_User_Rights_Table WHERE professionalID = '$professionalID' AND allowedProfessionalID = '$allowedViewProfessionalID'";
 $r5 = mysql_query($q5, $dbc);
 if (mysql_num_rows($r5)==0)
 {
  //Professional has only view right.
  $q6 = "INSERT INTO Professional_User_Rights_Table (professionalID, allowedProfessionalID, changeRight, viewRight) VALUE($professionalID, $allowedViewProfessionalID, 0, 1)";
  $r6 = mysql_query($q6, $dbc);
  if (mysql_affected_rows($dbc) == 0)
  {
   $rights_save_error = true;
  }
 }
   }
  }   
 }
 if(!$rights_save_error)
 {
  if(!$updateError)
  {
   echo '<result>The professional has been successfully updated.</result>';
  }
 }
 else
 {
  echo '<system_error>Error occured while saving user rights!</system_error>';
 }
 mysql_close($dbc);
}
else
{  // Errors!
 if($_POST['cmbprofessionalid']=="Default")
 {
  echo '<not_select_error>Please select a professional to update information.</not_select_error>';
 }
 else
 {
  echo '<system_error>Please correct problems with the highlighted field(s) below.</system_error>';
 }
}
echo '</response>';
exit();
?>

 

But after saving the file, I can see that the file is broken as follows.

 

<?php # update.php
header("Content-Type: text/xml");
echo '<?xml version="1.0" encoding="utf-8" standalone="yes" ?><response>';

$error = false;
if (!empty($_POST['prof_name']))
{
 $name = $_POST['prof_name'];
}
else
{
 $error = true;
 echo '<field_error>prof_name</field_error>';
}

if (!empty($_POST['prof_address']))
{
 $address = $_POST['prof_address'];
}
else
{
 $address = "";
}

if (!empty($_POST['prof_nic']))
{
 $nic = $_POST['prof_nic'];
}
else
{
 $nic = "";
}

if (!empty($_POST['prof_telephone']))
{
 $telephone = (int) $_POST['prof_telephone'];
}
else
{
 $telephone = 0;
}

if (!empty($_POST['prof_email']))
{
 $email = $_POST['prof_email'];
}
else
{
 $email = "";
}

if (!empty($_POST['prof_post']))
{
 $post = $_POST['prof_post'];
}
else
{
 $post = "";
}

if (!$error && $_POST['cmbprofessionalid']!="Default")
{
 //Open the database connection.
 require_once (dirname(__FILE__).'/../../functions/dbconnect.php');

 $q = "UPDATE Professional_Table
 SET name = '$name', address = '$address', NIC = '$nic', telephone = $telephone, email = '$email', post = '$post'
   WHERE professionalID = " . $_POST['cmbprofessionalid'];
 $r = mysql_query($q, $dbc);
 $updateError = false;
 if (mysql_affected_rows($dbc) == 0)
 {
  // Query failure.
  if(mysql_errno()>0)
  {
   $updateError = true;
   if(mysql_error()=="Duplicate entry '" . $name . "' for key 'name'")
   {
 echo '</response>';
exit();
?>

----------File is broken here---------------

<name_error>prof_name</name_error>';
 echo '<system_error>Same professional name already exists! Please select a different name and click "Submit".</system_error>';
   }
  }
 }
 //Start saving the professional user rights.
 $professionalID =  $_POST['cmbprofessionalid'];

 //Check whether the change rights are set.
 if(isset($_POST['prof_changeright']) || isset($_POST['prof_viewright']))
 {
  //Delete the old user rights from the professional_user_rights_table
  $q = "DELETE FROM Professional_User_Rights_Table WHERE professionalID = $professionalID";
  $r = mysql_query($q, $dbc);
  //Extract the professional names from change rights list seperately.
  $line_data1 = explode("\n", $_POST['prof_changeright']);
  //For each extracted professional name start saving the rights.
  foreach($line_data1 as $key => $value1)
  {
   //Retrive the allowed professional ID from the professional table.
   $q2 = "SELECT professionalID FROM Professional_Table WHERE name = '$value1'";
   $r2 = mysql_query($q2, $dbc);
   if (mysql_num_rows($r2)>0)
   {
 // Retrieve the results:
 $row2 = mysql_fetch_row($r2);
 //Prepare the professional ID.
 $allowedChangeProfessionalID =  $row2[0];
 //Professional has both change and view rights.
 $q3 = "INSERT INTO Professional_User_Rights_Table (professionalID, allowedProfessionalID, changeRight, viewRight) VALUE($professionalID, $allowedChangeProfessionalID, 1, 1)";
 $r3 = mysql_query($q3, $dbc);
 if (mysql_affected_rows($dbc) == 0)
 {
  $rights_save_error = true;
 }
   }
  }

  //Extract the professional names from view rights list seperately.
  $line_data2 = explode("\n", $_POST['prof_viewright']);
  //For each extracted professional name start saving the rights.
  foreach($line_data2 as $key => $value2)
  {
   $q4 = "SELECT professionalID FROM Professional_Table WHERE name = '$value2'";
   $r4 = mysql_query($q4, $dbc);
   if (mysql_num_rows($r4)>0)
   {
 // Retrieve the results:
 $row4 = mysql_fetch_row($r4);
 //Prepare the professional ID.
 $allowedViewProfessionalID =  $row4[0];

 //Check whether the user rights inserted for the selected professional under the change rights.
 $q5 = "SELECT allowedProfessionalID FROM Professional_User_Rights_Table WHERE professionalID = '$professionalID' AND allowedProfessionalID = '$allowedViewProfessionalID'";
 $r5 = mysql_query($q5, $dbc);
 if (mysql_num_rows($r5)==0)
 {
  //Professional has only view right.
  $q6 = "INSERT INTO Professional_User_Rights_Table (professionalID, allowedProfessionalID, changeRight, viewRight) VALUE($professionalID, $allowedViewProfessionalID, 0, 1)";
  $r6 = mysql_query($q6, $dbc);
  if (mysql_affected_rows($dbc) == 0)
  {
   $rights_save_error = true;
  }
 }
   }
  }   
 }
 if(!$rights_save_error)
 {
  if(!$updateError)
  {
   echo '<result>The professional has been successfully updated.</result>';
  }
 }
 else
 {
  echo '<system_error>Error occured while saving user rights!</system_error>';
 }
 mysql_close($dbc);
}
else
{  // Errors!
 if($_POST['cmbprofessionalid']=="Default")
 {
  echo '<not_select_error>Please select a professional to update information.</not_select_error>';
 }
 else
 {
  echo '<system_error>Please correct problems with the highlighted field(s) below.</system_error>';
 }
}
echo '

 

As you will notice from the second code segment, part of the PHP code is out from the PHP end tag.

 

I'm using Dreamweaver as the editor.

 

The Tag Inspector in Dreamweaver is not properly referring to the tags in the PHP file.

 

I cannot figure out the exact issue here!

 

I expect some support from you to solve this issue.

 

Thank you in advance!,

 

Sampath.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×