473,414 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

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

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
5 13127
Atli
5,058 Expert 4TB
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
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
3,237 Expert 2GB
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
5,058 Expert 4TB
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
Thanks. It did work. My first php code to work.
Mar 28 '07 #6

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

Similar topics

6
by: Ehartwig | last post by:
I recently created a script for user verification, solved my emailing issues, and then re-created the script in order to work well with the new PHP 5 that I installed on my server. After...
6
by: Ben Allen | last post by:
Hi, Im currently getting the error: Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in...
1
epots9
by: epots9 | last post by:
Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /var/www/html/xxx.php on line xxx I get that message when i try to run my...
4
kestrel
by: kestrel | last post by:
I have some html code that is supposed to be displayed by php echo. But for some reason i keep getting a syntax error, and i cant figure out what is going on. Heres what i have <?php...
3
by: basswhizz | last post by:
Hi everyone im a high school student and I'm having trouble with one of my projects could you please help!! I'm getting this error message. Parse error: syntax error, unexpected...
36
by: rhys | last post by:
My Gurus and Angels -- Please pardon this old-school programmer, only recently enlightened to open-source, having been trapped in the convenience of proprietary lingos for way too long. My...
2
by: fburn | last post by:
I need some help with an error I'm getting using php 5.2.5 running on linux. I receive an error: Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or...
15
by: micky125 | last post by:
Lo all, Ive been getting the error message Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/moneill/public_html/input.html...
68
mideastgirl
by: mideastgirl | last post by:
I keep getting this error and I cannot figure it out. My curly brackets are closed, and I am using the correct tags for <?php to open and ?> to close my code. Can someone please help me! Here is...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.