PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 29th, 2012, 08:52 AM
Cyd44 Cyd44 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 17 Cyd44 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 52 m 36 sec
Reputation Power: 0
PHP-General - 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

Reply With Quote
  #2  
Old November 29th, 2012, 08:54 AM
CWhittaker CWhittaker is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 9 CWhittaker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 56 m 13 sec
Reputation 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

Reply With Quote
  #3  
Old November 29th, 2012, 08:57 AM
CWhittaker CWhittaker is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 9 CWhittaker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 56 m 13 sec
Reputation Power: 0
Quote:
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

Reply With Quote
  #4  
Old November 29th, 2012, 09:06 AM
gw1500se gw1500se is online now
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jul 2003
Posts: 2,880 gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Year 2 Weeks 2 Days 4 h 10 m 34 sec
Reputation Power: 581
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.

Reply With Quote
  #5  
Old November 29th, 2012, 09:18 AM
Cyd44 Cyd44 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 17 Cyd44 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 52 m 36 sec
Reputation Power: 0
Quote:
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

Reply With Quote
  #6  
Old November 29th, 2012, 09:22 AM
gw1500se gw1500se is online now
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jul 2003
Posts: 2,880 gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Year 2 Weeks 2 Days 4 h 10 m 34 sec
Reputation Power: 581
It is not the explanation that is the problem. You have coding errors. Fix them first then see where it goes.

Reply With Quote
  #7  
Old November 29th, 2012, 09:46 AM
Cyd44 Cyd44 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 17 Cyd44 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 52 m 36 sec
Reputation Power: 0
Quote:
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

Reply With Quote
  #8  
Old November 29th, 2012, 10:05 AM
Jacques1's Avatar
Jacques1 Jacques1 is online now
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,870 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 14 m 46 sec
Reputation Power: 813
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.

Reply With Quote
  #9  
Old November 29th, 2012, 10:23 AM
Cyd44 Cyd44 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 17 Cyd44 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 52 m 36 sec
Reputation Power: 0
Quote:
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

Reply With Quote
  #10  
Old November 29th, 2012, 10:41 AM
Cyd44 Cyd44 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 17 Cyd44 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 52 m 36 sec
Reputation 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>

Reply With Quote
  #11  
Old November 29th, 2012, 10:53 AM
gw1500se gw1500se is online now
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jul 2003
Posts: 2,880 gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Year 2 Weeks 2 Days 4 h 10 m 34 sec
Reputation Power: 581
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!

Reply With Quote
  #12  
Old November 29th, 2012, 10:55 AM
Jacques1's Avatar
Jacques1 Jacques1 is online now
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,870 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 14 m 46 sec
Reputation Power: 813
Quote:
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)



Quote:
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.

Reply With Quote
  #13  
Old November 29th, 2012, 11:23 AM
Cyd44 Cyd44 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 17 Cyd44 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 52 m 36 sec
Reputation 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

Reply With Quote
  #14  
Old November 29th, 2012, 11:30 AM
Jacques1's Avatar
Jacques1 Jacques1 is online now
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,870 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 14 m 46 sec
Reputation Power: 813
Quote:
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.

Reply With Quote
  #15  
Old November 29th, 2012, 12:23 PM
Cyd44 Cyd44 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 17 Cyd44 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 52 m 36 sec
Reputation Power: 0
Quote:
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > PHP-General - Code Error

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap