By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,650 Members | 1,823 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,650 IT Pros & Developers. It's quick & easy.

Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or

P: 6
I am new to php and have written the following mysql code to enter the details of a new user in the admin subdomain of my website:

$sql = "INSERT INTO 'users' ('userid', 'username', 'upassword')
VALUES ('$_POST['userid']', '$_POST['username']', '$_POST['upassword']')
mysql_query($sql)";

When I view the code in Internet Explorer I get the following error message:

Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

The error is said to be in the line of code above. Will you please assist me?
Mar 27 '07 #1
Share this Question
Share on Google+
5 Replies


Atli
Expert 5K+
P: 5,058
Hi.

I see three problems.

First one is that you have your mysql_query() function inside your SQL query string. That's not gona work, obviously.

Second. You use &_POST['userid'] inside a string. Putting simple variable names inside a string like that is ok, but with complex array variables like that you will have to put {} arround it.

Third. In the SQL query, where you specify the columns you want the data to put into, you use single quote marks arround the names ('). You need to remove the single quote marks arround the column names and table and database names. The single quote marks are only required arround string data.

This is the edited code:
[PHP]
$sql = "INSERT INTO users (userid, username, upassword)
VALUES ('{$_POST['userid']}', '{$_POST['username']}', '{$_POST['upassword']}')";
mysql_query($sql);
[/PHP]
Mar 27 '07 #2

P: 6
Hi.

I see three problems.

First one is that you have your mysql_query() function inside your SQL query string. That's not gona work, obviously.

Second. You use &_POST['userid'] inside a string. Putting simple variable names inside a string like that is ok, but with complex array variables like that you will have to put {} arround it.

Third. In the SQL query, where you specify the columns you want the data to put into, you use single quote marks arround the names ('). You need to remove the single quote marks arround the column names and table and database names. The single quote marks are only required arround string data.

This is the edited code:
[PHP]
$sql = "INSERT INTO users (userid, username, upassword)
VALUES ('{$_POST['userid']}', '{$_POST['username']}', '{$_POST['upassword']}')";
mysql_query($sql);
[/PHP]
I have tried that but it gave me an error message which said: unexpected '}' expecting ']'. When I put the square brackets, I got the first error that I asked about. I have changed the code to

$sql = "INSERT INTO users (userid, username, upassword)
VALUES ('{"$_POST['userid']"}' , '{"$_POST['username']"}' , '{"$_POST['upassword']"}') ";


Now it gives me this error: parse error, unepected T_VARIABLE. What is it and how do I solve this error?
Mar 27 '07 #3

Motoma
Expert 2.5K+
P: 3,235
This should work:

[PHP]
$sql = "INSERT INTO 'users' ('userid', 'username', 'upassword') VALUES ('".$_POST['userid']."', '".$_POST['username']."', '".$_POST['upassword']."')";
mysql_query($sql);
[/PHP]

You did not have a final " at the end of your SQL statement, and, you did not concatenate the post values into the string. Notice my usage of the string concatenation operator "."
Mar 27 '07 #4

Atli
Expert 5K+
P: 5,058
The snippet I posted earlier works on my localhost. Im using PHP 5.2.1, not sure if earlier versions of PHP have a problem with {} inside strings but I guess that might be it.

The code that Motoma posted also works, and is infact simpler.

The reason, however, for the error you got was because you used " (double quote marks) arround the array variable inside the {} which did infact close the string and then open it again. The PHP parser was excpecting a ; to close the statement when you closed the string. By adding a simple dot, as Motoma did, the variables are added to the string, kind of like + signs are used in most programing languages (C#, java, etc.).
Mar 27 '07 #5

P: 6
Thanks. It did work. My first php code to work.
Mar 28 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.