Hey All,

I know this piece of script is not the best way to write this but it actually worked when I started this project, but for some reason it no longer works. I was wondering if someone could take a look at it and give me some improvements or recommendations for it to work better.

I just added a way for the db to send an email when a workorder is updated. It checks to see if the tech has changed, if so, send him an email letting him know the workorder is now his. Should be simple. This script results in a blank page, no error messages.

<?php

	$woid = $_POST['woid'];
  	$date = $_POST['date'];
	$rcvdby = $_POST['rcvdby'];
	$userid = $_POST['userid'];
	$problem = $_POST['problem'];
	$solution = $_POST['solution'];
	$status = $_POST['status'];
	$closeddt = $_POST['closeddt'];
	$partsused = $_POST['partsused'];
	$techid = $_POST['techid'];
	$catid = $_POST['catid'];
	$priority = $_POST['priority'];
	$poster = $_POST['poster'];
	$deptcode = $_POST['deptcode'];
	
	  
include("config.php");
dbConnect();
	
   //Check to see what tech is assigned now
   $query1="SELECT techid FROM workorder WHERE woid = $woid";
   $result1=mysql_query($query1);
   while($row1=mysql_fetch_array($result1,MYSQL_ASSOC))
   
       $oldtechid = $row1['techid'];
       
       
    //Get the tech's email address         
 	$query2="SELECT techid, email FROM tech WHERE techid = $techid";
	$result2=mysql_query($query2);
	while($row2=mysql_fetch_array($result2,MYSQL_ASSOC))  
 	   	
   	$email = $row2['email'];
   	  	
   	         
	//Check to see if the old tech and new tech are different, if so send email to new tech
	if ($oldtechid <> $techid)
	$text="Workorder #: $woid has been assigned to you by $poster";
	$headers = "From: Workorder Tracking\r\n";
	mail($email, 'Workorder Update', $text, $headers); 
   	else
   	
   	//continue with updating the database with new changes.
	$query = "UPDATE workorder SET userid='$userid', problem='$problem', solution='$solution', status='$status', techid='$techid', catid='$catid', priority='$priority', poster='$poster', deptcode='$deptcode' WHERE woid=$woid";
  	$result = mysql_query($query) or die('Sorry, we could not edit this WO to the database at this time');
	
    if ($result)
       echo "<h2><a href=\"index.php\">Workorder $woid Updated - Click Here Return to workorder</a></h2>\n";
              
    else
       echo "<h2>Sorry, there was a problem editing your workorder</h2>\n";

?>

Any help or pointers are greatly appreciated.

Thanks.

You need a program that will do a PHP error check for you. There are many of them, Netbeans is one example.

When you get a blank screen it usually means that you have a PHP error (assuming the program is supposed to display something). In this case, when you error check it, you find that there is an error on Line 44. That is because you are using IF and ELSE without curly brackets. You can only put one statement between an IF and an ELSE if you don't use curly brackets. I think that it also makes the code harder to follow when you don't use the brackets.

Member Avatar for rajarajan2017

Echo the variables you have and check whether it is displaying or not. If not then that might be an error?

You need a program that will do a PHP error check for you. There are many of them, Netbeans is one example.

When you get a blank screen it usually means that you have a PHP error (assuming the program is supposed to display something). In this case, when you error check it, you find that there is an error on Line 44. That is because you are using IF and ELSE without curly brackets. You can only put one statement between an IF and an ELSE if you don't use curly brackets. I think that it also makes the code harder to follow when you don't use the brackets.

I think you are right Chris, if I comment out lines 24 - 45 (the email portion) the script runs fine, I'll try putting in the curly brackets and see if that is the problem. Thank you for the reply, very helpful to this beginner.

You need a program that will do a PHP error check for you. There are many of them, Netbeans is one example.

When you get a blank screen it usually means that you have a PHP error (assuming the program is supposed to display something). In this case, when you error check it, you find that there is an error on Line 44. That is because you are using IF and ELSE without curly brackets. You can only put one statement between an IF and an ELSE if you don't use curly brackets. I think that it also makes the code harder to follow when you don't use the brackets.

You were absolutely correct Chris, thank you so much. Here is my finished product that works.

<?php

	$woid = $_POST['woid'];
  	$date = $_POST['date'];
	$rcvdby = $_POST['rcvdby'];
	$userid = $_POST['userid'];
	$problem = $_POST['problem'];
	$solution = $_POST['solution'];
	$status = $_POST['status'];
	$closeddt = $_POST['closeddt'];
	$partsused = $_POST['partsused'];
	$techid = $_POST['techid'];
	$catid = $_POST['catid'];
	$priority = $_POST['priority'];
	$poster = $_POST['poster'];
	$deptcode = $_POST['deptcode'];
	
	  
include("config.php");
dbConnect();
	
   //Check to see what tech is assigned now

   $query1="SELECT techid FROM workorder WHERE woid = $woid";
   $result1=mysql_query($query1);
   while($row1=mysql_fetch_array($result1,MYSQL_ASSOC))
   
       $oldtechid = $row1['techid'];
        
    //Get the tech's email address         
 	$query2="SELECT techid, email FROM tech WHERE techid = $techid";
	$result2=mysql_query($query2);
	while($row2=mysql_fetch_array($result2,MYSQL_ASSOC))  
 	   	
   	$email = $row2['email'];
   	  	
	         
	//Check to see if the old tech and new tech are different, if so send email to new tech
	if 	($oldtechid <> $techid){
	$text="Workorder # $woid has been assigned to you by $poster. The reported problem is as follows: $problem\r\r\r Thank you and have a pretty good day.\r\r Amis";
	$headers = "From: Workorder Tracking\r\n";
	mail($email, 'Workorder Update', $text, $headers); 
 	}
	else
	{ //if not, display error message
		echo "email was not sent";
	}
   	// then continue with updating the database with new changes.
	$query = "UPDATE workorder SET userid='$userid', problem='$problem', solution='$solution', status='$status', techid='$techid', catid='$catid', priority='$priority', poster='$poster', deptcode='$deptcode' WHERE woid=$woid";
  	$result = mysql_query($query) or die('Sorry, we could not edit this WO to the database at this time');
	
    if ($result)
       echo "<h2><a href=\"index.php\">Workorder $woid Updated - Click Here Return to workorder</a></h2>\n";
  
    else
    
       echo "<h2>Sorry, there was a problem editing your workorder</h2>\n";
?>

I had to add what to do if the techid's matched after the email function or it would error if the techid's were equal. Other than that, it works great. Thanks again for your insight.

Rick

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.