Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Newbie
 
Join Date: Jul 2008
Location: Bahirdar
Posts: 16
#1: Jul 17 '08
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
Expand|Select|Wrap|Line Numbers
  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. ?>
  16.  
since I am new on this forum, you can send me your answers through my email too: <email removed>

Newbie
 
Join Date: Jul 2008
Location: Bahirdar
Posts: 16
#2: Jul 17 '08

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


I have the same problem. Please help is needed.
darksteel21's Avatar
Newbie
 
Join Date: Jul 2008
Posts: 15
#3: Jul 17 '08

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..
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,754
#4: Jul 17 '08

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:
Expand|Select|Wrap|Line Numbers
  1. $str = 'Your name is '$yourname'.';
  2.  
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:
Expand|Select|Wrap|Line Numbers
  1. $str = "Your name is '$yourname'";
  2. $str = 'Your name is \''. $yourname .'\'';
  3.  
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:
Expand|Select|Wrap|Line Numbers
  1. $str = "Your name is {$_POST['Username']}";
  2.  
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,754
#5: Jul 17 '08

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
Newbie
 
Join Date: Jul 2008
Location: Bahirdar
Posts: 16
#6: Jul 17 '08

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:
Expand|Select|Wrap|Line Numbers
  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. ?>
  14.  
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,754
#7: Jul 20 '08

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:
Expand|Select|Wrap|Line Numbers
  1. $colValue = "Some value";
  2. $result = mysql_query("INSERT INTO tbl(`colName`) VALUES('$colValue')");
  3.  
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?:
Expand|Select|Wrap|Line Numbers
  1. first', 'second', 'third', 'ect...
  2.  
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