473,408 Members | 1,729 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,408 software developers and data experts.

Insert Data From a Form Into a Database

While inserting data from a form into a database i am getting error in the below code only. The error displayed is :( ! ) Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting ']'
Expand|Select|Wrap|Line Numbers
  1. $sql= "INSERT INTO Persons (FirstName,LastName,Age) VALUES('$_POST[FirstName ]','$_POST[LastName]','$_POST[Age]')";
  2.  
  3.    if (!mysqli_query($con,$sql))
  4.    {
  5.    die('Error: ' . mysqli_error($con));
  6.    }
  7.    echo "1 record added";
  8.  
  9.    mysqli_close($con); 
  10.    ?> 
  11.  
what does the displayed error mean? what is the possible solution?
Jan 1 '14 #1

✓ answered by zmbd

check this $_POST[FirstName ]'
should it be this $_POST[FirstName]' ?

8 1858
zmbd
5,501 Expert Mod 4TB
check this $_POST[FirstName ]'
should it be this $_POST[FirstName]' ?
Jan 1 '14 #2
The proposed solution does not work. The following error was displayed:
Database mi_db created successfullyTable persons created successfully
( ! ) Notice: Undefined index: FirstName in C:\wamp\www\insert.php on line 29
Call Stack
# Time Memory Function Location
1 0.0027 252088 {main}( ) ..\insert.php:0

( ! ) Notice: Undefined index: LastName in C:\wamp\www\insert.php on line 29
Call Stack
# Time Memory Function Location
1 0.0027 252088 {main}( ) ..\insert.php:0

( ! ) Notice: Undefined index: Age in C:\wamp\www\insert.php on line 29
Call Stack
# Time Memory Function Location
1 0.0027 252088 {main}( ) ..\insert.php:0
1 record added

Please suggest the possible solution...?
Jan 2 '14 #3
Dormilich
8,658 Expert Mod 8TB
The proposed solution does not work.
it did work. you just hit the next problem the previous error was hiding (because it triggered a script exit first).
Jan 2 '14 #4
Rabbit
12,516 Expert Mod 8TB
The next error being that the variable with those names were not passed to the PHP page.
Jan 2 '14 #5
So what is the solution to the above error?
Jan 2 '14 #6
The code is:
Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php 
  3. // Connects to your Database 
  4. $con = mysqli_connect("localhost", "root", "mysql@777") or die(mysqli_error());
  5. $db_selected = mysqli_select_db($con, 'mi_db');
  6. if (!$db_selected) 
  7. {
  8. die ('Can\'t use mi_db : ' . mysqli_error());
  9. }
  10. $sql= "INSERT INTO Persons (FirstName,LastName,Age) VALUES('$_POST[FirstName]','$_POST[LastName]','$_POST[Age]')";
  11.  
  12. if (!mysqli_query($con,$sql))
  13. {
  14. die('Error: ' . mysqli_error($con));
  15. }
  16. echo "1 record added";
  17.  
  18. mysqli_close($con); 
  19. ?> 
  20.  
enclosed herewith the code of the above program.Please suggest in view of the above code possible solution of the same.
Jan 2 '14 #7
zmbd
5,501 Expert Mod 4TB
New Question, New Thread.
Respectfully, we ask that each thread contain only one question.
Please start a new thread and, if needed, you can include a link to this thread for context.

Thnx
Jan 2 '14 #8
Ok & Thanks

I have posted a new thread linked to this as :PHP script for inserting a form data to a database. Suggest Possible solution of the problem?
Jan 2 '14 #9

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

Similar topics

3
by: c676228 | last post by:
Hi all, I think I have data type problem when I tried to insert data into sql server 200. The erroe message is like this: system.invalidcastexception data type DFlage char(1) in sql 2000, DMI...
1
by: Doc11 | last post by:
I'm trying to allow users insert data into a database using the form view. But when I click the insert button I get this error: Server Error in '/Customer Database' Application....
1
by: rahia307 | last post by:
hi to all i want to use Aajx in php and want to insert data into database. but i have no idea about it. if any one have idea for it please help me
1
by: SuvarnaChaudhari | last post by:
Hi,Plz any one can tell me how to insert data in database through GridView,or there is no way,then plz tell me another way(I mean,should I use DetailView?but how?) Thaknk You!
2
chathura86
by: chathura86 | last post by:
public void insertData(String sql) { if(conn != null) { SqlCommand cmd = new SqlCommand(); cmd.Connection = conn ; //setting the connection to sql command ...
3
by: bobbyze | last post by:
how to insert data into database using php with OOP structure
0
by: ghjk | last post by:
I want to insert web application data to mysql database. I'm using struts, jsp and hibernet.This is my action class. public ActionForward execute(ActionMapping mapping, ActionForm form, ...
3
by: abisiva | last post by:
Hi, I am new for PHP OOP concept. Pls give some example source code for "How to insert data using oops concept?" I want to improve my knowledge. pls any one help me.... I know below the basic...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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
agi2029
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,...
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.