Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with Conditonal IF Statement

Status
Not open for further replies.

Cheryl73

Programmer
Aug 16, 2004
9
0
0
US
Hello All,

I need help with some PHP code. I think that I having problems with properly coding my if-else conditional statements on a php web page. I'm rather new to PHP.

I am creating PHP code to work with a MySQL database in the background. I have three buttons on a PHP web page called "Updatereceipt.inc.php". The three buttons are used to either delete, or update a record in the MySQL database, or just select a record from the database to show on a web page for printing. Here is the code for the three buttons on the "Updatereceipt.inc.php" page:

Code:
 ...
   echo "<input type=\"submit\" name=\"button\" value=\"Update\">\n";
   echo "<input type=\"submit\" name=\"button\" value=\"Delete Receipt\">\n";
   echo "<input type=\"submit\" name=\"button\" value=\"Print Receipt\">\n"; 
   echo "</form>\n";
?>

Once a button is selected, it calls a PHP page called "changereceipt_working2.inc.php". Depending on which button is selected on "Updatereceipt.inc.php", I want it to go the portion of the code on "changereceipt_working2.inc.php" that deals with either "delete receipt", or "Update", or "print receipt" based on conditional if-else statements on the "changereceipt_working2.inc.php" web page. Such as ...$delete = $_POST['button']; if ($delete == "Delete Receipt"), ...$print = $_POST['button']; if ($print == "Print Receipt"), ... $update = $_POST['button'];if($update == "Update").

Each of these choices is based on the receipt_id variable passed from the "Updatereceipt.inc.php" page. I believe that my coding problem is with my PHP if-else conditonal statements on the "changereceipt_working2.inc.php" page. If I select to delete, hte code works fine and the record is deleted from mysql database based on receipt_id. But If I select either "Update" or "Print Receipt", I receive the following error message:


Parse error: parse error in C:\wamp\ on line 156

Line 156 is a part of the if-elseif-else conditonal code on the web page. Line 156 is just before the line of code that states:
Code:
$update = $_POST['button'];
if($update == "Update")
Could some one tell me what is not correct about my conditonal if-else code, here is all the code for the "changereceipt_working2.inc.php" web page:

