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($sq l)";
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?
5 13144 Atli 5,058
Recognized Expert Expert
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($sq l);
[/PHP]
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($sq l);
[/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?
Motoma 3,237
Recognized Expert Specialist
This should work:
[PHP]
$sql = "INSERT INTO 'users' ('userid', 'username', 'upassword') VALUES ('".$_POST['userid']."', '".$_POST['username']."', '".$_POST['upassword']."')";
mysql_query($sq l);
[/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 "."
Atli 5,058
Recognized Expert Expert
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.).
Thanks. It did work. My first php code to work.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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 submitting user
information into my creation script, I get the following error from the
page that is suppose to insert the user data into the database,...
|
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
/home/midwestm/public_html/support/tutorials.php on line 15.
When running the script below, I believe the error has only started to occur
since we made the move to a server with global...
|
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 script, but if i disable an if statement (code below) then my code functions correctly...
if($specs == "DT")
{
$angle = $specs;
$rating =...
|
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
if(isset($_GET)) {
echo "<div id="visible">";
echo "<span onclick="swapform()">Log In Form</span>";
echo "</div>";
echo "<div id="theform"...
|
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 T_ENCAPSED_AND_WHITESPACE, expecting ']' in C:\wamp\www\dwalk96\sign-up.php on line 26
Here is all the code of the file.
<?php
// Connects to your Database ...
| |
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 shortcomings will soon become apparent.
I am developing an estimating construction system, using PHP5 and MySQL 5.0.24a with Ubuntu. I have a main...
|
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 T_VARIABLE or T_NUM_STRING in /var/www/html/inventoryControl/supplier.php on line 26 (line number changed to match code tags)
The code is as follows:
...
|
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 on line 248
so ive checked me code over and over again but cant find whats wrong. So getting a annoyed I commented it to move on to next error but...
|
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 my code:
<?php
//Connect To Database
$hostname='mideasthonors2.db.4381361.hostedresource.com';
$username='**************';...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| | |