Unexpected T_VARIABLE

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Ben11858
Forum Newbie
Posts: 16
Joined: Sat Nov 28, 2009 11:27 am

Unexpected T_VARIABLE

Post by Ben11858 »

I received the follow error from my code...

Code: Select all

Parse error: syntax error, unexpected T_VARIABLE in C:\xampp\htdocs\Briefsystem\Accused.php on line 17
Line 17 is the follow...

Code: Select all

$updatesq = "UPDATE Accused SET firstname = \""$_POST['afname']"\", middlename = \""$_POST['amname']"\" WHERE briefcasen = \"" . $briefcasen . "\"";
I'm using this code on the submit of the form, to update my database from post data. The $briefcase number has been assigned earlier so that it updates the right record and updates the name details in that record, but I'm not sure why am getting this error. I have taken out the $briefcasen details I still get the same error.

Any ideas?
Griven
Forum Contributor
Posts: 165
Joined: Sat May 09, 2009 8:23 pm

Re: Unexpected T_VARIABLE

Post by Griven »

Try this:

Code: Select all

$afname = mysql_real_escape_string($_POST['afname']);
$amname = mysql_real_escape_string($_POST['amname']);
 
$updatesq = "UPDATE Accused SET firstname = '$afname', middlename = '$amname' WHERE briefcasen = '$briefcasen'";
Always, without exception, sanitize all database inputs that come from the POST and GET arrays. This will protect you from SQL Injection.

In addition, when placing variables inside double quotes, you do not need to concatenate. See the code below for a working example.

Code: Select all

$myvar1 = 'foo';
$myvar2 = 'bar';
 
//Using double quotes, we can place variables directly into the string 
//without the use of concatenation
echo "The value of my two variables are $myvar1 and $myvar2";
 
//Alternatively, if we use single quotes, we are required to concatenate
echo 'The value of my two variables are ', $myvar1 ,' and ', $myvar2;
Also take note of my use of commas rather than periods when concatenating the string with single quotes. Commas offer a moderate performance boost over periods in this case.
Ben11858
Forum Newbie
Posts: 16
Joined: Sat Nov 28, 2009 11:27 am

Re: Unexpected T_VARIABLE

Post by Ben11858 »

Thanks mate, your a legend. That worked beautifully...

I dunno if someone can help me with this one, but is there a way to join variables into 1 variable with addition text.. Like for example..

Code: Select all

 
$num1 = 1
$num2 = 2
$num3 = 3
$totalnum = $num1,$num2,num3;
echo $totalnum
 
So that end result is '1,2,3' when echo is done..
User avatar
Grizzzzzzzzzz
Forum Contributor
Posts: 125
Joined: Wed Sep 02, 2009 8:51 am

Re: Unexpected T_VARIABLE

Post by Grizzzzzzzzzz »

yes there is, although its something very basic so it would probably be better for you to learn the concept behind it,

the answer is in this post

viewtopic.php?f=1&t=109492

don't be like the other guy we tried to teach it to :P
Ben11858
Forum Newbie
Posts: 16
Joined: Sat Nov 28, 2009 11:27 am

Re: Unexpected T_VARIABLE

Post by Ben11858 »

Awesome, that worked just perfectly... Got it all joined together like i needed, pretty simple really... Thanks mate its helping me out heaps guys..
Post Reply