Thread: Code Error

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    17
    Rep Power
    0

    Code Error


    I am getting a syntax error unexpected T_Variable at line 95 (which is the SELECT query line. I have 3 fields in a database and need to select only 1 which is dependant upon the value of another variable.

    IE. If var1 MyType=1 the var2 SClass should be set to the relevent field in table, and so on. I am a novice in php and have coded as follows
    Code:
    If ($MyType='1') {
    	     $Class = 'SClass1';  
    		 }else {
                    If ($MyType=2) {
    		        $Class = 'SClass2';  
                     }else {
                            If ($MyType=3) {
                            $Class = 'SClass3;;
    	                    }
    						}
    						
    	 $qry="SELECT $Class FROM patient WHERE UID='$MyName'";
    	     if(!$result) {
    	     die('Failed to Update ' . mysql_error());
    		 } Else {
    	     $result=mysql_query($qry); 
             $row = mysql_fetch_array($result); 
             $MyClass = $row['$Class']; 
    		 }
    	var_dump($_POST);
    	
    	//Create Insert query and test it is OK
    	$qry = "INSERT INTO siezure_event(Patient_UID,Medication,Siezure_Type,Seizure_Class,Siezure_Date,Siezure_Time,Notes) VALUES ('$MyName', '$MyMed', '$SType', '$MyClass', '$PostDate', '$MyTOD', '$MyNotes')";
    	$result = @mysql_query($qry);
    	if(!$result) {
    	die('Failed to Update ' . mysql_error());
    Given the synatx error, I suspect I cannot select a $variable from a table. Could someone please advise me
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    9
    Rep Power
    0
    I dont know on this one, but as a first glance have you tried takin away the ' from either side of $class as its not text you are entering
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    9
    Rep Power
    0
    Originally Posted by Cyd44
    $result = @mysql_query($qry);
    Or it could be that line!, take out the @ symbol.

    And i normally use a $result2 for a second value
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,493
    Rep Power
    594
    You have several problems but I don't know which one is causing your error so you need to fix them first. As CWhittaker suggested you are misusing single quotes. Substitution does not work within them. Also please edit your post to use [ PHP ] tags not [ CODE ] tags for PHP. You indentation technique needs some work too. See ManiacDan's New User Guide.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    17
    Rep Power
    0
    Originally Posted by CWhittaker
    I dont know on this one, but as a first glance have you tried takin away the ' from either side of $class as its not text you are entering
    Thanks for response. Maybe it would be better explained thus.

    My table has fields SClass1, SClass2 and SClass3 along with others fields, one of which is Type. The Class fields relate to the type fields and each type can have a different Class definition.
    I have a web form to enter details and, depending on the type entered, I want to retrieve the associated.

    There should only ever be 1 row retrieved as a result of the query.

    Hope this explains better
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,493
    Rep Power
    594
    It is not the explanation that is the problem. You have coding errors. Fix them first then see where it goes.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    17
    Rep Power
    0
    Originally Posted by gw1500se
    It is not the explanation that is the problem. You have coding errors. Fix them first then see where it goes.
    OK, but if I new what the coding errors were, I would not need to ask you guys? I have noted the error on Class3 as I had missed the quotes and had 2 ;; instead. I had also noted I had note quoted the select $Class varia
    ble. This fixed orginal syntax error but am now getting the syntax error, unexpected $end . I know this means that there is some coding problem but I dont know what, hence my request here.

    The edited code is as follows:-
    PHP Code:
     If ($MyType='1') {
             
    $Class SClass1;  
             }else {
                    If (
    $MyType=2) {
                    
    $Class SClass2;  
                     }else {
                            If (
    $MyType=3) {
                            
    $Class SClass3;
                            }}
                            
                            
         
    $qry="SELECT '$Class' FROM patient WHERE UID='$MyName'";
             if(!
    $result) {
             die(
    'Failed to Update ' mysql_error());
             } Else {
             
    $result=mysql_query($qry); 
             
    $row mysql_fetch_array($result); 
             
    $MyClass $row['$Class']; 
             }
        
    var_dump($_POST);
        
        
    //Create Insert query and test it is OK
        
    $qry "INSERT INTO siezure_event(Patient_UID,Medication,Siezure_Type,Seizure_Class,Siezure_Date,Siezure_Time,Notes) VALUES ('$MyName', '$MyMed', '$SType', '$MyClass', '$PostDate', '$MyTOD', '$MyNotes')";
        
    $result = @mysql_query($qry);
        if(!
    $result) {
        die(
    'Failed to Update ' mysql_error()); 
        }else {
        
    header("location: record-siezure-success.php");
        exit();
             }
        
    ?> 
    Now just getting the unexpected end error. You will note that I have taken the quotes from around the SCLass vars as suggested but cant see how this is right because I am declaring the name for a field I want to Select in the SQL. However, I realise that there is still some code error but am ay a loss. I do thank you for any advice, or criticism for that matter
  14. #8
  15. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    it's a problem of the braces of the "if" statements (a closing one is missing, as far as I can see).

    I think the main problem here is that your code formatting is extremely messy (like gw1500se already said). Your identation is broken, and you randomly switch between lOwER CaSe and uPpeR cASe, both in your code and your database identifiers.

    If you fix the formatting and start to write clean code, you'll get much less errors of that kind, because you can actually see where a statement begins and where it ends.

    Formatting isn't just aesthetics. It's necessary for correct code.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    17
    Rep Power
    0
    Originally Posted by Jacques1
    Hi,

    it's a problem of the braces of the "if" statements (a closing one is missing, as far as I can see).

    I think the main problem here is that your code formatting is extremely messy (like gw1500se already said). Your identation is broken, and you randomly switch between lOwER CaSe and uPpeR cASe, both in your code and your database identifiers.

    If you fix the formatting and start to write clean code, you'll get much less errors of that kind, because you can actually see where a statement begins and where it ends.

    Formatting isn't just aesthetics. It's necessary for correct code.
    Thanks Jaques1. I note your constructive comments and will take them on board. I am not a professional php developer and am working on a personal project in order to improve my skills. Problem is that when you look on various sites for examples there does not appear to be any standard or consistency in coding, hence I suspect my random caseing. I am used to puttimg variables in upper case so most often do that.

    You state that I have a mising brace on the if statement. Given that I am not profiecient I dont know the correct construct for this. Are you saying I should have 2 clossing braces at the end of my if then else statement? If so, I tried this and still got the error?

    I will get there in the end but at the moment, there is a lot of trial and error. I know what I want to do but am very inexperienced on the syntax requirements
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    17
    Rep Power
    0
    Hi Guys,

    I have got the page to run without errors at last......thanks for all the advice. It turns out that I had not closed off the If..Then...Else statementwith the required number of braces (As Jaques1 pointed out). It now runs but I dont think it is getting the information required. My SClass data is empty?

    Is there any way I can see waht the variable values are? The var_dump assignment only seems to work if it encounters a specific error>
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,493
    Rep Power
    594
    STOP! You can't learn PHP by just copying code and then get it to do what you want. First, the code you copied is obsolete. It uses the depreciated mysql extensions. You need to be using PDO instead and OOP. If you are trying to learn PHP and database access, you are better off starting with a tutorial. You need to sit down without a keyboard and plan your application. Maybe a flow chart. Once you understand logically what you want to do, then you can start coding (but not by copying other people's mistakes and bad programming practices). Do it in small chunks. For example, get a database connection working first. Then get some classes defined and working. Finally start trying some queries using those classes. When you run into trouble or don't understand something ask here.

    Comments on this post

    • Jacques1 agrees
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  22. #12
  23. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Originally Posted by Cyd44
    Problem is that when you look on various sites for examples there does not appear to be any standard or consistency in coding, hence I suspect my random caseing.
    I don't know which sites you visited, but I've never seen PHP programmers writing "If" and "Else" (mixed with "if" and "else") or using this kind of indentation.

    An "if" statement should look something like this:
    PHP Code:
    if (...) {
        
    // code
        // code
    } elseif (...) {
        
    // code
        // code
    } else {
        
    // code
        // code

    (some people put the braces in separate lines, but I don't think that's relevant here)



    Originally Posted by Cyd44
    I am used to puttimg variables in upper case so most often do that.
    Well, as long as you're programming alone, you can invent your own personal style if you want, but you should at least be consistent.

    Personally, I'd simply use lowercase only. Playing with different cases may look absolutely cool, but from a technical perspective, it's terrible. You never know if it's $Result or $result or $RESULT or whatnot.
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    17
    Rep Power
    0
    Thanks again Jaques.

    I have taken your comments into account and amended code which runs without error. However, there is now a problem in getting the right SClass field based upon the $_Post variable for Type. Results are showing SClass1 nomatter what Type I select. Clearly the If statement is not changing the variable or I am not getting the correct field with the Select statement.

    My amended code is
    PHP Code:
    if ($MyType=0) {
        
    $Class 'Not Yet Known';  
        } elseif (
    $MyType=1) {
        
    $Class SClass1;  
        }elseif (
    $MyType=2) {
        
    $Class SClass2;  
        }else {
        
    $Class SClass3;
        }    
                            
         
    $qry="SELECT $Class FROM patient WHERE UID='$MyName'";
             if(!
    $result) {
             die(
    'Failed to Update ' mysql_error());
             } else {
              
    $result=mysql_query($qry); 
              
    $row mysql_fetch_array($result); 
              
    $MyClass $row
             } 
    I need the $Class variable to choose the field SClass1, SClass2 or SClass3 if the type is either 1,2 or 3 (I start with capuring Type 0 first and assign a text value if this is the case. The Insert statement is putting "SClass1" text into the database and is not changing
  26. #14
  27. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Originally Posted by Cyd44
    Results are showing SClass1 nomatter what Type I select.
    Thats because you have a single "=" (assignment) instead of two "==" (equality) in your "if" statement.
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    17
    Rep Power
    0
    Originally Posted by Jacques1
    Thats because you have a single "=" (assignment) instead of two "==" (equality) in your "if" statement.
    Thanks Jaques1

    I had spotted that by googling syntax and have now got it working

    Many thanks agian for your help. I wont let things beat me and will get there in the end. Nice to have experts to call on when realy stuck though

IMN logo majestic logo threadwatch logo seochat tools logo