1 Replies - 273 Views - Last Post: 11 July 2012 - 02:43 PM

#1 MattDev22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-April 12

Ajax data being passed but not properly distributed

Posted 11 July 2012 - 06:55 AM

Hello,

Not sure if I worded that correctly, but I have an Ajax form passing data into a PHP file. The PHP file checks the database to see if the passed information is valid or needs to be changed. The data from the Javascript file is passed fine, but the PHP file kicks out on the first check no matter what. Also, as you can see in the code below the data is passed back as XML (buildXML2 function) and that works fine too.

The following PHP file takes on three Parameters:

piid = 1700
prid = 4592
truckno = 1

I can't find the problem. The data is passed into the PHP and the PHP sends back the XML fine. Any help would be appreciated.

<?php 
require_once '../../admin/login.php';
//require_once './quarratrac.php';
$db_handle = pg_connect($conn_string) or die("Could not connect");

if (isset($_GET['piid']))
  $piid = sanitizeString($_GET['piid']);
// Our intention is to pull ALL columns in one query if speed is ok
$query = "SELECT * 
 FROM pieceInst pi, pieceDef pd, project pr, customer cu,
 fabSrc fs, materialDef md, materialSrc ms 
 WHERE  pi.piid=$piid 
 AND    pi.defID=pd.pdid 
 AND    pd.projectID=pr.prid
 AND    pr.custID=cu.cuid
 AND    pi.fabSrcID=fs.fsid
 AND    pd.matDefId=md.mdid
 AND    pd.matSrcID=ms.msid"; 
   
buildXML2($query);
$row = pg_fetch_assoc($result); 

echo $row;
if(!$result)
{
  echo "<msg1>FAIL</msg1>\n";
  echo "<msg2>PIN Not Found</msg2>\n";
  echo "</recordset>\n";
  return;   
}
echo "cp1"; 
while($row = pg_fetch_assoc($result))
{
  echo "cp1.5";
  $truckno = $row['truckno'];
  $project = $row['prid'];
  $status  = $row['status'];
  echo "get_prid:".$_GET['prid'];
  echo "row_prid: $project";
   
  if($_GET['prid'] != $row['prid'])
  { echo "inside_prid_check";
    echo "<msg1>FAIL</msg1>\n";
    echo "<msg2>Wrong Project ID</msg2>\n";
    echo "</recordset>\n";
    return;
  }
    if( $status >= 3601)
    { 
      echo "inside_stat_check";  
      echo "<msg1>FAIL</msg1>\n";
      echo "<msg2>Already Palletized/Shipped</msg2>\n";
      echo "</recordset>\n";
      return;
    }
    echo "cp2";
    if($_GET['truckno'] != $row['truckno'])
    {   
      echo "inside_truck_check";
      $truckno = $row['truckno'];
      $query2 = "SELECT * FROM pieceInst pi, pieceDef pd  WHERE pi.defID=pd.pdid AND pd.projectid=$project 
      AND pi.truckno = $truckno AND pi.status < 3001";
      $result2 = pg_query($db_handle, $query2);    
        
      if($result2)
        {
            $row = pg_fetch_assoc($result2);
            if (swapTrucks($_GET['piid'],$_GET['truckno'],$row['piid'],$row['truckno']) )
            {
              echo "<msg1>PASS</msg1>\n";
              echo "<msg2>SWAP</msg2>\n";
              echo "</recordset>\n";
            }
            else
            {
              echo "<msg1>FAIL</msg1>\n";
              echo "<msg2>UNABLE TO SWAP</msg2>\n";
              echo "</recordset>\n";   
            }  
        }else
        { 
          echo "<msg1>PASS</msg1>\n";
          echo "<msg2>ADD Possible</msg2>\n"; 
          echo "</recordset>\n";
       }
    }
    else
    {
       echo "<msg1>PASS</msg1>\n";
       echo "</recordset>\n";
       return;
    }
  
} // end of while

function swapTrucks($pin1, $pin2, $truck1, $truck2)
{  echo "inside swap_Trucks";
  $query1  = "UPDATE pieceInst WHERE piid=$pin1 VALUES(truckno='".$truck2."')";
  $query2  = "UPDATE pieceInst WHERE piid=$pin2 VALUES(truckno='".$truck1."')";  
  $result1 = pg_query($db_handle,$query1);
  $result2 = pg_query($db_handle,$query2);
  return ($result1 && $result2);
}

function buildXML2($query)
{
  header("Content-type: text/xml\n");
  header("Pragma: public\n");        
  header("Cache-control: private\n");
  header("Expires: -1\n");
  echo '<?xml version="1.0" encoding="utf-8"?>'."\n";
  echo "<recordset>\n";
  
  $rsAll = @pg_query($query) ;
  $row_rsAll = pg_fetch_assoc($rsAll);
  $totalRows_rsAll = pg_num_rows($rsAll);

  if ($totalRows_rsAll > 0) 
  {    
    do
    {
      echo "  <record>\n";
	  foreach ($row_rsAll as $column=>$value)
      {
	    echo "    <$column>" .trim($row_rsAll[$column]). "</$column>\n"; 
	  } 
      echo "  </record>\n";
    } while ($row_rsAll = pg_fetch_assoc($rsAll));
  }
  pg_free_result($rsAll);
  return $totalRows_rsAll;
}
function sanitizeString($var)
{
  $var = stripslashes($var);
  $var = htmlentities($var);
  $var = strip_tags($var);
  return $var;
}

?>



Is This A Good Question/Topic? 0
  • +

Replies To: Ajax data being passed but not properly distributed

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5949
  • View blog
  • Posts: 23,206
  • Joined: 23-August 08

Re: Ajax data being passed but not properly distributed

Posted 11 July 2012 - 02:43 PM

You totally contradict yourself!

Quote

the PHP file kicks out on the first check no matter what


Quote

The data is passed into the PHP and the PHP sends back the XML fine


You need to more clearly describe the problem.

That query looks very ugly. Why no JOINs?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1