Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

unexpected T_VARIABLE in /home2/swhisa/public_html/suggestion/sugadddb.php on line 8

Question posted by: proudlyphp (Newbie) on July 17th, 2008 06:59 AM
unexpected T_VARIABLE in /home2/swhisa/public_html/suggestion/sugadddb.php on line 8

My code is the following and I get the error message above. Please help
Code: ( text )
  1. <?php
  2.  
  3. ob_start();
  4.     include ('../common/connecttodb.php');
  5.     echo "Connection Successful!";
  6.    
  7. mysql_query ('INSERT INTO `swhisa_swhisadb`.`tblsug` SET
  8. 2ale = '$HTTP_GET_VARS['txtsuggest']'        ');
  9.  
  10.  
  11. header('Location:../index.php');
  12.  
  13. ob_end_flush();
  14.  
  15. ?>

since I am new on this forum, you can send me your answers through my email too: <email removed>
Last edited by Atli : July 17th, 2008 at 09:05 AM. Reason: Added [code] tags and removed email.
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
proudlyphp's Avatar
proudlyphp
Newbie
15 Posts
July 17th, 2008
07:25 AM
#2

Re: unexpected T_VARIABLE in /home2/swhisa/public_html/suggestion/sugadddb.php on line 8
I have the same problem. Please help is needed.

Reply
darksteel21's Avatar
darksteel21
Newbie
11 Posts
July 17th, 2008
08:35 AM
#3

Re: unexpected T_VARIABLE in /home2/swhisa/public_html/suggestion/sugadddb.php on line 8
try to make your "mysql_query ('INSERT INTO `swhisa_swhisadb`.`tblsug` SET
2ale = '$HTTP_GET_VARS['txtsuggest']' ');"
a one line..i hope it will help..

Reply
Atli's Avatar
Atli
Moderator
2,224 Posts
July 17th, 2008
09:12 AM
#4

Re: unexpected T_VARIABLE in /home2/swhisa/public_html/suggestion/sugadddb.php on line 8
Hi. Welcome to Bytes!.

The problem there is that you are opening and closing the string in random places while you try to build it...
You can't use a single-quote mark inside a single-quote mark enclosed string, obviously.

For example, this is a simplifyed version of what you are doing:
Code: ( text )
  1. $str = 'Your name is '$yourname'.';

You can see why that would be a problem right?
The single-quote mark meant to be inside the string is in fact closing the string, at which point the $yourname variable is out of place, causing the parse error.

Instead, try to either enclose the string in double-quote marks or escape the additional single-quotes.
Like:
Code: ( text )
  1. $str = "Your name is '$yourname'";
  2. $str = 'Your name is \''. $yourname .'\'';

Note, that because the second example there is enclosed in single-quote marks, I can not use variable names directly in the string. I had to end the string and add it using a dot.

Also note that when adding array elements directly into a string, it is advisable to use curly-braces.
Like:
Code: ( text )
  1. $str = "Your name is {$_POST['Username']}";

Reply
Atli's Avatar
Atli
Moderator
2,224 Posts
July 17th, 2008
09:17 AM
#5

Re: unexpected T_VARIABLE in /home2/swhisa/public_html/suggestion/sugadddb.php on line 8
And please remember to post your code inside [code] tags.

I've also removed the email in your post, as posting emails is not allowed in the technical forums.

Please take a look at our Posting Guidelines for more detail on that.

Moderator

Reply
proudlyphp's Avatar
proudlyphp
Newbie
15 Posts
July 17th, 2008
02:41 PM
#6

Re: unexpected T_VARIABLE in /home2/swhisa/public_html/suggestion/sugadddb.php on line 8
Quote:
Originally Posted by darksteel21
try to make your "mysql_query ('INSERT INTO `swhisa_swhisadb`.`tblsug` SET
2ale = '$HTTP_GET_VARS['txtsuggest']' ');"
a one line..i hope it will help..


Thank you darksteel21 and Atli, too. You are great help. I tried your suggestions. Yes I put it all in double quotes and into a single line. There is still a problem though it does not show an error message. The new problem is that it goes smoothly but when I checked the database from PhpMyAdmin, there is no data entered. In the table named tblsug, the column name is 2ale, which is identical to the code.

Please help.

The improved codes as per your suggestion is as follows:
Code: ( text )
  1. <?php
  2.  
  3. ob_start();
  4.     include ('../common/connecttodb.php');
  5.     echo "Connection Successful!";
  6.    
  7. "mysql_query('INSERT INTO `swhisa_swhisadb`.`tblsug` SET
  8. 2ale = '{$HTTP_GET_VARS['txtsuggest']}' ')";
  9.  
  10. header('Location:../index.php');
  11. ob_end_flush();
  12.  
  13. ?>

Last edited by Atli : July 20th, 2008 at 06:08 PM. Reason: Added [code] tags
Reply
Atli's Avatar
Atli
Moderator
2,224 Posts
July 20th, 2008
06:21 PM
#7

Re: unexpected T_VARIABLE in /home2/swhisa/public_html/suggestion/sugadddb.php on line 8
You don't want to put the function call into double-quotes, only the query strings itself.

Like:
Code: ( text )
  1. $colValue = "Some value";
  2. $result = mysql_query("INSERT INTO tbl(`colName`) VALUES('$colValue')");


P.S.
You should never put unvalidated user input into a query string like you do there.
What if I were to type the following as the GET parameter?:
Code: ( text )
  1. first', 'second', 'third', 'ect...

Now there I've just added three additional rows into your database that your code didn't account for...
And that example is a very innocent one... I could do some serious damage there if I really wanted to.

Before you use any user input anywhere in your site, make sure that it is in fact valid.
These function may help get you started:
mysql_real_escape_string, htmlentities, addslashes.

And again...
Please remember to post your code inside [code] tags!

Thank you.

Reply
Reply
Not the answer you were looking for? Post your question . . .
183,906 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
Top PHP Forum Contributors