Code:
<?php   
   $delete = $_POST['button'];
   if ($delete == "Delete Receipt")
   {
      $receipt_id = $_POST['receipt_id'];
      $query = "DELETE from icc_recipts WHERE receipt_id = $receipt_id";
      $result = mysql_query($query);
      if ($result)
      {
         echo "<h2>Receipt Number: $receipt_id deleted</h2>\n";
         exit;
      } else
      {
         echo "<h2>Problem deleting receipt $receipt_id</h2>\n";
         exit;
      }
  
 $print = $_POST['button'];
   }elseif ($print == "Print Receipt")

 
   {      
   
      $receipt_id = $_POST['receipt_id'];
      $donor_first_name = $_POST['donor_first_name'];
      $donor_middle_initial=$_POST['donor_middle_initial'];
      $donor_last_name=$_POST['donor_last_name'];
      $donor_address1=$_POST['donor_address1'];
      $donor_address2=$_POST['donor_address2'];
      $donor_city=$_POST['donor_city'];
      $state_id=$_POST['statename'];
      $donor_zip=$_POST['donor_zip'];
      $event_id=$_POST['eventtype'];
      $contrib_description=$_POST['contrib_description'];
      $total_amount_paid=$_POST['total_amount_paid'];
      $amount_tax_deductible=$_POST['amount_tax_deductible'];
      $payment_type_id=$_POST['paymenttype']; 
      $goods_services_provided = $_POST['goods_services_provided'];
}
 

  if (isset($_POST['goods_services_provided']))
  $goods_services_provided = 1;
  else
  $goods_services_provided = 0;

         $query = "SELECT a.receipt_id, a.donor_first_name, a.donor_last_name, ".      	
            "a.donor_address1, a.donor_address2, a.event_id, a.donor_city, ".	
            "a.donor_middle_initial, a.donor_zip, a.contrib_description, ".	
            "a.total_amount_paid, a.amount_tax_deductible, ".	
            "a.goods_services_provided, ".	
            "d.payment_type_id As payment_type_id, ".
            "b.state_id As state_id, c.event_description As event_description ".              	
             "FROM icc_recipts a, states_lookup b, events_lookup c, payment_type_lookup d ".	
             " WHERE  a.state_id = b.state_id ".	
             " AND a.payment_type_id = d.payment_type_id ".	
             " AND a.event_id = c.event_id ".	
             " AND a.receipt_id = $receipt_id";
    $result = mysql_query($query);   
    $row = mysql_fetch_array($result, MYSQL_ASSOC); 

   $donor_first_name = $row['donor_first_name'];
   $donor_middle_initial = $row['donor_middle_initial'];
   $donor_last_name = $row['donor_last_name'];
   $donor_address1 = $row['donor_address1'];
   $donor_address2 = $row['donor_address2'];
   $donor_city = $row['donor_city'];
   $state_id = $row['state_id'];
   $donor_zip = $row['donor_zip'];
   $event_id= $row['event_id'];
   $contrib_description = $row['contrib_description'];
   $total_amount_paid = $row['total_amount_paid'];
   $amount_tax_deductible = $row['amount_tax_deductible'];
   $payment_type_id = $row['payment_type_id'];
   $goods_services_provided = $row['goods_services_provided'];
   $query="SELECT state_id, state_name from states_lookup WHERE state_id = $state_id";
   $result=mysql_query($query);
   $row = mysql_fetch_array($result, MYSQL_ASSOC);   
   $statename = $row['state_id'];   
   $query="SELECT payment_type_id, payment_type from payment_type_lookup WHERE payment_type_id = $payment_type_id";
   $result=mysql_query($query);
   $row = mysql_fetch_array($result, MYSQL_ASSOC);   
   $paymenttype = $row['payment_type_id'];
   $query="SELECT event_id, event_description from events_lookup WHERE event_id = $event_id";
   $result=mysql_query($query);
   $row = mysql_fetch_array($result, MYSQL_ASSOC);   
   $eventname = $row['event_id']; 
   echo "<h3 align=center>The Ivy Community Charities of Prince George's County, Incorporated</h3>\n";   
   echo "<h4 align=center>(a 501(c) 3 organization)</h4>\n"; 
   echo "<h2 align=center>Contribution Acknowledgement/Receipt</h2>\n";    
   echo "<form enctype=\"multipart/form-data\" action=\"admin.php\" method=\"post\">\n";
   echo "<input type=\"hidden\" name=\"receipt_id\" value=\"$receipt_id\">\n";
   echo "<table width=\"100%\" cellpadding=\"0\" border=\"0\">\n";
   echo "<tr><td><h3>Receipt ID</h3></td><td>$receipt_id</td></tr>\n";
   echo "<tr><td><h3>Donor's Name</h3></td><td>$donor_first_name</td>\n";
   echo "<td>$donor_middle_initial</td>\n";
   echo "<td>$donor_last_name</td></tr>\n";
   echo "<tr><td><h3>Donor Address 1</h3></td><td><input type=\"text\" name=\"donor_address1\"   value=\"$donor_address1\"></td></tr>\n";
   echo "<tr><td><h3>Donor Address 2</h3></td><td><input type=\"text\" name=\"donor_address2\"   value=\"$donor_address2\"></td></tr>\n";
   echo "<tr><td><h3>Donor City</h3></td><td><input type=\"text\" name=\"donor_city\"   value=\"$donor_city\"></td></tr>\n";
  
   echo "<tr><td><h3>Donor State</h3><td><select name=\"statename\">\n";   
   $query="SELECT state_id, state_name from states_lookup";
   $result=mysql_query($query);
   while($row=mysql_fetch_array($result,MYSQL_ASSOC))
   {
       $tempstateid = $row['state_id'];
       $state_name = $row['state_name'];
       if ($tempstateid == $state_id)
         echo "<option value=\"$tempstateid\" selected=\"selected\">$state_name</option>\n";
       else
         echo "<option value=\"$tempstateid\">$state_name</option>";
   }
   echo "</select></td></tr>\n";
    echo "<tr><td><h3>Donor Zip</h3></td><td><input type=\"text\" name=\"donor_zip\"   value=\"$donor_zip\"></td></tr>\n";


  echo "<tr><td><h3>Event Type</h3><td><select name=\"eventtype\">\n"; 
  $query="SELECT event_id, event_description from events_lookup";
   $result=mysql_query($query);
   while($row=mysql_fetch_array($result,MYSQL_ASSOC))
   {
       $tempeventtypeid = $row['event_id'];
       $event_description = $row['event_description'];
       if ($tempeventtypeid == $event_id)
         echo "<option value=\"$tempeventtypeid\" selected=\"selected\">$event_description</option>\n";
       else
         echo "<option value=\"$tempeventtypeid\">$event_description</option>";
   }
   echo "</select></td></tr>\n";
   echo "<tr><td><h3>Description</h3></td><td><input type=\"text\" size=\"50\"  name=\"contrib_description\"   value=\"$contrib_description\"></td></tr>\n";
   echo "<tr><td><h3>Payment Type</h3><td><select name=\"paymenttype\">\n"; 
  $query="SELECT payment_type_id, payment_type from payment_type_lookup";
   $result=mysql_query($query);
   while($row=mysql_fetch_array($result,MYSQL_ASSOC))
   {
       $temppaytypeid = $row['payment_type_id'];
       $payment_type = $row['payment_type'];
       if ($temppaytypeid == $payment_type_id)
         echo "<option value=\"$temppaytypeid\" selected=\"selected\">$payment_type</option>\n";
       else
         echo "<option value=\"$temppaytypeid\">$payment_type</option>";
   }
   echo "</select></td></tr>\n";
   echo "<tr><td><h3>Total Amount Paid</h3></td><td><input type=\"text\" name=\"total_amount_paid\" value=\"$total_amount_paid\"></td></tr>\n";
   echo "<tr><td><h3>Amount Tax Deductible</h3></td><td><input type=\"text\" name=\"amount_tax_deductible\" value=\"$amount_tax_deductible\"></td></tr>\n";

   if ($goods_services_provided)
     echo "<tr><td><h3>Goods or Services Provided?</h3></td><td><input type=\"checkbox\" name=\"goods_services_provided\" value=\"1\" checked></td></tr>\n";
   else
      echo "<tr><td><h3>Goods or Services Provided?</h3></td><td><input type=\"checkbox\" name=\"goods_services_provided\" value=\"1\"></td></tr>\n";     
   echo "</table>\n";
   echo "</form>\n";
   exit;

}
else {


  $update = $_POST['button'];

   if($update == "Update")

 {    
      $receipt_id = $_POST['receipt_id'];
      $donor_first_name = $_POST['donor_first_name'];
      $donor_middle_initial=$_POST['donor_middle_initial'];
      $donor_last_name=$_POST['donor_last_name'];
      $donor_address1=$_POST['donor_address1'];
      $donor_address2=$_POST['donor_address2'];
      $donor_city=$_POST['donor_city'];
      $state_id=$_POST['statename'];
      $donor_zip=$_POST['donor_zip'];
      $event_id=$_POST['eventtype'];
      $contrib_description=$_POST['contrib_description'];
      $total_amount_paid=$_POST['total_amount_paid'];
      $amount_tax_deductible=$_POST['amount_tax_deductible'];
      $payment_type_id=$_POST['paymenttype']; 
      $goods_services_provided = $_POST['goods_services_provided'];
}
 if (get_magic_quotes_gpc())
 {
 $donor_first_name = stripslashes($donor_first_name);
 $donor_middle_initial = stripslashes($donor_middle_initial);
 $donor_last_name = stripslashes($donor_last_name);
 $donor_address1 = stripslashes($donor_address1);
 $donor_address2 = stripslashes($donor_address2);
 $donor_city = stripslashes($donor_city);
 $state_id = stripslashes($state_id);
 $donor_zip = stripslashes($donor_zip);
 $event_id = stripslashes($event_id);
 $contrib_description = stripslashes($contrib_description);
 $total_amount_paid = stripslashes($total_amount_paid);
 $amount_tax_deductible = stripslashes($amount_tax_deductible);
 $payment_type_id = stripslashes($payment_type_id);
 $goods_services_provided = stripslashes($goods_services_provided);
  }
 $donor_first_name = mysql_real_escape_string($donor_first_name);
 $donor_middle_initial = mysql_real_escape_string($donor_middle_initial);
 $donor_last_name = mysql_real_escape_string($donor_last_name);
 $donor_address1 = mysql_real_escape_string($donor_address1);
 $donor_address2 = mysql_real_escape_string($donor_address2);
 $donor_city = mysql_real_escape_string($donor_city);
 $state_id = mysql_real_escape_string($state_id);
 $donor_zip = mysql_real_escape_string($donor_zip);
 $event_id = mysql_real_escape_string($event_id);
 $contrib_description = mysql_real_escape_string($contrib_description);
 $total_amount_paid = mysql_real_escape_string($total_amount_paid);
 $amount_tax_deductible = mysql_real_escape_string($amount_tax_deductible);
 $payment_type_id = mysql_real_escape_string($payment_type_id);
 $goods_services_provided = mysql_real_escape_string($goods_services_provided);

  if (isset($_POST['goods_services_provided']))
  $goods_services_provided = 1;
  else
  $goods_services_provided = 0;
  
      $query = "UPDATE icc_recipts SET donor_first_name ='$donor_first_name', ".
      " donor_middle_initial ='$donor_middle_initial', ".
      " donor_last_name ='$donor_last_name', ".
      " donor_address1 ='$donor_address1', ".
      " donor_address2 ='$donor_address2', ".
      " donor_city ='$donor_city', ".
      " state_id ='$state_id', ".
      " donor_zip ='$donor_zip', ".
      " event_id ='$event_id', ".
      " contrib_description ='$contrib_description', ".
      " payment_type_id ='$payment_type_id', ".
      " total_amount_paid ='$total_amount_paid', ".
      " amount_tax_deductible ='$amount_tax_deductible', ".
      " goods_services_provided = $goods_services_provided ".
      " WHERE receipt_id = $receipt_id";  
          
      $result = mysql_query($query) or die(mysql_error());
      if ($result)
      {
         echo "<h2>Receipt information changed.</h2>\n";
      }
      else
      {
         echo "<h2>Sorry, the receipt information could not be changed.</h2>\n";
      }
  
?>

Please help. Any assistance on this would be greatly appreciated.

Thank you.
 
your if ... else constructs are unbalanced.

you seem to be using a combination of well structured constructs with curly braces and some with not. those with not are 'legal' but, imo, are bad practice.

replace all with full constructs and things should drop out sensibly.

other things that lok odd

Code:
} elseif ($print == "Print Receipt") {

you have not set the $print variable other than in the if condition so this condition will never be met.

consider using switch statements instead

Code:
	$query = "UPDATE icc_recipts SET donor_first_name ='$donor_first_name', "." donor_middle_initial ='$donor_middle_initial', "." donor_last_name ='$donor_last_name', "." donor_address1 ='$donor_address1', "." donor_address2 ='$donor_address2', "." donor_city ='$donor_city', "." state_id ='$state_id', "." donor_zip ='$donor_zip', "." event_id ='$event_id', "." contrib_description ='$contrib_description', "." payment_type_id ='$payment_type_id', "." total_amount_paid ='$total_amount_paid', "." amount_tax_deductible ='$amount_tax_deductible', "." goods_services_provided = $goods_services_provided "." WHERE receipt_id = $receipt_id";
the dots "." in your code? what are they doing? why are you breaking in and out of the string?

I think that this (slightly cleaned up and simplified) is more like what you meant to do

Code:
<?php 
$fields = array ('receipt_id','donor_first_name','donor_middle_initial','donor_last_name','donor_address1','donor_address2','donor_city','statename','donor_zip','eventtype','contrib_description','total_amount_paid','amount_tax_deductible','paymenttype');

switch($_POST['button']){
	case 'Delete Receipt':
	    $receipt_id = $_POST['receipt_id'];
	    $query = "DELETE from icc_recipts WHERE receipt_id = $receipt_id";
	    $result = mysql_query($query);
	    if ($result) {
	        echo "<h2>Receipt Number: $receipt_id deleted</h2>\n";
	        exit;
	    } else {
	        echo "<h2>Problem deleting receipt $receipt_id</h2>\n";
	        exit;
	    }
		break;
    
	case 'Print Receipt':
		foreach($fields as $field){
			$$field = isset($_POST[$field]) ? trim ($_POST[$field]) : '';
		}
		$goods_service_provided = is_null($goods_services_provided) ? 1 : 0;
		
		$query = "SELECT a.receipt_id, a.donor_first_name, a.donor_last_name, a.donor_address1, a.donor_address2, a.event_id, a.donor_city, a.donor_middle_initial, a.donor_zip, a.contrib_description, a.total_amount_paid, a.amount_tax_deductible, a.goods_services_provided, d.payment_type_id As payment_type_id, 
		b.state_id As state_id, c.event_description As event_description FROM icc_recipts a, states_lookup b, events_lookup c, payment_type_lookup d 
		 WHERE  a.state_id = b.state_id AND a.payment_type_id = d.payment_type_id AND a.event_id = c.event_id AND a.receipt_id = $receipt_id";
		$result = mysql_query($query);
		$row = mysql_fetch_array($result, MYSQL_ASSOC);
		if(!$row) die (mysql_error());
		extract ($row);
		$query = "SELECT state_id, state_name from states_lookup WHERE state_id = $state_id";
		$result = mysql_query($query);
		$row = mysql_fetch_array($result, MYSQL_ASSOC);
		$statename = $row['state_id'];
		$query = "SELECT payment_type_id, payment_type from payment_type_lookup WHERE payment_type_id = $payment_type_id";
		$result = mysql_query($query);
		$row = mysql_fetch_array($result, MYSQL_ASSOC);
		$paymenttype = $row['payment_type_id'];
		$query = "SELECT event_id, event_description from events_lookup WHERE event_id = $event_id";
		$result = mysql_query($query);
		$row = mysql_fetch_array($result, MYSQL_ASSOC);
		$eventname = $row['event_id'];
		echo "<h3 align=center>The Ivy Community Charities of Prince George's County, Incorporated</h3>\n";
		echo "<h4 align=center>(a 501(c) 3 organization)</h4>\n";
		echo "<h2 align=center>Contribution Acknowledgement/Receipt</h2>\n";
		echo "<form enctype=\"multipart/form-data\" action=\"admin.php\" method=\"post\">\n";
		echo "<input type=\"hidden\" name=\"receipt_id\" value=\"$receipt_id\">\n";
		echo "<table width=\"100%\" cellpadding=\"0\" border=\"0\">\n";
		echo "<tr><td><h3>Receipt ID</h3></td><td>$receipt_id</td></tr>\n";
		echo "<tr><td><h3>Donor's Name</h3></td><td>$donor_first_name</td>\n";
		echo "<td>$donor_middle_initial</td>\n";
		echo "<td>$donor_last_name</td></tr>\n";
		echo "<tr><td><h3>Donor Address 1</h3></td><td><input type=\"text\" name=\"donor_address1\"   value=\"$donor_address1\"></td></tr>\n";
		echo "<tr><td><h3>Donor Address 2</h3></td><td><input type=\"text\" name=\"donor_address2\"   value=\"$donor_address2\"></td></tr>\n";
		echo "<tr><td><h3>Donor City</h3></td><td><input type=\"text\" name=\"donor_city\"   value=\"$donor_city\"></td></tr>\n";
		
		echo "<tr><td><h3>Donor State</h3><td><select name=\"statename\">\n";
		$query = "SELECT state_id, state_name from states_lookup";
		$result = mysql_query($query);
		while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
		    $tempstateid = $row['state_id'];
		    $state_name = $row['state_name'];
		    if ($tempstateid == $state_id) {
		        echo "<option value=\"$tempstateid\" selected=\"selected\">$state_name</option>\n";
		    } else {
		        echo "<option value=\"$tempstateid\">$state_name</option>";
			}
		}
		echo "</select></td></tr>\n";
		echo "<tr><td><h3>Donor Zip</h3></td><td><input type=\"text\" name=\"donor_zip\"   value=\"$donor_zip\"></td></tr>\n";
		echo "<tr><td><h3>Event Type</h3><td><select name=\"eventtype\">\n";
		
		$query = "SELECT event_id, event_description from events_lookup";
		$result = mysql_query($query);
		while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
		    $tempeventtypeid = $row['event_id'];
		    $event_description = $row['event_description'];
		    if ($tempeventtypeid == $event_id){
		        echo "<option value=\"$tempeventtypeid\" selected=\"selected\">$event_description</option>\n";
		    } else {
		        echo "<option value=\"$tempeventtypeid\">$event_description</option>";
			}
		}
		echo "</select></td></tr>\n";
		echo "<tr><td><h3>Description</h3></td><td><input type=\"text\" size=\"50\"  name=\"contrib_description\"   value=\"$contrib_description\"></td></tr>\n";
		echo "<tr><td><h3>Payment Type</h3><td><select name=\"paymenttype\">\n";
		$query = "SELECT payment_type_id, payment_type from payment_type_lookup";
		$result = mysql_query($query);
		while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
		    $temppaytypeid = $row['payment_type_id'];
		    $payment_type = $row['payment_type'];
		    if ($temppaytypeid == $payment_type_id){
		        echo "<option value=\"$temppaytypeid\" selected=\"selected\">$payment_type</option>\n";
		    } else {
		        echo "<option value=\"$temppaytypeid\">$payment_type</option>";
			}
		}
		echo "</select></td></tr>\n";
		echo "<tr><td><h3>Total Amount Paid</h3></td><td><input type=\"text\" name=\"total_amount_paid\" value=\"$total_amount_paid\"></td></tr>\n";
		echo "<tr><td><h3>Amount Tax Deductible</h3></td><td><input type=\"text\" name=\"amount_tax_deductible\" value=\"$amount_tax_deductible\"></td></tr>\n";

		if ($goods_services_provided) {
    		echo "<tr><td><h3>Goods or Services Provided?</h3></td><td><input type=\"checkbox\" name=\"goods_services_provided\" value=\"1\" checked></td></tr>\n";
		} else {
    		echo "<tr><td><h3>Goods or Services Provided?</h3></td><td><input type=\"checkbox\" name=\"goods_services_provided\" value=\"1\"></td></tr>\n";
		}
		echo "</table>\n";
		echo "</form>\n";
		exit;
	case 'Update':
		$array = get_magic_quotes_gpc() ? array_map('stripslashes', $_POST) : $_POST;
		foreach($fields as $field){
			$$field = isset($array[$field]) ? mysql_real_escape_string(trim ($array[$field])) : '';
		}
		
		$goods_service_provided = is_null($goods_services_provided) ? 1 : 0;
	    
    	$query = "UPDATE icc_recipts SET 
					donor_first_name ='$donor_first_name', 
					donor_middle_initial ='$donor_middle_initial',
					donor_last_name ='$donor_last_name',
					donor_address1 ='$donor_address1', 
					donor_address2 ='$donor_address2',
					donor_city ='$donor_city',
					state_id ='$state_id',
					donor_zip ='$donor_zip', 
					event_id ='$event_id', 
					contrib_description ='$contrib_description', 
					payment_type_id ='$payment_type_id', 
					total_amount_paid ='$total_amount_paid', 
					amount_tax_deductible ='$amount_tax_deductible', 
					goods_services_provided = $goods_services_provided
				WHERE receipt_id = $receipt_id";
    
		$result = mysql_query($query) or die(mysql_error());
		if ($result) {
		    echo "<h2>Receipt information changed.</h2>\n";
		} else {
		    echo "<h2>Sorry, the receipt information could not be changed.</h2>\n";
		}
		break;
} //end switch
?>
 
Thank you very much JPadie, the switch statement works just fine. Using switch instead of if-else for conditional statements in the PHP code works better for me.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top