Page 1 of 1

Parse error: syntax error, unexpected T_ELSE

Posted: Sat Apr 16, 2011 2:05 pm
by liyun88
hi,i face some problem about T_ELSE..i dont know how to put them..
actually i want that if the product is already in the shopping cart,it will not insert into cart table,but if the product is not in the shopping cart,it will insert into shopping cart..so i use if else function..i have error in the code..
can anyone help me to solve them??thanks in advance..

Code: Select all

<?php 
session_start();
require_once 'config.php' ;
require_once 'application.php' ;

//var_dump($_FILES);
  
 $id = $_SESSION['id'];
 $username = $_SESSION['username'];
 $pid=$_POST['pid'];
 $pname=$_POST['pname'];
 $uprice=$_POST['uprice'];
 $quantity=$_POST['quantity'];
 
  $query = "SELECT * FROM products WHERE pid = $pid"; 
  $result = mysql_query($query);
  $row=mysql_fetch_row($result);
  $pid =   $row[1];
  $pname = $row[3];
  $price = $row[5];
  $promo = $row[6];
  
  if ($promo != '0.00')
			{ $uprice = $promo; }
			else
			{ $uprice = $price; }
		

 
  $query1 = "SELECT * FROM register WHERE id =$id";   
  $result1 = mysql_query($query1);
  $row1=mysql_fetch_row($result1);
  
  if(isset($_SESSION['pid'])){
     if(in_array($_POST['pid'],$_SESSION['pid'])){
	   header('Content-type:text/html;charset=utf-8');
	   echo"<script>alert(\"Your product is in your shopping cart!\")</script><script>window.location='home.php?id=$id'</script>";  
	   
	   }
     }
	  
	   $_SESSION['pid'][] = $_POST['pid'];
	   $_SESSION['pname'][] = $_POST['pname'];
	   $_SESSION['uprice'][] = $_POST['uprice'];
	   $_SESSION['quantity'][]=$_POST['quantity'];
  
        else {
		$result2 = mysql_query("INSERT INTO cart  VALUES('','$id','$username','$pid','$pname','$uprice','$quantity')");

				 
 echo"<script>alert(\"Saved Successfully!\")</script><script>window.location='home.php?id=$id'</script>";  }
  
?>

Re: Parse error: syntax error, unexpected T_ELSE

Posted: Sat Apr 16, 2011 2:31 pm
by fugix
its because you set session variables after the last if..the else must come the line after the closing if function bracket

Re: Parse error: syntax error, unexpected T_ELSE

Posted: Sat Apr 16, 2011 10:26 pm
by liyun88
but if i close } after set session,it not have function not display blank screen..

Re: Parse error: syntax error, unexpected T_ELSE

Posted: Sat Apr 16, 2011 10:38 pm
by fugix

Code: Select all

}
         
           $_SESSION['pid'][] = $_POST['pid'];
           $_SESSION['pname'][] = $_POST['pname'];
           $_SESSION['uprice'][] = $_POST['uprice'];
           $_SESSION['quantity'][]=$_POST['quantity'];
 
        else {


should be....


}
else {
         
           $_SESSION['pid'][] = $_POST['pid'];
           $_SESSION['pname'][] = $_POST['pname'];
           $_SESSION['uprice'][] = $_POST['uprice'];
           $_SESSION['quantity'][]=$_POST['quantity'];